PY_
PY_

Reputation: 1269

Convert MailApp.sendEmail code to GmailApp.sendEmail with HTML Body

All, I am looking for help from someone with more experience with this. I have a cobbled together email script that works great as is. I want to specify the from: address using an alias and it's my understanding that I need to use GmailApp vs MailApp to accomplish this. The trouble is, I can't figure out how to make my htmlBody work in the GmailApp version.

Here is my working MailApp code:

function SendPreReleaseAlertEmail(row) { 
  var sheet = SpreadsheetApp.getActive().getSheetByName('EmailSheet');
  var subject = sheet.getRange("M3").getValues();  // Change the subject as needed.
  var recipients = sheet.getRange("M2").getValue();  // Change the recipient as needed.
  var message = "<HTML><BODY>" + "<P>"
   for (var x=6;x<14;x++) {                                       //Loop from * to * with a +1 increment
     message = message + sheet.getRange("M" + x).getValues() + "<BR>"      //Add row I(x) to the message 
   }
    message = message + "</HTML></BODY>"; 
   MailApp.sendEmail(recipients, subject, "", {htmlBody: message});
}

And this is my FAILED attempt at converting it to GmailApp with a From address:

function SendPreReleaseAlertEmail(row) { 
  var sheet = SpreadsheetApp.getActive().getSheetByName('EmailSheet');
  var subject = sheet.getRange("M3").getValues();  // Change the subject as needed.
  var recipients = sheet.getRange("M2").getValue();  // Change the recipient as needed.
  var message = "<HTML><BODY>" + "<P>"
   for (var x=6;x<14;x++) {                                       //Loop from * to * with a +1 increment
     message = message + sheet.getRange("M" + x).getValues() + "<BR>"      //Add row I(x) to the message 
   }
    message = message + "</HTML></BODY>"; 
   GmailApp.sendEmail(recipients, subject, {htmlBody: message}, {from: "[email protected]"});
}

The above code does in fact email from the alias. But the htmlBody just has the words "[object Object]" in the body of the email.

It's Alive! Here is the final code that solved my problem:

GmailApp.sendEmail(recipients, subject, '', {htmlBody: message, from: "[email protected]"});

Upvotes: 0

Views: 960

Answers (2)

Marios
Marios

Reputation: 27350

You can still use MailApp like that:

var email = "[email protected]";
var Subject_to_Send = "This is an automated email";
  
var check_body = 
     "Good morning team,  <br/><br/>" 
     +"I hope this email finds you well. <br/><br/>";

MailApp.sendEmail( {to:email, subject:Subject_to_Send, body:check_body,htmlBody:check_body, from: "[email protected]"}); 

If you also want to send a noReply message you can adjust the last line as follows:

MailApp.sendEmail( {to:email, subject:Subject_to_Send, body:check_body,htmlBody:check_body, noReply: true});

Upvotes: 1

ADW
ADW

Reputation: 4247

I think you need to fix two things.

1.The syntax is

sendEmail(recipient, subject, body, options) 

So you may need to include a blank placeholder for body in your code.

2.You may also need to have both the htmlBody and from in the options JavaScript object like so:

  GmailApp.sendEmail(recipients, subject, '' , {
    htmlBody: message, 
    from: "[email protected]"});

Upvotes: 2

Related Questions