使用 Python (smtplib) 发送邮件时指定发件人 [英] Specify a sender when sending mail with Python (smtplib)
本文介绍了使用 Python (smtplib) 发送邮件时指定发件人的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一段非常简单的代码(仅用于测试):
I have a very simple piece of code (just for testing):
import smtplib
import time
server = 'smtp.myprovider.com'
recipients = ['johndoe@somedomain.com']
sender = 'me@mydomain.com'
message = 'Subject: [PGS]: Results
BlaBlaBla'
session = smtplib.SMTP(server)
session.sendmail(sender,recipients,message);
这可行,但问题是电子邮件客户端不显示发件人.我希望能够在电子邮件中添加发件人姓名.建议?
This works but the problem is that e-mail clients don't display a sender. I want to be able to add a sender name to the e-mail. Suggestions?
推荐答案
smtplib
不会自动包含 From:
标头,因此您必须自己放入:
smtplib
doesn't automatically include a From:
header, so you have to put one in yourself:
message = 'From: me@example.com
Subject: [PGS]: Results
BlaBlaBla'
(实际上,smtplib
不会自动包含任何 标头,而只是将您提供的文本作为原始消息发送)
(In fact, smtplib
doesn't include any headers automatically, but just sends the text that you give it as a raw message)
这篇关于使用 Python (smtplib) 发送邮件时指定发件人的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文