Setting Return-Path with Python sendmail for a MIME message

左心房为你撑大大i 提交于 2019-12-04 03:54:40

问题


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", "something@something.com")
#...

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.


回答1:


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: michael@mydomain.com',
    'From: michael@mydomain.com',
    'Subject: test email',
    '',
    'Just testing'
]))
smtp = smtplib.SMTP()
smtp.connect()
smtp.sendmail('something@something.com', 'michael@mydomain.com', msg.as_string())


来源:https://stackoverflow.com/questions/3337055/setting-return-path-with-python-sendmail-for-a-mime-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!