如何在python电子邮件脚本中在发件人地址之前添加发件人名称

如何在python电子邮件脚本中在发件人地址之前添加发件人名称,python,email,smtp,Python,Email,Smtp,这是我的密码 # Import smtplib to provide email functions import smtplib # Import the email modules from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # Define email addresses to use addr_to = 'us

这是我的密码

# Import smtplib to provide email functions import smtplib # Import the email modules from email.mime.multipart import MIMEMultipart from email.mime.text import MIMEText # Define email addresses to use addr_to = 'user@outlook.com' addr_from = 'user@aol.com' # Define SMTP email server details smtp_server = 'smtp.aol.com' smtp_user = 'user@aol.com' smtp_pass = 'pass' # Construct email msg = MIMEMultipart('alternative') msg['To'] = addr_to msg['From'] = addr_from msg['Subject'] = 'test test test!' # Create the body of the message (a plain-text and an HTML version). text = "This is a test message.\nText and html." html = """\ """ # Record the MIME types of both parts - text/plain and text/html. part1 = MIMEText(text, 'plain') part2 = MIMEText(html, 'html') # Attach parts into message container. # According to RFC 2046, the last part of a multipart message, in this case # the HTML message, is best and preferred. msg.attach(part1) msg.attach(part2) # Send the message via an SMTP server s = smtplib.SMTP(smtp_server) s.login(smtp_user,smtp_pass) s.sendmail(addr_from, addr_to, msg.as_string()) s.quit() #导入smtplib以提供电子邮件功能 导入smtplib #导入电子邮件模块 从email.mime.multipart导入MIMEMultipart 从email.mime.text导入MIMEText #定义要使用的电子邮件地址 加上user@outlook.com' 地址来自user@aol.com' #定义SMTP电子邮件服务器详细信息 smtp_服务器='smtp.aol.com' smtp(用户)user@aol.com' smtp_pass='pass' #构建电子邮件 msg=MIMEMultipart('alternative') msg['To']=添加到 msg['From']=addr\u From msg['Subject']=“测试!” #创建消息正文(纯文本和HTML版本)。 text=“这是一条测试消息。\n文本和html。” html=”“”\ """ #记录这两部分的MIME类型-text/plain和text/html。 part1=MIMEText(文本“纯”) part2=MIMEText(html,'html') #将部件附加到消息容器中。 #根据RFC 2046,在本例中,多部分消息的最后一部分 #HTML消息是最好的首选。 附加信息(第1部分) 附加信息(第2部分) #通过SMTP服务器发送邮件 s=smtplib.SMTP(SMTP_服务器) s、 登录(smtp_用户,smtp_通行证) s、 sendmail(addr\u from、addr\u to、msg.as\u string()) s、 退出 我只希望收到的电子邮件在发件人电子邮件地址之前显示发件人名称,如下所示:

这取决于“友好名称”是基本ASCII还是需要特殊字符

基本示例:

msg['From'] = str(Header('Magnus Eisengrim <meisen99@gmail.com>'))
msg['From']=str(标题('Magnus Eisengrim'))

如果您需要使用非US-ASCII字符,则更为复杂,但随附的文章应该会有所帮助,它非常全面:

您可以使用下面提到的代码,您只需使用用户名和密码更改发送者和接收者,就可以了

import smtplib

sender = 'xyz@gmail.com'
receivers = ['abc@gmail.com']

message = """From: sender_name <xyz@gmail.com>
To: reciever_name <abc@gmail.com>
Subject: sample test mail

This is a test e-mail message.
"""

try:
   smtpObj = smtplib.SMTP('smtp_server',port)
   smtpObj.sendmail(sender, receivers, message) 
   smtpObj.login(user,password)        
   print ("Successfully sent email")
except:
   print ("Error: unable to send email")
导入smtplib
发送者xyz@gmail.com'
接收者=['abc@gmail.com']
message=“”发件人:发件人\姓名

收件人:receiver_name

这是一个老问题-然而,我遇到了同样的问题,并提出了以下建议:

msg['From']=formataddr((str(Header('Someone Somewhere','utf-8')),'xxxxx@gmail.com)

您需要从email.header导入
,并从email.utils导入formataddr导入

这将使收件箱中只显示发件人姓名,而不显示

虽然电子邮件正文将包括完整的模式:


将发件人姓名和电子邮件放在一个字符串中(
发件人姓名
)会使一些电子邮件客户端在收件人的收件箱中相应地显示它(与第一张图片不同,只显示姓名).

我发现,如果我用gmail发送一封电子邮件,并将
发件人
标题设置为
发件人姓名
,则电子邮件到达时会带有
发件人
如下:

发件人姓名email@gmail.com email@gmail.com.

因此,我想至少在gmail中,您应该设置
From
标题,如下所示:

msg['From'] = "sender name"

我以内置示例为例,使用以下内容制作:

mail_body = "the email body"
mailing_list = ["user1@company.com"]
msg = MIMEText(mail_body)

me = 'John Cena <mail@company.com>'
you = mailing_list
msg['Subject'] = subject
msg['From'] = me
msg['To'] = mailing_list

# Send the message via our own SMTP server, but don't include the
# envelope header.
s = smtplib.SMTP('localhost')
s.sendmail(me, [you], msg.as_string())
s.quit()
mail\u body=“电子邮件正文”
邮件列表=[”user1@company.com"]
msg=MIMEText(邮件正文)
我=‘约翰·塞纳’
你=邮寄清单
msg['Subject']=主语
msg['From']=我
msg['To']=邮件列表
#通过我们自己的SMTP服务器发送邮件,但不包括
#信封头。
s=smtplib.SMTP('localhost')
s、 sendmail(我,[you],msg.as_string())
s、 退出

在2020年和Python 3中,您可以这样做:

从email.utils导入formataddr
导入smtplib
从email.message导入EmailMessage
msg=EmailMessage()
msg['From']=formataddr(('Example Sender Name','john@example.com'))
msg['To']=formataddr(('Example Recipient Name','jack@example.org'))
msg.set_content('Lorem Ipsum')
使用smtplib.SMTP('localhost')作为s:
s、 发送消息(msg)

这将更改接收方名称!!更新了答案,但相同的逻辑适用于“发件人”标题和“收件人”标题。关于RFC2046的评论非常不正确。接受者选择他们喜欢看的身体部位。如果有什么区别的话,如果用户没有指定首选项,第一部分可能会优先考虑。用硬编码的手工构造的消息替换完美的MIME消息并不是正确的方向。伟大的解决方案!