使用Python发送邮件时指定发件人(smtplib) [英] Specify a sender when sending mail with Python (smtplib)

查看:1007
本文介绍了使用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\n\nBlaBlaBla'

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\nSubject: [PGS]: Results\n\nBlaBlaBla'

(实际上, 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆