我正在尝试使用以下代码发送电子邮件。
import smtplib
from email.mime.text import MIMEText
sender = 'sender@sender.com'
def mail_me(cont, receiver):
msg = MIMEText(cont, 'html')
recipients = ",".join(receiver)
msg['Subject'] = 'Test-email'
msg['From'] = "XYZ ABC"
msg['To'] = recipients
# Send the message via our own SMTP server.
try:
s = smtplib.SMTP('localhost')
s.sendmail(sender, receiver, msg.as_string())
print "Successfully sent email"
except SMTPException:
print "Error: unable to send email"
finally:
s.quit()
cont = """\
<html>
<head></head>
<body>
<p>Hi!<br>
How are you?<br>
Here is the <a href="http://www.google.com">link</a> you wanted.
</p>
</body>
</html>
"""
mail_me(cont,['xyz@xyzcom'])
我希望在收到电子邮件时将“XYZ ABC”显示为发件人姓名,并将其电子邮件地址显示为“sender@sender.com”。但是当我收到电子邮件时,我在电子邮件的“发件人”字段中收到了奇怪的详细信息。
[![from: XYZ@<machine-hostname-appearing-here>
reply-to: XYZ@<machine-hostname-appearing-here>,
ABC@<machine-hostname-appearing-here>][1]][1]
我附上了我收到的电子邮件的屏幕截图。
我该如何根据我的需要解决这个问题。
原文由 cool77 发布,翻译遵循 CC BY-SA 4.0 许可协议
这应该有效:
示例如下: