Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Google Script: MailApp.sendEmail to multiple addresses?

I have a script that is using the following script:

MailApp.sendEmail(row.shiftManager, "Holiday Approval Request", "", {htmlBody: message});
  row.state = STATE_PENDING;

Howeverm I would like to also send the same mail to row.shiftSupervisor, this is probably something really simple that I've overlooked, but I figured someone here would know straight away what it was.

Cheers for your help :)

Edit - I tried to use:

MailApp.sendEmail(row.shiftManager, row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});
      row.state = STATE_PENDING;

But no joy.

Edit 2 - I got it working with:

  MailApp.sendEmail(row.shiftManager, "Holiday Approval Request", "", {htmlBody: message});
  MailApp.sendEmail(row.shiftSupervisor, "Holiday Approval Request", "", {htmlBody: message});
  row.state = STATE_PENDING;

Not the most graceful looking piece of code, but it does the job...

like image 581
SL8t7 Avatar asked Feb 05 '15 22:02

SL8t7


1 Answers

The email addresses can be concatenated (joined together) using the plus sign with a comma in between each email address. In JavaScript the plus sign can be used for addition OR joining strings. The plus sign is both an addition operator and a string operator in JavaScript. Strings are text, and if you use the plus sign to concatenate text strings then it's a string formula.

One solution would be:

var recipient = row.shiftManager + "," + row.shiftSupervisor;
MailApp.sendEmail(recipient, "Holiday Approval Request", "", {htmlBody: message});

In the above example, there are 4 parameters. But MailApp.sendEmail() has multiple possible parameter structures. The following example shows all of the settings put into an object, where the "to" key in the object is for the recipient.

MailApp.sendEmail({
  to: recipient,
  cc: recipientsCC,
  subject: Subject,
  htmlBody: html
});

A complete example:

function sendToMultiple() {
  var message = "This is a test of HTML <br><br> Line two";
  
  var recipientsTO = "[email protected]" + "," + "[email protected]";
  var recipientsCC = "[email protected]";
  var Subject = "Vacation Approval Request";
  var html = message;
  
  MailApp.sendEmail({
    to: recipientsTO,
    cc: recipientsCC,
    subject: Subject,
    htmlBody: html
  });

}

Documentation with an example is at the following link:

Google Documentation - MailApp.sendEmail

like image 98
Alan Wells Avatar answered Oct 08 '22 10:10

Alan Wells