google-apps-scriptgoogle-apps-for-education

Google Apps Script: Multiple recipients in GmailApp?


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!


Solution

  • 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.