简体   繁体   中英

Send one Email to Multiple Addresses in Google Spreadsheet

I'm trying to create code that takes a line that contains the address and sends an email to all addresses in the same email. But my code sends the email only to the first address on the list and does not add the others.

 function sendMail(){ var sheetMail = SpreadsheetApp.getActiveSpreadsheet(); var ecrvc = sheetMail.getSheetByName('List'); var lastecrvc = ecrvc.getLastRow(); //var email = []; var people = ","; var subject = "Hello All"; var message = "This is a test"; var email = ecrvc.getRange("B2:B"+lastecrvc).getValues(); for(var i = 9; i<lastecrvc; i++){ var emailTo = ecrvc.getRange(i,2).getValues(); email = emailTo + "@gmail.com"; MailApp.sendEmail(email, subject, message) } }

If you'd like MailApp to send a single email to multiple recipients, you can use a comma separated email string as the first argument of MailApp.sendEmail . For example:

const emails = [ "john.doe@email.com", "jane.doe@email.com" ]

// Join the emails into a single string separated by commas
const combinedEmails = emails.join() // "john.doe@email.com,jane.doe@email.com"

MailApp.sendEmail(combinedEmails, subject, message)

That ought to work. But do verify that you are correctly reading the email addresses from the sheet. If you aren't sure, you could share a sample sheet with identical arrangement as the one you are using.

Assuming that you are reading the email addresses from the spreadsheet correctly, you can modify your last for loop slightly to generate a combined email string, and then send the email outside the loop:

var emailsList = [];
for(var i = 9; i<lastecrvc; i++) {
  var emailTo = ecrvc.getRange(i,2).getValues(); 
  email = emailTo + "@gmail.com";
  emailsList.push(email);
}
var combinedEmails = emailsList.join();
MailApp.sendEmail(combinedEmails, subject, message);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM