使用 Python 可靠地强制返回路径

发布于 2024-10-05 07:12:43 字数 2157 浏览 0 评论 0原文

我是一名 Python 初级学生,所以请记住这一点。在 Python 脚本中,我需要设置与发件人地址不同的返回路径地址。 (我使用 Gmail 作为 SMTP 服务器。)

我对这个问题进行了大量搜索,找到了大量“答案”,但没有解决方案。我尝试了此链接 Setting Return-Path with Python sendmail for一条 MIME 消息,但它对我来说根本不起作用。我可以更改电子邮件收件人看到的“收件人:”地址,但是当他们单击“回复”时,它会再次返回到发送电子邮件地址。

这是我正在尝试编写的函数。它工作得很好,只是我需要强制使用不同的返回路径。

#!/usr/bin/python
import smtplib
import os

from email.MIMEMultipart import MIMEMultipart  
from email.MIMEBase import MIMEBase  
from email.MIMEText import MIMEText  
from email.Utils import COMMASPACE, formatdate  
from email import Encoders  
import sap_mailserverdata as sf 

def send_mail(sent_to, subject, body_text, sent_from_addr='', sent_from_name='', files=[], cc=[], bcc=[]):  
    """Send emails with or without attachments."""  
    assert type(sent_to)==list    
    assert type(files)==list  
    assert type(cc)==list  
    assert type(bcc)==list  

    message = MIMEMultipart()
    message['From'] = sent_from_addr
    message['To'] = COMMASPACE.join(sent_to)
    message['Date'] = formatdate(localtime=True)
    message['Subject'] = subject
    message['Cc'] = COMMASPACE.join(cc)
    message.preamble = 'You need a MIME enabled mail reader to see this message.\n'

    message.attach(MIMEText(body_text, 'html'))

    for f in files:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(f, 'rb').read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
        message.attach(part)

    addresses = []
    for x in sent_to:
        addresses.append(x)
    for x in cc:
        addresses.append(x)
    for x in bcc:
        addresses.append(x)

    mail_server = smtplib.SMTP(sf.server, sf.server_port)
    mail_server.ehlo()
    mail_server.set_debuglevel(1)
    mail_server.starttls()
    mail_server.login(sf.username, sf.password)
    mail_server.sendmail(sent_from_addr, addresses, message.as_string())
    mail_server.quit()

为了能够可靠地指定不同的回复返回路径,这个函数缺少什么?

I'm a Python junior, so keep that in mind. In a Python script, I need to set a Return-Path address that is different than the sender's address. (I'm using Gmail as SMTP server.)

I've done lots of searching on this question and found plenty of "answers", but no solutions. I tried this link Setting Return-Path with Python sendmail for a MIME message but it's not working for me at all. I can change the "To:" address that the email recipient sees, but when they click "Reply", it's back to the sending email address again.

This is the function that I'm trying to write. It works well enough, except that I need to force a different Return-Path.

#!/usr/bin/python
import smtplib
import os

from email.MIMEMultipart import MIMEMultipart  
from email.MIMEBase import MIMEBase  
from email.MIMEText import MIMEText  
from email.Utils import COMMASPACE, formatdate  
from email import Encoders  
import sap_mailserverdata as sf 

def send_mail(sent_to, subject, body_text, sent_from_addr='', sent_from_name='', files=[], cc=[], bcc=[]):  
    """Send emails with or without attachments."""  
    assert type(sent_to)==list    
    assert type(files)==list  
    assert type(cc)==list  
    assert type(bcc)==list  

    message = MIMEMultipart()
    message['From'] = sent_from_addr
    message['To'] = COMMASPACE.join(sent_to)
    message['Date'] = formatdate(localtime=True)
    message['Subject'] = subject
    message['Cc'] = COMMASPACE.join(cc)
    message.preamble = 'You need a MIME enabled mail reader to see this message.\n'

    message.attach(MIMEText(body_text, 'html'))

    for f in files:
        part = MIMEBase('application', 'octet-stream')
        part.set_payload(open(f, 'rb').read())
        Encoders.encode_base64(part)
        part.add_header('Content-Disposition', 'attachment; filename="%s"' % os.path.basename(f))
        message.attach(part)

    addresses = []
    for x in sent_to:
        addresses.append(x)
    for x in cc:
        addresses.append(x)
    for x in bcc:
        addresses.append(x)

    mail_server = smtplib.SMTP(sf.server, sf.server_port)
    mail_server.ehlo()
    mail_server.set_debuglevel(1)
    mail_server.starttls()
    mail_server.login(sf.username, sf.password)
    mail_server.sendmail(sent_from_addr, addresses, message.as_string())
    mail_server.quit()

What am I missing with this function to be able to reliably specify a different replyto Return-Path?

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

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

发布评论

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

评论(1

格子衫的從容 2024-10-12 07:12:43

回复和返回路径是两个不同的野兽。请参阅 RFC

您可以将回复设置为:

msg['reply-to'] = '[email protected]'

返回路径由 MTA 设置为接收退回邮件的地址。它由服务器管理员控制,因此除非您在 Google 工作,否则我认为这不在您的控制之下。

大多数时候是在“回复”之后;如果您确实需要更改返回路径,则必须使用您控制下的 SMTP 服务器,并通过 google 搜索如何针对您正在使用的特定 MTA 执行此操作 - 许多服务器都有一个可以覆盖返回的用户和​​/或主机白名单小路。

Reply-to and return path are two distinct beasts. See the RFC.

You can set Reply-to with:

msg['reply-to'] = '[email protected]'

The return-path is set by the MTA to the address that receives bounces. It is controlled by the server administrator, so unless you work for Google I don't think this is under your control.

Most of the time one is after "Reply-to"; if you really need to change the return path you must use a SMTP server under your control and google for how to do this for the specific MTA you are using - many will have a white list of users and/or hosts that can override the return path.

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