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:
Name | |
---|---|
John Doe | johndoe@example.com |
Jane Smith | janesmith@example.com |
Bob Brown | bobbrown@example.com |
Make sure your sheet has the headers “Name” and “Email” in the first row.
Step 2: Open Google Apps Script
- Open your Google Sheet.
- Click on
Extensions
in the top menu. - 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 theemail
variable, with the givensubject
andbody
.
Step 4: Run the Script
- Save your script by clicking the floppy disk icon or pressing
Ctrl + S
. - Click the
Select function
dropdown in the toolbar and choosesendEmails
. - 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:
- In the Apps Script editor, click on the clock icon (Triggers) on the left sidebar.
- Click
+ Add Trigger
. - Choose
sendEmails
from the function dropdown. - Choose your desired frequency (e.g., daily) and time of day.
- 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.
![](https://i0.wp.com/basescripts.com/wp-content/uploads/2024/07/image-10.png?resize=1024%2C1024&ssl=1)