How to add sender name before sender address in python email script

两盒软妹~` 提交于 2021-01-03 07:08:20

问题


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


回答1:


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




回答2:


It depends on whether the "friendly name" is basic ASCII or requires special characters.

Basic example:

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

If you need to use non US-ASCII characters, it's more complex, but the attached article should help, it is very thorough: http://blog.magiksys.net/generate-and-send-mail-with-python-tutorial




回答3:


In the year 2020 and Python 3, you do things like this:

from email.utils import formataddr
import smtplib
from email.message import 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')

with smtplib.SMTP('localhost') as s:
    s.send_message(msg)



回答4:


I took the built-in example and made it with this:

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



回答5:


I found that if I send an email with gmail and set the From header to sender name <email@gmail.com>, the email arrives with the From like:

From sender name email@gmail.com email@gmail.com.

So I guess at least with gmail you should set the From header like as follow:

msg['From'] = "sender name"



回答6:


You can use below mentioned code, you just need to change sender and receiver with user name and password, it will work for you.

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

for more detail please visit https://www.datadivein.com/2018/03/how-to-auto-send-mail-using-python.html



来源:https://stackoverflow.com/questions/46107983/how-to-add-sender-name-before-sender-address-in-python-email-script

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!