在Python email/smtplib中设置不同的回复消息

发布于 2024-11-05 15:25:40 字数 677 浏览 0 评论 0原文

我正在使用 Python email 和 smtplib 从 Python 发送电子邮件。我正在使用我的 Gmail 凭据通过 Gmail SMTP 服务器执行此操作。这工作正常,但我想指定一个与 from 地址不同的 Reply-to 电子邮件地址,以便回复发送到单独的地址(非 Gmail)。

我尝试创建一个 reply to 参数,如下所示:

   msg = MIMEMultipart()

   msg['From'] = "[email protected]"
   msg['To'] = to
   msg['Subject'] = subject
   msg['Reply-to'] = "[email protected]"

但这不起作用。在 Python 文档中找不到任何相关信息。

I am using Python email and smtplib to send an email from Python. I am doing this via the Gmail SMTP server using my Gmail credentials. This works fine, however I would like to specify a Reply-to email address different from the from address, so that replies go to a separate address (non-Gmail.)

I have tried creating a reply to parameter like this:

   msg = MIMEMultipart()

   msg['From'] = "[email protected]"
   msg['To'] = to
   msg['Subject'] = subject
   msg['Reply-to'] = "[email protected]"

But this doesn't work. Can't find any info on this in the Python docs.

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

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

发布评论

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

评论(5

信仰 2024-11-12 15:25:40

这是我的看法。我认为应该明确设置“Reply-To”标头。可能的原因是它不如“主题”、“收件人”和“发件人”等标头常用。

python
Python 2.6.6 (r266:84292, May 10 2011, 11:07:28)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> MAIL_SERVER = 'smtp.domain.example'
>>> TO_ADDRESS = '[email protected]'
>>> FROM_ADDRESS = '[email protected]'
>>> REPLY_TO_ADDRESS = '[email protected]'
>>> import smtplib
>>> import email.mime.multipart
>>> msg = email.mime.multipart.MIMEMultipart()
>>> msg['to'] = TO_ADDRESS
>>> msg['from'] = FROM_ADDRESS
>>> msg['subject'] = 'testing reply-to header'
>>> msg.add_header('reply-to', REPLY_TO_ADDRESS)
>>> server = smtplib.SMTP(MAIL_SERVER)
>>> server.sendmail(msg['from'], [msg['to']], msg.as_string())
{}

Here's my take on it. I believe that the "Reply-To" header should be set explicitly. The likely reason is that it's less commonly used than headers such as "Subject", "To", and "From".

python
Python 2.6.6 (r266:84292, May 10 2011, 11:07:28)
[GCC 4.2.1 (Apple Inc. build 5666) (dot 3)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> MAIL_SERVER = 'smtp.domain.example'
>>> TO_ADDRESS = '[email protected]'
>>> FROM_ADDRESS = '[email protected]'
>>> REPLY_TO_ADDRESS = '[email protected]'
>>> import smtplib
>>> import email.mime.multipart
>>> msg = email.mime.multipart.MIMEMultipart()
>>> msg['to'] = TO_ADDRESS
>>> msg['from'] = FROM_ADDRESS
>>> msg['subject'] = 'testing reply-to header'
>>> msg.add_header('reply-to', REPLY_TO_ADDRESS)
>>> server = smtplib.SMTP(MAIL_SERVER)
>>> server.sendmail(msg['from'], [msg['to']], msg.as_string())
{}
ゃ人海孤独症 2024-11-12 15:25:40

我有同样的问题,我所要做的就是将标题设置为小写,如下所示:

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

I had the same question and all I had to do to make it work was to set the header in lowercase like so:

msg['reply-to'] = "[email protected]"
甜点 2024-11-12 15:25:40

对于 2021 年的 Python3,我建议使用以下内容来构造消息:

from email.message import EmailMessage
from email.utils import formataddr

msg = EmailMessage()
msg['Subject'] = "Message Subject"
msg['From'] = formataddr(("Sender's Name", "[email protected]"))
msg['Reply-To'] = formataddr(("Name of Reply2", "[email protected]"))
msg['To'] = formataddr(("John Smith", "[email protected]"))
msg.set_content("""\
<html>
  <head></head>
  <body>
    <p>A simple test email</p>
  </body>
</html>
""", subtype='html')

然后,为了发送消息,我在邮件服务器上使用以下内容,该邮件服务器在端口 587 上使用 StartTLS:

from smtplib import SMTP
from ssl import create_default_context as context

with SMTP('smtp.domain.example', 587) as server:
    server.starttls(context=context())
    server.login('[email protected]', password)
    server.send_message(msg)

For Python3 in 2021 I would recommend the following to construct the message:

from email.message import EmailMessage
from email.utils import formataddr

msg = EmailMessage()
msg['Subject'] = "Message Subject"
msg['From'] = formataddr(("Sender's Name", "[email protected]"))
msg['Reply-To'] = formataddr(("Name of Reply2", "[email protected]"))
msg['To'] = formataddr(("John Smith", "[email protected]"))
msg.set_content("""\
<html>
  <head></head>
  <body>
    <p>A simple test email</p>
  </body>
</html>
""", subtype='html')

Then to send the message I use the following for my mail server which uses StartTLS on port 587:

from smtplib import SMTP
from ssl import create_default_context as context

with SMTP('smtp.domain.example', 587) as server:
    server.starttls(context=context())
    server.login('[email protected]', password)
    server.send_message(msg)
江心雾 2024-11-12 15:25:40

正如 Jonathon Reinhart 指出的那样,“To”需要大写:

msg['Reply-To'] = "[email protected]"

As Jonathon Reinhart pointed out, the "To" needs to be upper case:

msg['Reply-To'] = "[email protected]"
澉约 2024-11-12 15:25:40

只有这个对我有用:

msg['In-Reply-To'] = "[email protected]"

看这里:使用 python 3.4 回复电子邮件 按城市

Only this worked for me:

msg['In-Reply-To'] = "[email protected]"

Look here: Reply to email using python 3.4 by urban

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