Pergunta

Can I email multiple recipients through GmailApp.sendEmail()? I've tried storing the recipient addresses as an array, but it doesn't seem to send any of them.

Thanks!

Foi útil?

Solução

Yes you can.

If you direct yourself towards the Google Apps Spreadsheet services, you can see under the Gmail method that there are advanced parameters. GmailApp Services

It can look something like this.

GmailApp.sendEmail(recipient, subject, message, {cc: "email1@email.com,email2@email.com"});

I never liked that way, and if I was sending to a varying number of people, and wanted to send them each an email instead of it being one big CC, the work around I found was this:

var emails = ["email1@email.com","email2@email.com","email3@email.com"];
for (var i = 0; i < emails.length; i++) {
  GmailApp.sendEmail(emails[i], subject, message);
}

This way you can just simply edit the emails array by adding/subtracting an email and never having to change the actual code that sends the email. Only downside is it sends out X number of emails based on how many addresses you have in the array (if you're worried about hitting the daily cap), but it works none the less.

Outras dicas

The recipient argument takes a string, so you can simply write your multiple recipients as a string with a comma in between:

GmailApp.sendEmail('first.last@example.com, your.friend@example.com', ...); 

Should work. Note that they will all see each other as recipients.

Edit: I tried it and it works.

When I have multiple addresses, I just reference the list via a range. The below script sample would send to 5 email addresses pulled from C2:C6 on the sheet/tab named Email.

    var ss = SpreadsheetApp.getActiveSpreadsheet();
    var emailRange = ss.getSheetByName("Email").getRange(2,3,5,1);
    var emailAddress = emailRange.getValues();
      Logger.log(emailAddress);
    var message = 'Message you wish to enter';
    var subject = 'Verbiage for Subject Line';
   GmailApp.sendEmail(emailAddress, subject, message) 
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top