如何在python电子邮件脚本中的发件人地址之前添加发件人名称 [英] How to add sender name before sender address in python email script

查看:44
本文介绍了如何在python电子邮件脚本中的发件人地址之前添加发件人名称的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码

#导入smtplib以提供电子邮件功能导入smtplib#导入电子邮件模块从email.mime.multipart导入MIMEMultipart从email.mime.text导入MIMEText#定义要使用的电子邮件地址addr_to ='user@outlook.com'addr_from ='user@aol.com'#定义SMTP电子邮件服务器详细信息smtp_server ='smtp.aol.com'smtp_user ='user@aol.com'smtp_pass ='通过'#构建电子邮件msg = MIMEMultipart('alternative')msg ['To'] = addr_tomsg ['From'] = addr_frommsg ['Subject'] ='测试测试!#创建消息的正文(纯文本和HTML版本).text =这是一条测试消息.\ n文本和html."html =""\"#记录两个部分的MIME类型-text/plain和text/html.第1部分= MIMEText(text,'plain')第2部分= MIMEText(html,'html')#将零件附加到消息容器中.#根据RFC 2046,在这种情况下,是多部分消息的最后一部分#HTML消息是最好的,也是首选.msg.attach(part1)msg.attach(part2)#通过SMTP服务器发送邮件s = smtplib.SMTP(smtp_server)s.login(smtp_user,smtp_pass)s.sendmail(addr_from,addr_to,msg.as_string())s.quit()

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

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

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

Here's my code

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

I just want the email received to display the sender name before sender email address like this : sender_name

解决方案

This is an old question - however, I faced the same problem and came up with the following:

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

You'll need to import from email.header import Header and from email.utils import formataddr.

That would make only the sender name appear on the inbox, without the <xxxxx@gmail.com>:

While the email body would include the full pattern:

Putting the sender name and the email in one string (Sender Name <sender@server.com>) would make some email clients show the it accordingly on the receiver's inbox (unlike the first picture, showing only the name).

这篇关于如何在python电子邮件脚本中的发件人地址之前添加发件人名称的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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