Set up Email Marketing using Google Apps Script without setting any dedicated servers.

Setup Email Marketing using Google Apps Script

Ravgeet Dhillon

Ravgeet Dhillon

Updated on Oct 08, 2021 in Marketing

⏱ 5 min read

Blog banner for 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, you will learn about email marketing. You will set up custom email marketing purely using Google Apps Script. The advantage is you can take control of your email marketing campaign and create your own automated workflows.

Contents

Creating a Spreadsheet

First of all, you need a Google Sheet where you can store all of your email addresses to whom you want to send the emails. So, first create a new spreadsheet.

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

Creating a Google Apps Project

Next, you need to connect your Google Sheet to a Google Apps Script. From Tools, select the Script Editor.

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

Writing Code

Finally, it is time to write some code.

a.) Main.gs

Add the following code to this file:

function sendEmails(mail_template='content',
                    subject='Testing my Email Marketing') {
  
  // get the active spreadsheet and data in it
  const id = SpreadsheetApp.getActiveSpreadsheet().getId()
  const sheet = SpreadsheetApp.openById(id).getActiveSheet()
  const data = sheet.getDataRange().getValues()
  
  // iterate through the data, starting at index 1
  for (let i = 1; i < data.length; i++) {
    const row = data[i]
    const email = row[0]
    const name = row[1]
    
    // check if you can send an email
    if (MailApp.getRemainingDailyQuota() > 0) {
        
      // populate the template
      let template = HtmlService.createTemplateFromFile(mail_template)
      template.name = name
      const 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 Main.gs uses an HTML file and populates it, you need to create an HTML template file. Add the following code to content.html:

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

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

4. Running the Script

You have done all the necessary setup to start a successful email marketing campaign. Before you execute your code, you need to grant permissions to your script:

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

Results

Let’s check the inbox to see if any email is received. Awesome! You can see that the email was delivered successfully to the user’s inbox.

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

You can create more beautiful and custom HTML templates and manage your email marketing campaigns around them. In the next blog, you will learn to track whether a user opens your emails or not.

📫

Loved this post? Join our Newsletter.

We write about React, 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 and Developer at RavSam. He helps startups, businesses, open-source organizations with Digital Product Development and Technical Content Writing. He is a fan of Jamstack and likes to work with React, Vue, Flutter, Strapi, Node, Laravel and Python. He loves to play outdoor sports and cycles every day.

Got a project or partnership in mind?

Let's Talk

Contact Us ->