Sending email with python -
i have following code
import smtplib sender = 'sender@sender.com' receivers = ['receiver@receiver.com'] message = """from: person <sender@sender.com> to: person <receiver@receiver.com> subject: blending baby ! test e-mail message. """ try: smtpobj = smtplib.smtp('localhost') smtpobj.sendmail(sender, receivers, message) print "successfully sent email" except smtpexception: print "error: unable send email"
its copy/paste somewhere , works fine.
however . . . once included overall program, email receive not have sender or receiver available ???
its blank.... same code.
import paramiko import time import smtplib def disable_paging(remote_conn): '''disable paging on cisco router''' remote_conn.send("terminal length 0\n") time.sleep(1) # clear buffer on screen output = remote_conn.recv(1000) return output def main(): # variables need changed ip = '1.2.3.4' username = 'xxx' password = 'xxx' # create instance of sshclient object remote_conn_pre = paramiko.sshclient() # automatically add untrusted hosts (make sure okay security policy in environment) remote_conn_pre.set_missing_host_key_policy(paramiko.autoaddpolicy()) # initiate ssh connection remote_conn_pre.connect(ip, username=username, password=password, look_for_keys=false, allow_agent=false) # use invoke_shell establish 'interactive session' remote_conn = remote_conn_pre.invoke_shell() # strip initial router prompt output = remote_conn.recv(1000) # turn off paging disable_paging(remote_conn) # let's try send router command remote_conn.send("\n") remote_conn.send("show log last 50\n") # wait command complete time.sleep(2) output = remote_conn.recv(10000) if 'bad.thing' in output: email_sender() def email_sender(): sender = 'sender@sender.com' receivers = ['receiver@receiver.com'] message = """from: person <sender@sender.com> to: person <receiver@receiver.com> subject: blending baby ! test e-mail message. """ try: smtpobj = smtplib.smtp('localhost') smtpobj.sendmail(sender, receivers, message) print "successfully sent email" except smtpexception: print "error: unable send email" main()
i'm puzzled, please excuse indentation may wrong, done purpose of post.
if indentation of code pasted, please indent code correctly , check if works:
def email_sender(): sender = 'sender@sender.com' receivers = ['receiver@receiver.com'] message = """from: person <sender@sender.com> to: person <receiver@receiver.com> subject: blending baby ! test e-mail message. """ try: smtpobj = smtplib.smtp('localhost') smtpobj.sendmail(sender, receivers, message) print "successfully sent email" except smtpexception: print "error: unable send email"
Comments
Post a Comment