2016-09-07 191 views
-1

發送電子郵件,我有以下代碼與蟒蛇

import smtplib 

sender = '[email protected]' 
receivers = ['[email protected]'] 

message = """From: From Person <[email protected]> 
To: To Person <[email protected]> 
Subject: We are blending baby ! 

This is a test e-mail message. 
""" 

try: 
    smtpObj = smtplib.SMTP('localhost') 
    smtpObj.sendmail(sender, receivers, message) 
    print "Successfully sent email" 
except SMTPException: 
    print "Error: unable to send email" 

其複製/粘貼從什麼地方,它工作正常。

但是, 。 。一旦納入我的整體計劃,我收到的電子郵件沒有發件人或收件人?

它只是空白....但它的代碼相同。

import paramiko 
import time 
import smtplib 

def disable_paging(remote_conn): 
    '''Disable paging on a Cisco router''' 

    remote_conn.send("terminal length 0\n") 
    time.sleep(1) 

    # Clear the buffer on the screen 
    output = remote_conn.recv(1000) 

    return output 


def main(): 

    # VARIABLES THAT 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 for security policy in your 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 to establish an 'interactive session' 
    remote_conn = remote_conn_pre.invoke_shell() 

    # Strip the initial router prompt 
    output = remote_conn.recv(1000) 

    # Turn off paging 
    disable_paging(remote_conn) 

    # Now let's try to send the router a command 
    remote_conn.send("\n") 
    remote_conn.send("show log last 50\n") 

    # Wait for the command to complete 
    time.sleep(2) 

    output = remote_conn.recv(10000) 

    if 'bad.thing' in output: 
     email_sender() 





def email_sender(): 

sender = '[email protected]' 
receivers = ['[email protected]'] 

message = """From: From Person <[email protected]> 
To: To Person <[email protected]> 
Subject: We are blending baby ! 

This is a test e-mail message. 
""" 

try: 
    smtpObj = smtplib.SMTP('localhost') 
    smtpObj.sendmail(sender, receivers, message) 
    print "Successfully sent email" 
except SMTPException: 
    print "Error: unable to send email" 

main() 

我很疑惑,請原諒任何可能錯誤的縮進,這只是爲了這篇文章的目的。

回答

0

如果作爲粘貼你的代碼的縮進,請正確縮進代碼,並檢查是否正常工作:

def email_sender(): 
    sender = '[email protected]' 
    receivers = ['[email protected]'] 

    message = """From: From Person <[email protected]> 
    To: To Person <[email protected]> 
    Subject: We are blending baby ! 

    This is a test e-mail message. 
    """ 

    try: 
     smtpObj = smtplib.SMTP('localhost') 
     smtpObj.sendmail(sender, receivers, message) 
     print "Successfully sent email" 
    except SMTPException: 
     print "Error: unable to send email" 
+0

它不工作,測試,工作正常 – psniffer

+0

很高興我能幫助:) – Ultcyber