mrassili
mrassili

Reputation: 371

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

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   = '[email protected]'
addr_from = '[email protected]'
 
# Define SMTP email server details
smtp_server = 'smtp.aol.com'
smtp_user   = '[email protected]'
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

Upvotes: 25

Views: 28236

Answers (6)

Danila Vershinin
Danila Vershinin

Reputation: 9855

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

from email.utils import formataddr
from email.message import EmailMessage
import smtplib

msg = EmailMessage()
msg['From'] = formataddr(('Example Sender Name', '[email protected]'))
msg['To'] = formataddr(('Example Recipient Name', '[email protected]'))
msg.set_content('Lorem Ipsum')

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

Upvotes: 49

Ohad Cohen
Ohad Cohen

Reputation: 81

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

mail_body = "the email body"
mailing_list = ["[email protected]"]
msg = MIMEText(mail_body)

me = 'John Cena <[email protected]>'
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()

Upvotes: 8

dermasmid
dermasmid

Reputation: 514

I found that if I send an email with gmail and set the From header to sender name <[email protected]>, the email arrives with the From like:

From sender name [email protected] [email protected].

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

msg['From'] = "sender name"

Upvotes: -1

noamyg
noamyg

Reputation: 3104

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')), '[email protected]'))

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 <[email protected]>:

While the email body would include the full pattern:

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

Upvotes: 12

Pradeep Bilaiya
Pradeep Bilaiya

Reputation: 9

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 = '[email protected]'
receivers = ['[email protected]']

message = """From: sender_name <[email protected]>
To: reciever_name <[email protected]>
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

Upvotes: -2

meisen99
meisen99

Reputation: 576

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

Basic example:

msg['From'] = str(Header('Magnus Eisengrim <[email protected]>'))

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

Upvotes: 14

Related Questions