2017-08-15 75 views
0

我试图从localhost发送消息:1025。我使用这个命令python -m smtpd -n localhost:1025运行一个smtp调试服务器。python:从本地主机发送邮件没有按预期工作

下面是用于发送邮件代码:

msg = mailer.Message(From='[email protected]'+company['host'], 
         To=req['mail'], 
         Subject='E-mail confirmation', 
Body=Template(open('./confirmation.html').read()).render(company=company, account=account, accode=accode)) 
mailer.Mailer(company['host'], port=1025).send(msg) 

的REQ [“邮件”]是包含我的电子邮件地址,当我检查我的电子邮件收件箱和垃圾邮件文件夹,我没有发现任何消息 - 什么应该导致这个问题?

回答

1

我认为使用smtpd /邮件是一个错误。

  1. 测试进出口使用:

    nano testexim

    From: "Exim" <[email protected]> 
    To: MYFULLNAME MYEMAILADDRESS 
    Reply-To: [email protected] 
    Subject: Hello exim! 
    Content-Type: text/plain; charset=utf-8 
    
    this is exim test message 
    
    EOF 
    

    sendmail MYEMAILADDRESS < testexim

    结果:该消息得到了顺利通过进出口发送(sendmail命令)

  2. Tested smtpd using:

    我用这个方法解决了这个问题

    python

    import smtplib 
    Import email.utils 
    from email.mime.text import MIMEText 
    
    # Create the message 
    msg = MIMEText('Hello SMTPD!') 
    msg['To'] = email.utils.formataddr((MYFULLNAME, 
                MYEMAILADDRESS)) 
    msg['From'] = email.utils.formataddr(('SMTPD', 
                '[email protected]')) 
    msg['Subject'] = 'SMTPD test message' 
    
    server = smtplib.SMTP('localhost', 1025) 
    server.set_debuglevel(True) # show communication with the server 
    try: 
        server.sendmail('[email protected]', 
            [MYEMAILADDRESSS], 
            msg.as_string()) 
    finally: 
        server.quit() 
    

    结果:通过smtpd的将消息发送失败

  3. Tried a method not based on smtpd:

    python

    from email.mime.text import MIMEText 
    from subprocess import Popen, PIPE 
    
    msg = MIMEText("Hello from exim") 
    msg["From"] = "[email protected]" 
    msg["To"] = MYEMAILADDRESS 
    msg["Subject"] = "Python sendmail test" 
    p = Popen(["/usr/sbin/sendmail", "-t", "-oi"], stdin=PIPE) 
    p.communicate(msg.as_string()) 
    

    结果:创建一个sendmail进程编程做的工作

2

作为非常明确in the documentation调试服务器不会尝试传递电子邮件。这是为了在不实际发送任何邮件的情况下测试和验证电子邮件的内容。

+0

我错过了,谢谢你回答。但即使在使用'python -m smtpd -n localhost:1025'运行服务器之后,邮件也不会被发送。你有什么主意吗? – Kais

+0

需要告知'PureServer'类需要交付的位置。从'python -m smtpd --help'看来,默认情况下,它将使用本地主机上的端口25作为其尝试传送点。是否有本地SMTP服务器在运行? – holdenweb

+0

我已经安装了exim。但运行'python -m smtpd -n -c PureServer localhost:25'后,我得到“AttributeError:'模块'对象没有属性'PureServer'”。 – Kais