Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Google Script for Google Sheets mass emailing
// modified from https://developers.google.com/apps-script/articles/sending_emails#section2, cf https://developers.google.com/site-policies
// Licensed under Apache 2.0 License http://www.apache.org/licenses/LICENSE-2.0,
// Copyright (c) Google Inc, MathJax Consortium
//
function sendEmails() {
var sheet = SpreadsheetApp.getActiveSheet();
var startRow = 1; // First row of data to process
var numRows = 1; // Number of rows to process
// Fetch the range of cells
var dataRange = sheet.getRange(startRow, 1, numRows, 2) // getRange(row, column, numRows, numColumns)
// Fetch values for each row in the Range.
var data = dataRange.getValues();
for (var i = 0; i < data.length; ++i) {
var row = data[i];
var name = row[0]; // First column
var emailAddress = row[1]; // Second column
var subject = "subject line";
var message = 'Dear '+ name +',\n\n' // the actual message
+ 'moar lines.\n\n' // or else
if (sheet.getRange(startRow + i, 3).isBlank()) { // Prevents sending duplicates
MailApp.sendEmail(emailAddress, subject, message);
var d = new Date();
sheet.getRange(startRow + i, 3).setValue(d.toLocaleTimeString()); // record time of the email
// Make sure the cell is updated right away in case the script is interrupted
SpreadsheetApp.flush();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment