Search code examples
google-apps-scriptgmail

How do I send email to multiple recipients in google app script


I am unable to send email through google script to multiply addresses

Tried with GmailApp.sendEmail,MailApp.SendEmail, var recipient = testmail + chemAmail ;.

  var testmail     = "example1@gmail.com" ;
  var chemAmail    = "example2@gmail.com" ;

  var body = "Hi,<br><br>" +
             "This is a Quality Alert.Please brief the required people and submit the Briefing Record EForm when it is completed." + "<br><br>" +
             "The details are stated below :<br>" +
             "Customer: " + customer + "<br>" +
             "Part Number: " + part + "<br>" +
             "S Number: " + s + "<br>" +
             "ID Number: " + id + "<br>" +
             "Defect : " + defect + "<br>" +
             "Description : " + desc + "<br>" +
             "Reject Photos : " + reject + "<br>" +
             "Okay Photos : " + ok + "<br><br>" +
             "Please complete it as soon as possible." + "<br><br>" +
             "Thanks." ;

    // Sends if chemA is ticked (Conditions for email to be sent)
    if(chemA == '✔')
    {
      var recipient = testmail , chemAmail;
      var subject = "Quality Alert for Chem A";
    }

GmailApp.sendEmail(recipient, subject, " ",{htmlBody: body});  

I can send it to testmail only. I want to be able to send to multiple email addresses.


Solution

    • You want to send a mail to several mail addresses.

    If my understanding is correct, how about this modification?

    From:

    var recipient = testmail , chemAmail;
    

    To:

    var recipient = [testmail, chemAmail];
    

    Note:

    • In my environment, I'm using above method. But I couldn't find the official document, so if this didn't work in your environment, I apologize.
      • By TheMaster's comment, I could find the description of comma separated list of email addresses at GmailApp.sendEmail().
        • By this, the array might be converted to the string by the script like array.join(",").

    Reference: