How to Send Emails in Python

Learn how to use Python's standard library smtplib and email modules to connect to any SMTP server and send emails in Python automatically.
  · 6 min read · Updated jun 2020 · Python Standard Library

Sending emails manually is no doubt a time-consuming and tough task, a programmer can easily automate this using his favorite programming language, in this tutorial, you will learn how you can send emails using smtplib module in Python.

The smtplib module defines an SMTP client session object that can be used to send mail to any Internet machine with an SMTP (or Extended-SMTP) listener. It comes pre-installed with Python, so we don't have to install anything.

If you're not sure what SMTP (Simple Mail Transfer Protocol) is, it is basically a communication protocol for electronic mail transmission, it was first introduced in 1982 and updated in 2008 by RFC 5321 to Extended SMTP additions (which is what's used today). So in a nutshell, mail servers use this network protocol to send mail messages.

Related: How to Read Emails in Python.

Building our Message

Alright, let's get started, let's first construct our email:

import smtplib
from email.mime.text import MIMEText
from email.mime.multipart import MIMEMultipart

We gonna need email module as well, as it provides us with the MIME standard, which will help us send not only ASCII character set emails, but larger character sets, HTML, and other data formats such as audio and video.

Message bodies with MIME formatting consist of multiple parts, each part may contain any type of data that you actually want to send.

Let's define our parameters:

# your credentials
email = "[email protected]"
password = "password"

# the sender's email
FROM = "[email protected]"
# the receiver's email
TO   = "[email protected]"
# the subject of the email (subject)
subject = "Just a subject"

Pretty straightforward, email and password variables are the credentials of the email address you wanna send with, FROM and TO are the sender's email and receiver's email respectively (email and FROM are usually the same), and subject is the title (or subject) of the mail we'll send. Make sure you edit these variables on your own needs, you can send to multiple email addresses by using a list of emails in TO variable instead of a string.

Now let's construct our mail, we gonna use MIMEMultipart object here, so we can attach multiple parts of the mail such as HTML, image and so on.

# initialize the message we wanna send
msg = MIMEMultipart()
# set the sender's email
msg["From"] = FROM
# set the receiver's email
msg["To"] = TO
# set the subject
msg["Subject"] = subject

Note: If you're using Gmail account to send, make sure you turn "Allow less secure apps" to ON, although this will make everyone can access your gmail account just with your email and password.

We also set the From, To email addresses and the subject. Let's build the message body now, I'm gonna use HTML in this example:

# set the body of the email
text = MIMEText("This email is sent using <b>Python</b> !", "html")
# attach this body to the email

After the construction of the message body, we attached it to the MIMEMultipart object we just created. Let's see how this email looks like:



Content-Type: multipart/mixed; boundary="===============3518853130198016098=="
MIME-Version: 1.0
From: [email protected]
To: [email protected]
Subject: Just a subject

Content-Type: text/html; charset="us-ascii"
MIME-Version: 1.0
Content-Transfer-Encoding: 7bit

This email is sent using <b>Python</b> !

As you may notice, each part is separated with a fixed set of characters, the first part contains the mail header and the second contains the actual message body we just attached, note that if there is any non ASCII characters, it'll automatically change the encoding for us, so you usually don't have to worry about that. For more information about the MIME standard, check this Wikipedia page.

You can also attach other file types using email module, for example, if you want to send audio files, you can use MIMEAudio class from the package, please refer to the official Python documentation for detailed information.

Sending the Message

Now that we have the mail ready to be sent, let's connect to the SMTP server using smtplib:

# initialize the SMTP server
server = smtplib.SMTP("", 587)

I've used Gmail SMTP server here, with the port 587. If you want other SMTP servers (such as Outlook or Yahoo), check the list of SMTP servers and their port numbers.

Putting the connection to the SMTP server into TLS mode:

# connect to the SMTP server as TLS mode (secure) and send EHLO

And finally, we login using the account credentials and terminate the session after we send our email:

# login to the account using the credentials
server.login(email, password)
# send the email
server.sendmail(FROM, TO, msg.as_string())
# terminate the SMTP session

sendmail() method performs an entire mail transaction, it takes several arguments: the address sending this mail (FROM), a list of addresses to send this mail to (if it's a string, then it will be treated as a list with 1 address), and the message to send.

After I executed the above code (of course I used real gmail credentials), here is the screenshots of the email received:

Email Received

Opened email after the receive


Awesome, There are many use cases for this ! You can for example, create your own custom alerts to notify you when something happened, or you want to send email confirmation to users when they create an account on your website, or send emails to members of your organization, or sending keylogger results, possibilities are endless !

If you're wondering if you can read emails in Python, we have a tutorial for that as well!

An example use case of this, is that you can use an email extractor to grab a good mail list and send marketing compaigns to reach a broad number of audience !

Let us see what you built with this in the comments below !

Finally, if you're a beginner and want to learn Python, I suggest you take Master Python in 5 Online Courses from University of Michigan, in which you'll learn a lot about Python, good luck!

Learn also: How to Manipulate IP Addresses in Python using ipaddress Module.

Happy Coding ♥

View Full Code
Sharing is caring!

Read Also

Comment panel