python - 使用 python 发送邮件 - 如何设置与 'From' 地址不同的返回路径

标签 python smtp email-bounces lamson

我想做的是使用 lamson (lamsonproject.org) 设置 smail 邮件服务器。它工作得很好,但我在设置返回路径时遇到问题,因此,退回邮件将发送到“发件人”地址,因此不会到达我的邮件服务器。

Lamson 使用 pythons smtplib 发送邮件。

我的项目和我的 smtp 服务器之间的“对话”是这样的:

send: 'ehlo xxx.mydomiain.com\r\n'
reply: '250-smtp.smtpserver.com\r\n'
reply: '250-PIPELINING\r\n'
reply: '250-SIZE 20000000\r\n'
reply: '250-VRFY\r\n'
reply: '250-ETRN\r\n'
reply: '250-STARTTLS\r\n'
reply: '250-XVERP\r\n'
reply: '250 8BITMIME\r\n'
reply: retcode (250); Msg: smtp.smtpserver.com
PIPELINING
SIZE 20000000
VRFY
ETRN
STARTTLS
XVERP
8BITMIME
send: u'mail FROM:<me@myotherdomain.com> size=352\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: u'rcpt TO:<pleasebouncethis@myotherdomain.com>\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: 'data\r\n'
reply: '354 End data with <CR><LF>.<CR><LF>\r\n'
reply: retcode (354); Msg: End data with <CR><LF>.<CR><LF>
data: (354, 'End data with <CR><LF>.<CR><LF>')
send: 'MIME-Version: 1.0\r\nTo: pleasebouncethis@myotherdomain.com\r\nSubject: Testing bounces\r\nSender: MAILER-DAEMON@mydomiain.com\r\nReturn-Path: MAILER-DAEMON@mydomain.com\r\nReply-To: MAILER-DAEMON@mydomain.com\r\nMessage-Id: 377b8dcdf661810d3dc73a4a01fe23b3\r\nFrom: me@myotherdomain.com\r\nContent-Type: text/plain; charset="utf-8"\r\nContent-Transfer-Encoding: base64\r\n\r\nc2RmZ2TDpGfDpGRsZsOkZ2xkZmc=\r\n.\r\n'
reply: '250 Ok: queued as E635D157D3\r\n'
reply: retcode (250); Msg: Ok: queued as E635D157D3
data: (250, 'Ok: queued as E635D157D3')
send: 'quit\r\n'
reply: '221 Bye\r\n'
reply: retcode (221); Msg: Bye
Done

所以我可以看到,返回路径确实在 header 中。

在寻找答案时我发现了这个: Setting Return-Path with Python sendmail for a MIME message

答案中的最后一条评论是: 错误/退回/等等。应发送至信封发件人,而不是“发件人:”中的地址。

在查找信封发件人以及如何指定时,我发现了这篇 wiki 文章: http://en.wikipedia.org/wiki/Bounce_address

这列出了另一个可能的 header 参数,例如: 返回路径、反向路径、信封发件人、信封发件人、MAIL FROM、2821-FROM、返回地址、From_、Errors-to。

我将所有这些放入邮件 header 中,然后 - 瞧 - 它起作用了。

我现在的问题是 - 我真的必须始终在邮件 header 中列出这么多可能的返回路径选项吗?邮件服务器现在是否已配置为接受其中一两个参数?是否有更好的方法来确保邮件在退回时返回到我的邮件服务器?

艾伦。

最佳答案

如果您发送邮件,则无需设置任何这些 header ;信封发件人的正确位置位于 MAIL FROM: 命令中,因此在您的情况下:

MAIL FROM:<MAILER-DAEMON@mydomain.com>

由于此命令是 SMTP 协议(protocol)的一部分而不是邮件本身,因此收件人邮件服务器会添加一个 Return-Path header ,以便过滤器或其他下游处理(其中原始 SMTP 信封所在位置)不可用。请参阅RFC2821 :

When the delivery SMTP server makes the "final delivery" of a message, it inserts a return-path line at the beginning of the mail data. This use of return-path is required; mail systems MUST support it. The return-path line preserves the information in the <reverse-path> from the MAIL command.

关于python - 使用 python 发送邮件 - 如何设置与 'From' 地址不同的返回路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4177496/

相关文章:

php - 对 Swiftmailer 的退回支持

php - 如何编写读取退回邮件的 PHP 脚本?

c# - AWS SES - 退回未传送到 ReturnPath 地址

python - 在 Python 中绘制立方晶格

email - SMTP 不包括发送邮件中的发件人地址

c# - 在 C# 中验证电子邮件地址是否存在

JavaMail API 和 Tomcat 7 实现

PythonKit 找不到 Python3 的 PYTHON_LIBRARY

python - mysql + python 2.6 编程的最佳资源

python - matplotlib 颜色线由 "value"