Sending personalized mass emails with Python

June 24, 2019 0 Comments

Have you ever been in a position where you have to send 100 emails to 100 different people? I recently have. I also had to address the recipients in the email personally: “Dear Anton/Alex/etc.” High time to use Python and automatize my work!

I will walk you through this short adventure, but I may not be able to explain everything extensively in one blog. Instead, I made certain words clickable for you to access more information!

Prepare a .csv document: I prepared a .csv document including the information which needs to be modified for each email. I wanted to change the names of the recipients after the word “Dear”: Dear Anton, Dear Alex, etc. So, I had two columns in the .csv file: The Names, The Email addresses.




Write the content of the email: I wrote the content of my email and saved it in .txt format. The content looks like this:

Dear {},

The rest of the email.

After preparing the .csv document (“email.csv”), and the email content (“message.txt”), I opened up a .py file (Python files have .py extension). For the sake of simplicity, it was essential that all three (.csv, .txt, and .py) files are in the same directory. Then, I needed to install and import two modules from Python: csv and smtp lib. The csv module was necessary to read the .csv files. The smtp lib, [SMTP: “Simple Mail Transfer Protocol“], is used for sending and routing emails between servers. These are the first two lines of the code which you see below. On the fourth line, you will see “MIME” which stands for “Multipurpose Internet Mail Extensions”. With MIME, I was specifying the content type, which in this case is text.

import csv
import smtplib

from email.mime.text import MIMEText

import re

fp = open('message.txt', 'r')
msg = MIMEText(
msg['Subject'] = 'Subject goes here'
msg['From'] = 'your email'

server = smtplib.SMTP('localhost', portnumber)
server.login('your email', 'your password')
email_data = csv.reader(open('email.csv', 'r'))
email_pattern= re.compile("^.+@.+\..+$")
for row in email_data:
    if([1]) ):
        del msg['To']
        msg['To'] = row[1]
    server.sendmail('your email', [row[1]], msg.as_string().format(row[0]))

On the 6th line, you see that I imported regular expression module: “import re.” This module is for the special (e.g., ‘|’, ‘(‘ ), and ordinary characters (e.g., A, a) in the message text.

Then, I defined a variable, fp, for reading the text. On the same line, you will see “r”, meaning: open for reading. After that, I defined a dictionary (msg), and keyvalues (“Subject”, “From”) on the 11th & 12th line. “Subject” will be the email header/title and “From” will show your address.

Before moving to line 14, I needed to find my SMTP information (password, & port number). I use Mac mail app, and the required information was in my mail preferences. After filling in my server information, I defined a variable “email_data” to read the .csv. Moving to the next line, I created the “email_pattern” variable, and I used the re.compile function. (This function compiles regular expression patterns into regular expression objects). Then, you will see a for loop. In that loop, the variable, “email_data”, will take information from the .csv document. The first row (row[0]–counting starts from 0) of the .csv document included the names, and the second row (Row[1]) included the email addresses. So, in that for loop, for each row in the “email.csv” document: It will take the name, and update it in the email, and then it will take the email address, and send the email to that address. Then, we are good to go!:)

Leave a Reply

Your email address will not be published. Required fields are marked *