dericcain
dericcain

Reputation: 2300

Send Email to Multiple Addresses in Google Spreadsheet

I have constructed a script which sends some data to addresses in cell "B" a spreadsheet sheet named "Email". Currently, it seems to only send to one email address. I want it to be able to send to multiple addresses within the same cell, which would be separated by a comma. Hopefully, this makes sense. I appreciate your help with this! Here is my current code for fetching the email address:

var email = ''; // If we don't find a match, we'll fail the send
  for (var row=0; row < names.length; row++) {
  if (names[row][0] == recipient) {
  email = emails[row][0];
  break; // end the search, we're done
 }
}

Upvotes: 1

Views: 3456

Answers (1)

Mogsdad
Mogsdad

Reputation: 45750

Both GmailApp & MailApp support sending to multiple email addresses, where the addresses are a comma-delimited string. I've tried both, using 3 different addresses on different services (hotmail, yahoo, private), and they worked fine for me.

While we may not know why this isn't working for you, you could try this work-around. It just breaks the string of addresses up into an array, then sends a separate email for each element. (Con: this will increase your email-per-day count unnecessarily.)

var emailArray = email.split(",");
emailArray.forEach( function (address) {
  MailApp.sendEmail(address,subject,message);
});

If you're not into the use of forEach(), this is equivalent:

var emailArray = email.split(",");
for (var i=0; i<emailArray.length; i++) {
  MailApp.sendEmail(emailArray[i],subject,message);
};

Upvotes: 1

Related Questions