使用 python 发送邮件 - 如何设置与“From”不同的 Return-Path地址

发布于 2024-10-01 20:57:32 字数 3255 浏览 0 评论 0原文

我想做的是使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

总攻大人 2024-10-08 20:57:32

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

MAIL FROM:<[email protected]>

由于此命令是 SMTP 协议的一部分而不是邮件本身,因此收件人 邮件服务器添加一个 Return-Path 标头,以便在原始 SMTP 信封不可用的情况下有利于过滤器或其他下游处理。请参阅 RFC2821

当传送 SMTP 服务器对邮件进行“最终传送”时
消息,它在邮件的开头插入一个返回路径行
数据。需要使用返回路径;邮件系统必须支持
它。返回路径行保留了中的信息
<反向路径>来自 MAIL 命令。

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:

MAIL FROM:<[email protected]>

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:

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.

莫相离 2024-10-08 20:57:32

您在 sendmail(from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]) 函数中使用的 from_addr 将被设置为返回路径

您使用该函数发送的 msg 可以包含不同的 From: 标头,这是人们打开电子邮件时会看到的正常发件人地址。

您可以在 smtplib 文档中看到这一点。
引用那里的注释:

from_addr 和 to_addrs 参数用于构造传输代理使用的消息信封。 sendmail 不会以任何方式修改邮件标头。

The from_addr you use in the sendmail(from_addr, to_addrs, msg, mail_options=[], rcpt_options=[]) function will be the one the one that get's set as the Return-Path.

The msg you send with the function can contain a different From: 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:

The from_addr and to_addrs parameters are used to construct the message envelope used by the transport agents. sendmail does not modify the message headers in any way.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文