Google Script: MailApp.sendEmail to multiple addresses?
The email addresses can be concatenated (joined together) using the plus sign with a comma in between each email address. In JavaScript the plus sign can be used for addition OR joining strings. The plus sign is both an addition operator and a string operator in JavaScript. Strings are text, and if you use the plus sign to concatenate text strings then it's a string formula.
One solution would be:
var recipient = row.shiftManager + "," + row.shiftSupervisor;
MailApp.sendEmail(recipient, "Holiday Approval Request", "", {htmlBody: message});
In the above example, there are 4 parameters. But MailApp.sendEmail()
has multiple possible parameter structures. The following example shows all of the settings put into an object, where the "to" key in the object is for the recipient.
MailApp.sendEmail({
to: recipient,
cc: recipientsCC,
subject: Subject,
htmlBody: html
});
A complete example:
function sendToMultiple() {
var message = "This is a test of HTML <br><br> Line two";
var recipientsTO = "[email protected]" + "," + "[email protected]";
var recipientsCC = "[email protected]";
var Subject = "Vacation Approval Request";
var html = message;
MailApp.sendEmail({
to: recipientsTO,
cc: recipientsCC,
subject: Subject,
htmlBody: html
});
}
Documentation with an example is at the following link:
Google Documentation - MailApp.sendEmail
Here is how I'm doing it:
//check quota and log
const emailsLeft = MailApp.getRemainingDailyQuota();
console.log( emailsLeft + " emails left in quota");
//get list of emails from spreadsheet itself
//filter out empty rows
const emails = getTab("Config").getRange("D2:D").getValues().map(function(el){ return el[0]; }).filter(function(el){ return el != '' });
//send emails from NO_REPLY email, subject and HTML body
MailApp.sendEmail({
to: emails.join(","),
subject: subject,
htmlBody: html,
noReply: true
});
function getTab(name) {
var sheet = SpreadsheetApp.getActiveSpreadsheet();
return sheet.getSheetByName(name);
}
getTab() and other helper functions can be found here https://github.com/tim-kozak/google-spreadsheet-helpers