How to Email a Group of Contacts Using Google Apps Script

Google Apps Script is a powerful tool that allows you to automate tasks within Google Workspace. One of its many capabilities is sending emails to a group of contacts. In this blog post, we’ll walk through the steps to create a script that sends an email to a group of contacts stored in a Google Sheet.

Step 1: Set Up Your Google Sheet

First, you’ll need a Google Sheet containing the email addresses of your contacts. Here’s an example of how your sheet might look:

NameEmail
John Doejohndoe@example.com
Jane Smithjanesmith@example.com
Bob Brownbobbrown@example.com

Make sure your sheet has the headers “Name” and “Email” in the first row.

Step 2: Open Google Apps Script

  1. Open your Google Sheet.
  2. Click on Extensions in the top menu.
  3. Select Apps Script.

This will open the Google Apps Script editor in a new tab.

Step 3: Write the Script

In the Apps Script editor, delete any code in the script file and replace it with the following code:

function sendEmails() {
// Open the Google Sheet
var sheet = SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1');

// Get all the data in the sheet
var data = sheet.getDataRange().getValues();

// Loop through each row (starting from row 2 to skip headers)
for (var i = 1; i < data.length; i++) {
var name = data[i][0]; // Get the name
var email = data[i][1]; // Get the email address

// Create the email subject and body
var subject = 'Hello ' + name + '!';
var body = 'Hi ' + name + ',\n\nThis is a test email sent using Google Apps Script.';

// Send the email
MailApp.sendEmail(email, subject, body);
}
}

Explanation:

  • SpreadsheetApp.getActiveSpreadsheet().getSheetByName('Sheet1'): Opens the active spreadsheet and gets the sheet named ‘Sheet1’.
  • sheet.getDataRange().getValues(): Retrieves all the data from the sheet.
  • The for loop iterates through each row of data, skipping the header row.
  • MailApp.sendEmail(email, subject, body): Sends an email to the address specified in the email variable, with the given subject and body.

Step 4: Run the Script

  1. Save your script by clicking the floppy disk icon or pressing Ctrl + S.
  2. Click the Select function dropdown in the toolbar and choose sendEmails.
  3. Click the play button (triangle icon) to run the script.

You will be prompted to authorize the script to send emails on your behalf. Follow the prompts to grant the necessary permissions.

Step 5: Automate the Script (Optional)

If you want this script to run automatically at certain intervals (e.g., daily), you can set up a trigger:

  1. In the Apps Script editor, click on the clock icon (Triggers) on the left sidebar.
  2. Click + Add Trigger.
  3. Choose sendEmails from the function dropdown.
  4. Choose your desired frequency (e.g., daily) and time of day.
  5. Click Save.

Conclusion

With Google Apps Script, sending emails to a group of contacts stored in a Google Sheet is a simple and powerful way to automate your communication tasks. By following the steps in this guide, you can set up your own email automation in no time.