Chapter 59 – Using Python and Gmail SMTP to Automate Sending Personalised Emails

From a marketing perspective, organic traffic is not just limited to SEO. Conversely, it covers a wide range of traffic sources as long as the majority percentage of investment is focused on manpower. They can be social media, partnership, VoD, email and so on. Compared to these channels in terms of organic traffic, email marketing is still the most efficient approach in one-to-one conversation marketing with a higher new customer conversion rate and lower cost per retention notably in the eCommerce and B2B sectors.

Basically we have two options out there if leveraging the magic of email marketing – Paid and Free platform. If a long term investment return is your preference rather than you being restricted by the platform recurring payment fees, this article can fit your stomach.

This piece expands on how to save 90% of your manual email marketing campaign time by using Gmail and Python script. By the end of this article, you and your team can write a Python script to build an email blast system using Gmail.

gmail smtp

From a marketing perspective, organic traffic is not just limited to SEO. Conversely, it covers a wide range of traffic sources as long as the majority percentage of investment is focused on manpower. They can be social media, partnerships, VoD, email, and so on. Compared to these channels in terms of organic traffic, email marketing is still the most efficient approach in one-to-one conversation marketing with a higher new customer conversion rate and lower cost per retention notably in the eCommerce and B2B sectors.

Basically we have two options out there if leveraging the magic of email marketing – Paid and Free platform. If a long term investment return is your preference rather than you being restricted by the platform recurring payment fees, this article can fit your stomach.

This piece expands on how to save 90% time of your manual and regular email marketing campaign by using Gmail and Python script. By the end of this article, you and your team can write a custom Python script to build an email blast system using Gmail.

Pros and Cons to make more time by using Gmail SMTP marketing automation:

Pros:

  • Save 90%+ time compared to manually send emails;
  • Save 90%+ dollars compared to subscribing a CRM software or investing in an inhouse system built by the 3rd party. Subsequently we can invest these dollars in another area that is difficult to automate;
  • Possibly integrate the Gmail automation application with other applications to streamline the project and further save time.

Cons:

  • A free Gmail account only offer 500 email send per day;
  • Offer less advanced email templates and reporting functions compared to paid CRM;
  • Require talents with Python skills or spend time to write scripts in the first place

Section of the Article:

Connect with Customer Database Using gspread and Google Sheet

First thing first, we need to connect with our customer database which include the target audience contacts and messages. Google Sheet is a great option to store the data because it’s convenient to connect or integrate with applications using gspread. As the screencap depicts, each column represents a type of dataset. I will elaborate more on how to use them in a moment.

google smtp

In the Python script, we can connect Google Sheet using the gspread module. Below are the script codings for your reference.

Add the Personal Account Credential and Connect with Gmail SMTP Server

After having connected with the database, we can add your Gmail account credential to the script and connect with the Gmail server.

Create and Read a Template Message with variable parameters

Sending the right email message to the target audience list is the purpose of Gmail automation, instead of showing off the database and Gmail connection. Therefore, we need to add our email messages to the email campaign.

Here is a sample where we create a txt file and add the message there. What we need your attention is that there are some words there with a symbol $. The reason we need this symbol is because of customizing the messages for every receiver. Whichever word has a symbol before them, that means this position is a variable word. I will explain more on how to make it.

After that, we can open the template file for the purpose of connecting it with the email campaign later on. Below is the script coding sample.

f = open('template.txt','r')

message_template = Template(f.read())

Add Personalized Messages

As I mentioned two things above regarding the database and symbol in the template txt, they are correlated actually. 

First, every row represents an unique audience in the Google Sheet. Subsequently, we categorize the target audience information by column. Thus, each column represents one type of dataset, such as email, name, website link and so on. Please keep in mind that the column naming matters as it’s used to connect with the Python script. Thus, please make sure it’s de-duplicated among the columns

Secondly, the word following after the symbol in the template txt is the variable we create in the Python script. For example, Channel_Name, Video_headline in this case. 

In the MIME module, it has a substitute method for developers to create these variables and connect with the database.  As you can see from the screencap below, each variable has a unique value. The unique value basically is the value from the specific column in the spreadsheet.

For example, we create a loop here: for ChannelName in reader, because there are more than one row normally in the email automation. Then, we use this looping variable to locate which column we aim to match the symbol set in advance in the template txt. ChannelName[2] means the 3rd column in the Google Sheet stores the channel name data. It’s because we count it starting from 0

Send Emails

When the email message customization is ready, it’s time to code sending the email to the target audience in the loop.

The 1st step is to fill in the email information that includes subject, receiver email by using msg method given by MINEText function.

Secondly, as you might be aware, the receiver email is stored in the Google Sheet. Thus, we use the variable with the specific column in the ‘To’ coding line. 

Last but not least, we write a line of codings by using server.sendmail(). In the method, we need to add three values which are sender email, the receiver email, and msg.as_string. 

Additionally, I will suggest to add a time.sleep() because it reduces the emails blocked by Google when the frequency of sending is too high.

Full Python Script of Sending Emails Using Gmail SMTP

If you are interested in the full python script of Chapter 59 – Using Python and Gmail SMTP to Automate Sending Personalised Emails, please subscribe to our newsletter by adding the message “Chapter 59”. We would send you the script immediately to your mailbox.

I hope you enjoy reading Chapter 59 – Using Python and Gmail SMTP to Automate Sending Personalised Emails. If you did, please support us by doing one of the things listed below, because it always helps out our channel.

  • Support and donate to our channel through PayPal (paypal.me/Easy2digital)
  • Subscribe to my channel and turn on the notification bell Easy2Digital Youtube channel.
  • Follow and like my page Easy2Digital Facebook page
  • Share the article to your social network with the hashtag #easy2digital
  • Buy products with Easy2Digital 10% OFF Discount code (Easy2DigitalNewBuyers2021)
  • You sign up for our weekly newsletter to receive Easy2Digital latest articles, videos, and discount code
  • Subscribe to our monthly membership through Patreon to enjoy exclusive benefits (www.patreon.com/louisludigital)