使用 python 发送邮件 - 如何设置与“From”不同的 Return-Path地址
我想做的是使用 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:<[email protected]> size=352\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: u'rcpt TO:<[email protected]>\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: [email protected]\r\nSubject: Testing bounces\r\nSender: [email protected]\r\nReturn-Path: [email protected]\r\nReply-To: [email protected]\r\nMessage-Id: 377b8dcdf661810d3dc73a4a01fe23b3\r\nFrom: [email protected]\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
所以我可以看到,返回路径确实在标头中。
在寻找答案时我发现了这个: 使用 Python sendmail 设置 MIME 消息的返回路径< /a>
答案中的最后一条评论是: 错误/退回/等等。应发送至信封发件人,而不是“发件人:”中的地址。
在查找信封发件人以及如何指定时,我发现了这篇 wiki 文章: http://en.wikipedia.org/wiki/Bounce_address
这列出了另一个可能的标头参数,例如: 返回路径、反向路径、信封发件人、信封发件人、MAIL FROM、2821-FROM、返回地址、From_、Errors-to。
我将所有这些放入邮件标题中,然后 - 瞧 - 它起作用了。
我现在的问题是 - 我真的必须总是在邮件标头中列出这么多可能的返回路径选项吗?邮件服务器现在是否已配置为接受其中一两个参数?有没有更好的方法来确保邮件在退回时返回到我的邮件服务器?
艾伦.
What im trying to do is to set up smail mail server with lamson (lamsonproject.org). Its working very well, but i have problem with setting return path and for that reason, bounces are going to 'From' address and therefore not reaching my mail server.
Lamson uses pythons smtplib to send mails out.
The "talk" between my project and my smtp server goes like that:
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:<[email protected]> size=352\r\n'
reply: '250 Ok\r\n'
reply: retcode (250); Msg: Ok
send: u'rcpt TO:<[email protected]>\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: [email protected]\r\nSubject: Testing bounces\r\nSender: [email protected]\r\nReturn-Path: [email protected]\r\nReply-To: [email protected]\r\nMessage-Id: 377b8dcdf661810d3dc73a4a01fe23b3\r\nFrom: [email protected]\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
so i can see, that the Return-Path is indeed in headers.
While looking for an answer i found this :
Setting Return-Path with Python sendmail for a MIME message
Last comment in the answer is:
Errors/bounces/etc. should go to the Envelope sender, not the address in From:.
When looking up Envelope sender and how to specify, i came across this wiki article:
http://en.wikipedia.org/wiki/Bounce_address
This lists another possible header parameters like:
return path, reverse path, envelope from, envelope sender, MAIL FROM, 2821-FROM, return address, From_, Errors-to.
I put all those into mail headers and - Voila - It worked.
My question is now - Do i really have to ALWAYS list so many possible options for return path in mail headers? Have the mail servers been configured to accept one or two of those parameters now? Are there better ways to make sure, that mail is returned to my mail server, when it bounces?
Alan.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您要发送邮件,则无需设置任何这些标头;信封发件人的正确位置是在
MAIL FROM:
命令中,因此在您的情况下:由于此命令是 SMTP 协议的一部分而不是邮件本身,因此收件人 邮件服务器添加一个 Return-Path 标头,以便在原始 SMTP 信封不可用的情况下有利于过滤器或其他下游处理。请参阅 RFC2821:
If you are sending mail then you don't need to set any of those headers; the correct place for the envelope sender is in the
MAIL FROM:
command, so in your case:Since this command is part of the SMTP protocol rather than the message itself, the recipient mail server adds a Return-Path header for the benefit of filters or other downstream processing where the original SMTP envelope is unavailable. See RFC2821:
您在
sendmail(from_addr, to_addrs, msg, mail_options=[], rcpt_options=[])
函数中使用的from_addr
将被设置为返回路径
。您使用该函数发送的
msg
可以包含不同的From:
标头,这是人们打开电子邮件时会看到的正常发件人地址。您可以在 smtplib 文档中看到这一点。
引用那里的注释:
The
from_addr
you use in thesendmail(from_addr, to_addrs, msg, mail_options=[], rcpt_options=[])
function will be the one the one that get's set as theReturn-Path
.The
msg
you send with the function can contain a differentFrom:
header that's the normal from address people will see when they open the email.You can see this in the smtplib documentation.
Quoting the note there: