2016-09-07 5 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

Смежные вопросы