Bulk Email from Python Project

In this post, I’ll go over how to use Python to send emails to several recipients automatically. I had to write this article because I needed to use the same template to send personalised emails to several recipients. Assume you wish to send individualised emails to several recipients; that is, you want to send an email using an email client to several recipients, with the intention that no recipient sees the others. In order to accomplish this, install a mail merge-capable plugin for your email client or use mail merge built into word processing software. Refer to the instructions [1] that demonstrate how to use mail merge in Microsoft Word for additional information about mail merge. In the long run, though, mail merge is not as dependable and flexible of an option. We must therefore write a script to automate this process.

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.

I’ll go over each line of code’s function here. Let me first go over the Python code for sending an email, starting with the import statements. The first two lines import the necessary libraries, which allow us to send emails over secure socket layer (SSL) and send emails using smtplib.

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 file's content is formatted so that, as seen below, a single line includes a person's first name, last name, and email. In my case, the tab character ('\t') is used to separate the first name, last name, and email. However, it can be changed to use a comma to separate them. To divide the three columns, the code must also take this into account.

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)

This is the portion of the code that actually sends the recipients of our email. We loop over each line in the content we read from the addresses file because we want to send bulk emails. Using the delimiter we used in the file, the following line of code divides each line into the first name, last name, and address. In this case, I used the tab character "\t."

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

We supply the sendmail() function with the sender email, recipient email, and message after the message details have been assigned to the appropriate variables from the previous line.

mailer.sendmail(sender_email, receiver_email, message)

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

Sending problems using Yahoo Mail You might encounter the error “python:smtplib.SMTPServerDisconnected: Connection unexpectedly closed” when sending emails using Yahoo mail (or an email server that Yahoo manages, like AOL). This error is not entirely evident. The Yahoo server rejecting the connection [2] is the root cause of the error “Too many bad auth attempts error when trying to send email,” which can be found out in detail by enabling server.set debuglevel(1). Yahoo has implemented an option that by default prevents third-party products from accessing the email servers [3]. Allowing apps that use less secure sign-in for Yahoo mail [4] and AOL [5] will resolve the “Too many bad auth attempts error when trying to send email” problem.

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.

DOWNLOAD ASSETS ]

GET INSTANT HELP ]

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Read More