从本地主机发送邮件无法按预期工作

2024-06-07 17:23:23 发布

您现在位置:Python中文网/ 问答频道 /正文

我试图从localhost:1025发送消息。我正在使用此命令运行SMTP调试服务器python -m smtpd -n localhost:1025

以下是用于发送邮件的代码:

msg = mailer.Message(From='noreply@'+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['mail']包含我的电子邮件地址,当我检查我的电子邮件收件箱和垃圾邮件文件夹时,我没有找到任何邮件-据推测,是什么导致了这个问题?


Tags: 命令localhosthost消息电子邮件mailer邮件mail
2条回答

我认为使用smtpd/mailer是个错误。我用这种方法解决了这个问题:

  1. 使用以下方法测试Exim:

    nano testexim

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

    sendmail MYEMAILADDRESS < testexim

    结果:通过exim(sendmail命令)成功发送邮件

  2. 使用以下方法测试smtpd:

    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',
                                         'noreply@localhost'))
    msg['Subject'] = 'SMTPD test message'
    
    server = smtplib.SMTP('localhost', 1025)
    server.set_debuglevel(True)  # show communication with the server
    try:
        server.sendmail('noreply@localhost',
                        [MYEMAILADDRESSS],
                        msg.as_string())
    finally:
        server.quit()
    

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

  3. 尝试了不基于smtpd的方法:

    python

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

    结果:通过编程创建sendmail进程完成了此任务

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

相关问题 更多 >

    热门问题