Sending Bulk Email from Python

Sisay A. Chala
4 min readJul 12, 2020
Photo by Paweł Czerwiński on Unsplash

In this article, I will describe a method to automate sending email to multiple recipients using python. My motivation to write this article is the need to send personalized email to multiple recipients using the same template. Suppose you want to send personalized emails to multiple recipients, i.e., you would like to send an email with email client to multiple recipients where each recipient shall not see the other recipients. To do this, you need to either use mail merge from word processing software or install a plugin to your email client with capacity of mail merge. For more information about mail merge, follow the instructions [1] that show how to use mail merge in Microsoft Word. However, mail merge is not as flexible and reliable option in the long run. That is why we need to write a script that will automate this task.

To get started, all you need is some knowledge of python, text file containing the message, and another text file containing list of recipients with their email address. The complete code is available on GitHub.

Here I will describe what each line of code performs. First let me explain the python code to send email starting from import statements. The first two lines import necessary libraries, i.e., smtplib enable us to send email while ssl enables us to send email over secure socket layer.

import smtplib
import ssl

The next two lines define the smtp server (and its port) that we use to send our email.

server = "smtp.mail.yahoo.com" 
port = 587

The following two lines define the sender email and its password that will be used for authentication by the smtp server.

sender_email = "sender@yahoo.com"
password = "password here"

The next line reads recipient addresses from a text file containing list of people to whom we want to send the email.

addresses = open('addresses.txt', 'r').readlines()

The content of the file is formatted in such a way that one line contains first name, last name and email of a single person as shown below. The first name, last name and email are separated using tab (‘\t’) in my case. But one can modify it to separate them with comma. This needs to be reflected in the code as well to split the three columns.

OneFirst OneLast one@yahoo.com
TwoFirst TwoLast two@gmail.com
ThreeFirst ThreeLast three@hotmail.com

The next line creates ssl context using the default settings chosen by the ssl module. This helps us get a higher security level than when calling the SSLContext constructor directly.

context = ssl.create_default_context()

Then comes the line that sets debug level. This line is actually not needed for sending email per se. However, if things go south, we could get more debug information that may help us fix the issues.

mailer.set_debuglevel(1)

Next is to start tls with the given context followed by authentication of the user.

mailer.starttls(context=context)
mailer.login(sender_email, password)

Now comes the part of the code that actually sends our email to the recipients. Because we want to send bulk email, we iterate through all the lines in the content we read from the addresses file. So, the following line of code splits every line into first name, last name and address using the delimiter we used in the file. Here I used a tab character ‘\t’.

first_name, last_name, address = line.split('\t')

Once the message details are assigned to the respective variables from the above line, we feed the sendmail() function with the sender email, receiver email and the message.

mailer.sendmail(sender_email, receiver_email, message)

Finally, let us try to discuss some issues that may arise during execution of the script.

Issues with sending from Yahoo mail
When using Yahoo mail (or an email server managed by Yahoo, such as AOL) as a sender, you may face an error that the connection is unexpectly closed, i.e., “python:smtplib.SMTPServerDisconnected: Connection unexpectedly closed”. This error is not so clear. Therefore, enabling server.set_debuglevel(1), you may find out that the detail of the error that contains “Too many bad auth attempts error when trying to send email”, the root cause being the yahoo server rejecting the connection [2] because Yahoo has implemented an option that by default, does not let 3rd party products access the email servers [3].

The solution to this issue is to fix “Too many bad auth attempts error when trying to send email” by enabling the option to allow apps that use less secure sign in for Yahoo mail [4] and for AOL [5].

In the next part, I will show the part of the code that needs to be added to enable our email script to attach files. Stay tuned.

References

[1] https://support.microsoft.com/en-us/office/use-mail-merge-to-send-bulk-email-messages-0f123521-20ce-4aa8-8b62-ac211dedefa4
[2] https://stackoverflow.com/questions/54266393/pythonsmtplib-smtpserverdisconnected-connection-unexpectedly-closed
[3] https://support.cdesoftware.com/kb/a1909/535-5_7_0-auth005-too-many-bad-auth-attempts-error-when-trying-to-send-email_.aspx
[4] https://login.yahoo.com/account/security
[5] https://login.aol.com/account/security

--

--