1
votes

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
 }
}
1
This appears to be a continuation of this question. If you're using MailApp.sendEmail(email,subject,message); as in the previous answer, it should do what you describe. If it's not working, please be more specific about what is wrong. - Mogsdad
@Mogsdad This is a continuation of the same script and thank you for your help with the last component of it. For some reason, it is not sending to the second address, after the comma. It is only sending it to the first address. However, it is not throwing any errors. - dericcain
Works For Me. I'm sending to three different (non-aliased, non-google) email addresses, all receive the message. Tested with both GmailApp & MailApp. Even with an invalid email address, no error will be "thrown", although you may receive "undeliverable" messages from email servers, sometimes days later. - Mogsdad
I wonder what is wrong with mine then. I tried numerous different addresses and it only sends to the first email address listed. Any ideas? - dericcain
The three accounts I was using forward to my main gmail account; only one copy of the email appears there. This is due to gmail "smart filtering"... if I go check the separate servers, though, all have copies. Do you have a similar set up? If you're using gmail aliases, the same filtering will happen. - Mogsdad

1 Answers

1
votes

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);
};