使用 Python 发送电子邮件时消息中的 MIME 标头

发布于 2024-10-25 02:16:19 字数 834 浏览 3 评论 0原文

因此,我尝试使用此模板并使用日志文件作为正文发送电子邮件,电子邮件发送正常。但是,它在消息正文中有一个非常丑陋的标头(如下所示)

From nobody Thu Mar 17 14:13:14 2011
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

是否有办法使消息不包含上面的标头?谢谢你!

#!/usr/bin/python
import smtplib
import time
import datetime
from email.mime.text import MIMEText
today = datetime.date.today()
textfile = "/home/user/Public/stereo-restart-log"
FROM = "my-username"
TO = ["recipients"]

SUBJECT = "Stereo log: %s" % today
fp = open(textfile, 'rb')
TEXT = MIMEText(fp.read())
fp.close()
message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, TO, SUBJECT, TEXT)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('my-username','mypass')
server.sendmail(FROM, TO, message)
server.close()

So I am trying to send out an email using this template and using a log file as the body, the email gets sent fine. However, it has this really ugly header in the body of the message (As seen below)

From nobody Thu Mar 17 14:13:14 2011
Content-Type: text/plain; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

Is there anyway to make it so the message does not include the header above? Thank you!

#!/usr/bin/python
import smtplib
import time
import datetime
from email.mime.text import MIMEText
today = datetime.date.today()
textfile = "/home/user/Public/stereo-restart-log"
FROM = "my-username"
TO = ["recipients"]

SUBJECT = "Stereo log: %s" % today
fp = open(textfile, 'rb')
TEXT = MIMEText(fp.read())
fp.close()
message = """\
From: %s
To: %s
Subject: %s

%s
""" % (FROM, TO, SUBJECT, TEXT)

server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('my-username','mypass')
server.sendmail(FROM, TO, message)
server.close()

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

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

发布评论

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

评论(1

故笙诉离歌 2024-11-01 02:16:19

使用 MIMEText,您已经创建了消息对象。您只需向其中添加正确的标头即可:

FROM = "my-username"
TO = ["recipients"]
SUBJECT = "Stereo log: %s" % today
fp = open(textfile, 'rb')
TEXT = MIMEText(fp.read())
fp.close()
TEXT['From'] = FROM
TEXT['To'] = ",".join(TO)
TEXT['Subject'] = SUBJECT
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('my-username','mypass')
server.sendmail(FROM, TO, TEXT.as_string)
server.close()

请注意,在添加为标头之前,您必须将 TO 列表转换为字符串,因为“To/From”标头中不允许使用方括号。
希望这有帮助。

With MIMEText you have already created the message object. You just need to add the proper headers to it:

FROM = "my-username"
TO = ["recipients"]
SUBJECT = "Stereo log: %s" % today
fp = open(textfile, 'rb')
TEXT = MIMEText(fp.read())
fp.close()
TEXT['From'] = FROM
TEXT['To'] = ",".join(TO)
TEXT['Subject'] = SUBJECT
server = smtplib.SMTP('smtp.gmail.com', 587)
server.starttls()
server.login('my-username','mypass')
server.sendmail(FROM, TO, TEXT.as_string)
server.close()

Note that you can must convert the TO list to string before adding as header, because the square brackets are not allowed in the To/From headers.
Hope this helps.

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