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.

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.
  • FAQ

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.

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)

FAQ:

Q1: What is Gmail SMTP?

A: Gmail SMTP (Simple Mail Transfer Protocol) is an email sending protocol that allows you to send emails using your Gmail account.

Q2: Why should I use Gmail SMTP for my eCommerce website?

A: Using Gmail SMTP for your eCommerce website ensures reliable and secure email delivery, as Gmail is known for its high deliverability rate and robust security measures.

Q3: How do I set up Gmail SMTP for my eCommerce website?

A: To set up Gmail SMTP for your eCommerce website, you will need to configure the SMTP settings in your email client or website platform. You can find the necessary settings and instructions in the Gmail SMTP documentation.

Q4: What are the advantages of using Gmail SMTP?

A: Some advantages of using Gmail SMTP include high deliverability, reliable email sending, secure transmission of emails, and the ability to send emails from your own domain using Gmail’s SMTP servers.

Q5: Can I send bulk emails using Gmail SMTP?

A: Yes, you can send bulk emails using Gmail SMTP. However, Gmail has certain sending limits to prevent abuse. It is recommended to use a dedicated email sending service for large-scale email marketing campaigns.

Q6: Is Gmail SMTP free to use?

A: Yes, Gmail SMTP is free to use. However, there may be limitations on the number of emails you can send per day or the number of recipients per email.

Q7: Can I use Gmail SMTP with my own domain email address?

A: Yes, you can use Gmail SMTP with your own domain email address. By configuring the SMTP settings correctly, you can send emails from your domain using Gmail’s SMTP servers.

Q8: Are there any restrictions when using Gmail SMTP?

A: Gmail SMTP has certain restrictions to prevent abuse, such as daily sending limits and limitations on the number of recipients per email. It is important to adhere to these restrictions to maintain a good sending reputation.

Q9: What should I do if my emails sent through Gmail SMTP are not being delivered?

A: If your emails sent through Gmail SMTP are not being delivered, you should check the email addresses, email content, and any error messages received. Additionally, ensure that you have configured the SMTP settings correctly.

Q10: Can I use Gmail SMTP for transactional emails on my eCommerce website?

A: Yes, you can use Gmail SMTP for transactional emails on your eCommerce website. Transactional emails, such as order confirmations and shipping notifications, can be sent reliably and securely through Gmail’s SMTP servers.