4
votes

I am running the following script in Google apps script (emailAddress is a variable pulled from a sheet):

MailApp.sendEmail(emailAddress, subject, message,{htmlBody:message});

I am trying to add a cc and/or bcc string to this, but when I do so (and I using the correct format) I am getting a consistent error that there are too many strings when I do so.

Is MailApp.sendEmail limited to only four strings? Is my use of the {htmlBody:message} the problem? I was able to partially achieve what I am trying to do by eliminating this, but my goal is to send an html email out and copy that email to an internal address so all my staff can see the sent email, rather than only the sender.

Another issue is that I do not want the following

var emailSent = row[5];
if (emailSent != "EMAIL_SENT")

to operate on the cc'd and/or bcc'd email or my source sheet will include two EMAIL_SENT entries for every email.

Is there a solution?

3

3 Answers

11
votes

Please try this:

MailApp.sendEmail(emailAddress, subject, message, {
  htmlBody: message,
  cc: '[email protected]',
  bcc: '[email protected]'
});

There are other parameters also available check here : https://developers.google.com/apps-script/reference/mail/mail-app#sendemailrecipient-subject-body-options

2
votes
function myFunction(){

  
  // html email
  var htmlEmailBody = HtmlService.createTemplateFromFile('html-template-name');

  // email title
  var subject = "your title here..";
  
  // this must be set or .sendEmail will not work. You can insert your own email address to get a copy of the email or just let it blank. Alternative you can delete bcc and just the emailAddress value to send 1 email only.
  var emailAddress = "";
  
  // same like emailAddress this must be set aswell. You can just keep it blank and use htmlBody for your html email. Alternative delete htmlBody and use normalBody for plain text email instead.
  var normalBody = "";
 

MailApp.sendEmail(emailAddress, subject, normalBody, {
  htmlBody: htmlEmailBody.evaluate().getContent(),
  bcc: '[email protected],[email protected]'
});

    

}
0
votes

You can use the following syntax with an HTML template.

   /**
   * Send email with CC
   * @param {string} email - to email address
   * @param {string} cc_email - cc email address
   * @param {string} email_subject - subject of the email
   * @param {string} from_name - email will appear to be from this name
   * @param {string} template_name - name of HTML template
   */
    function sendEmail(email, cc_email, email_subject, from_name, template_name) {
        var templ = HtmlService.createTemplateFromFile(template_name);
        var message = templ.evaluate().getContent();
        MailApp.sendEmail({
          to: email,
          cc: cc_email,
          subject: email_subject,
          htmlBody: message,
          name: from_name
        });
    }

Here is the official documentation of MailApp.sendEmail with advanced options: https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(String,String,String,Object)