How to setup Email Marketing using Google Apps Script

Ravgeet Dhillon
Ravgeet Dhillon on Aug 27, 20205 min read
Written in
Blog banner for How to setup Email Marketing using Google Apps Script

Recently, we have been writing a lot of stuff related to Web Design and Development, like collecting form responses, that can be implemented using Google Apps Script and Serverless Architecture. In this blog, we will talk about email marketing. We will set up custom email marketing purely using Google Apps Script. The advantage is we can take control of our email marketing campaign and create our own automated workflows.

Contents

1. Creating a new Spreadsheet

First of all, we need a Google Sheet where we store all of our email addresses to whom we want to send the emails. Let’s create a new spreadsheet.

Enter the user details in Google Sheet
Enter the user details in Google Sheet

2. Creating a new Google Apps Project

Now is the time to connect our Google sheet to a Google Apps Script. From Tools, we select the Script Editor.

Connect Google Sheet to Google Apps Script project
Connect Google Sheet to Google Apps Script project

3. Writing code

Finally, it is time to write some code.

a.) Main.gs

Add the following the code to the file:

function sendEmails(mail_template='content',
                    subject='Testing my Email Marketing') {
  
  // get the active spreadsheet and data in it
  var id = SpreadsheetApp.getActiveSpreadsheet().getId();
  var sheet = SpreadsheetApp.openById(id).getActiveSheet();
  var data = sheet.getDataRange().getValues();
  
  // iterate through the data, starting at index 1
  for (var i = 1; i < data.length; i++) {
    var row = data[i];
    var email = row[0];
    var name = row[1];
    
    // check if we can send an email
    if (MailApp.getRemainingDailyQuota() > 0) {
        
      // populate the template
      var template = HtmlService.createTemplateFromFile(mail_template);
      template.name = name;
      var message = template.evaluate().getContent();
      
      GmailApp.sendEmail(
        email, subject, '',
        {htmlBody: message, name: 'RavSam Team'}
      );
    }
  }
}

The comments have been included in the file for a proper description of the above function.

Always use GmailApp.sendEmail instead of MailApp.sendEmail. It is a more stable and reliant function.

b.) content.html

Since the above script uses an HTML file and populates it, we need to create an HTML template file. Add the following the code to the file:

<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hi <?= name ?>. We are testing our beta features for email marketing.
  </body>
</html>

The <?= name ?> template variable gets auto-filled by the email marketing script.

4. Running the script

We have done all the necessary setup to start a successful email marketing campaign. Before we run our code, we need to grant

Setup the Google Apps Script Authorization
Authorize the Google Apps Script to send an email on your behalf

Results

Let us check our email to see if the email was received. Awesome! We can clearly see that email was delivered successfully to the user’s inbox.

Email Delivered successfully to the inbox
Email Delivered successfully to the inbox

We can create more beautiful and custom HTML templates and manage our email marketing campaigns around them. In our next blog, we will be talking about how to track whether a user opens our emails or not. If you any doubts or appreciation for our team, let us know in the comments below.

Subscribe to RavSam Web Solutions newletter for information about Website development, mobile apps development and software development

Loved this post? Join our Newsletter 🚀

We write about Nuxt, Vue, Flutter, Strapi, Python and Automation. We don't spam.

Please add a valid email.
By clicking submit button, you agree to our privacy policy.
Thanks for subscribing to our newsletter.
There was some problem while registering your newsletter subscription. Please try again after some time or notify the owners at info@ravsam.in

ABOUT AUTHOR

Ravgeet Dhillon

Ravgeet is a Co-Founder, Freelancer and Developer at RavSam. He helps startups, businesses, open-source organizations with Digital Product Development. He is a fan of Jamstack and likes to work with Flutter, Strapi, Nuxt/Vue and Python. Connect with Ravgeet on Twitter and LinkedIn.

KEEP READING

Blog banner for Add Unsubscribe link in emails using Google Apps Script

Add Unsubscribe link in emails using Google Apps Script

Written in Marketing
Blog banner for How to track Email opens with Google Apps Script

How to track Email opens with Google Apps Script

Written in Marketing

Got a project or partnership in mind?

Let's Talk

Contact Us ->