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

时间:2017-09-08 03:00:02

标签: 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   = '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()

我只是希望收到的电子邮件在发件人电子邮件地址之前显示发件人姓名,如下所示:sender_name

6 个答案:

答案 0 :(得分:3)

在2020年和Python 3中,您将执行以下操作:

[['S01','C01'], ['S01', 'C02'], ['S02', 'CO3'], ['SO4','CO4']]

答案 1 :(得分:2)

我以内置示例为例:

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()

答案 2 :(得分:1)

这取决于&#34;友好名称&#34;是基本的ASCII或需要特殊字符。

基本示例:

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

如果您需要使用非US-ASCII字符,它会更复杂,但附带的文章应该有所帮助,它非常透彻:http://blog.magiksys.net/generate-and-send-mail-with-python-tutorial

答案 3 :(得分:0)

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

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

您需要导入from email.header import Headerfrom email.utils import formataddr

这将仅使发件人姓名出现在收件箱中,而没有<xxxxx@gmail.com>

尽管电子邮件正文将包含完整模式:

将发件人名称和电子邮件放在一个字符串(Sender Name <sender@server.com>)中,会使某些电子邮件客户端在收件人的收件箱中相应地显示该名称(与第一张图片不同,只显示名称)。

答案 4 :(得分:0)

我发现,如果我发送带有gmail的电子邮件并将From标头设置为sender name <email@gmail.com>,则电子邮件将以From的形式到达:

发件人姓名来自发件人姓名email@gmail.com email@gmail.com。

所以我猜至少应该使用gmail来设置From标头,如下所示:

msg['From'] = "sender name"

答案 5 :(得分:-1)

您可以使用下面提到的代码,您只需要使用用户名和密码更改发件人和收件人,它就适合您。

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")

有关详细信息,请访问https://www.datadivein.com/2018/03/how-to-auto-send-mail-using-python.html