使用 Python sendmail 设置 MIME 消息的返回路径

发布于 2024-09-11 10:29:25 字数 531 浏览 4 评论 0原文

您好,我想为我使用 Python 发送的 MIME 消息设置“Return-Path”标头。 基本上,我尝试了这样的操作:

message = MIMEMultipart()
message.add_header("Return-Path", "[email protected]")
#...

smtplib.SMTP().sendmail(from, to, message.as_string())

我收到的消息的“Return-Path”标头设置为与“From”标头相同的内容,即使我明确添加“Return-Path”标头也是如此。

如何在 Python 中为通过 smtplib 的 sendmail 发送的 MIME 消息设置“Return-Path”标头?

提前致谢。

Hi would like to set the "Return-Path" header for a MIME message I send with Python.
Basically, I tried something like this :

message = MIMEMultipart()
message.add_header("Return-Path", "[email protected]")
#...

smtplib.SMTP().sendmail(from, to, message.as_string())

The message I receive have its "Return-Path" header set to the same content as the "From" one, even if I explicitly add "Return-Path" header.

How can I set "Return-Path" header for a MIME message sent through smtplib's sendmail in Python ?

Thanks in advance.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

一口甜 2024-09-18 10:29:25

Return-Path 由 SMTP 协议设置,它不是从邮件本身派生的。大多数设置都是信封发件人地址。

完成此操作的正确方法是:

msg = email.message_from_string('\n'.join([
    'To: [email protected]',
    'From: [email protected]',
    'Subject: test email',
    '',
    'Just testing'
]))
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail('[email protected]', '[email protected]', msg.as_string())

Return-Path is set by the SMTP protocol, it's not derived from the message itself. It'll be the Envelope From address is most setups.

The proper way to accomplish this is:

msg = email.message_from_string('\n'.join([
    'To: [email protected]',
    'From: [email protected]',
    'Subject: test email',
    '',
    'Just testing'
]))
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail('[email protected]', '[email protected]', msg.as_string())
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文