Sending Email in Python using smtplib Module With Examples

Python smtplib module can be used to send emails in the Python program. It’s a very common requirement in software applications and smtplib provides SMTP protocol client to send emails.

1. Sending Email in Python

Let’s develop a program to send email in python.

  • We will use a template file as well which we will show in the next section and which we will use while we’re sending the emails.
  • We will also pick the name and email of the person we want to send the email to, from a text file we make

That sounds better than just a simple task of sending email to static emails. Let’s get started.

1.1) Defining File containing emails

We will start defining a simple file which will contain the names and emails of people we want to send the email to. Let’s look at the format of the file we use:

This file just contains the name of the person in lowercase characters followed by the email of the person. We use lowercase characters in the name as we will leave it to Python’s capabilities to convert it to proper capital words.

We will call the above file as contacts.txt.

1.2) Defining the Template

When we send an email to users, we usually want to personalize the email with their name so that they feel specifically asked for. We can achieve this by using a template where we can embed the user’s name so that each user receives an email with their name embedded in it.

Let’s look at the template we are going to use for the program:

Notice the template string ${USER_NAME}. This string will be replaced with the name which is contained in the text file we last created.

We will call the above file as message.txt.

1.3) Parsing Emails from File

We can parse the text file by opening it in the r mode and then iterating through each line of the file:

With this Python function, we Return two lists names, emails which contains names and emails of users from the file we pass to it. These will be used in the email template message body.

1.4) Getting the Template Object

It is time we get the template object in which we make use of the template file we created by opening it in the r mode and parsing it:

With this function, we get a Template object which comprises of the contents of the file we specified by filename.

2. How sending emails work?

Till now, we’re ready with the data we want to send in the email and the receiver’s emails. Here, let us look at the steps we need to complete to be ready to send the emails:

  • Setup the SMTP Connection and Account credentials for login
  • A message object MIMEMultipart needs to constructed with corresponding headers for From, To, and Subject fields.
  • Prepare and adding the message body
  • Sending the message using SMTP Object

Let’s perform each of these steps here.

3. Defining Connection Details

To define the SMTO Server connection details, we will make a main() function in which we define our HostLet’s look at a code snippet:

In above main() function, we first received the user names and emails followed by which we constructed the SMTP Server Object. The host and port depends on the service provider you use to send emails. For example, in case of Gmail, we will have:

Now, we’re finally ready to send the email.

4. Sending the email from Python Program

Here is a sample program:

We can now see the email arriving at the addresses we defined in the file. To close up, let’s look at the complete code we used for sending emails:

Note that you will have to replace the host and port property for the email provider you use. For Gmail, I made use of these properties:

When we run this script, we just print the text we send:

python-sending-email

 

Python Sending Emails

Next, in case of Gmail, you might have to turn off the security related to your account. When you configure your email and password and run this script first time, you might receive an email from Gmail like:

python-sending-email

 

Gmail Security Error

Just follow the instructions given in the email and run the script again and you will see that an email has arrived in the email box you configured in the contacts file like:

Email received from Python Script

 

Email received from Python Script

Reference: Python smtplib Official Docs

By admin

Leave a Reply

%d bloggers like this: