vector
vector

Reputation: 1022

Having issue with passing HTML Template body to gmail body through App-Script

I'm trying to send an email reminder every day to a user about their pending tasks. I'm using an HTML Template for HTML content in the email body, but what I am receiving in Email is this:

[object Object]

The Code appears to be running smoothly, and log data is looking good, I need help in understanding what I am doing wrong?

Thanks,

function TasKReminder() {
 const ss= SpreadsheetApp.openById('1pNTZxlj500VbXcGY6aIuzBtqaTP6Ho3dpPYCoRHtjMM').getSheetByName("My Open Tasks");
 const pendingTasks = ss.getRange("B1").getValue();
 const lr = ss.getLastRow();
 if(pendingTasks > 0) { 
    var tData = ss.getRange(5,1,pendingTasks,8).getDisplayValues();
 //  Logger.log(tData);
 }
 //Delay Tasks
 var delayedTasks = tData.filter(function(tasks){
   return tasks[6].length > 0 ;
 });
 const htmlTemplate = HtmlService.createTemplateFromFile("Template");
 htmlTemplate.delayedTasks = delayedTasks;
 const htmlEmailbody = htmlTemplate.evaluate().getContent();
 Logger.log(htmlEmailbody);
  GmailApp.sendEmail("[email protected]", "Pending task on your Task List",{htmlbody: htmlEmailbody });
}
<!DOCTYPE html>
<html>
  <head>
    <base target="_top">
  </head>
  <body>
    Hi,<B></B>
    <table>
       <thead>
             <tr>
                 <th>Task Type</th><th>Task Name</th><th>Code</th><th>Assigned By</th><th>Planned Completion</th><th>Delay</th><th>Status</th>
             </tr>      
       </thead>
       <tbody>
             <? delayedTasks.forEach((r,i) => { ?>
              <tr>
                <td><?= r[0]?></td><td><?= r[1]?></td><td><?= r[3]?></td><td><?= r[4]?></td><td><?= r[5]?></td><td><?= r[6]?></td>
              </tr>
             <? }) ?>
       </tbody>
    
    </table>
  </body>
</html>

Upvotes: 1

Views: 519

Answers (1)

Tanaike
Tanaike

Reputation: 201643

I think that your script has 2 modification points. The arguments of sendMail of Class GmailApp are sendEmail(recipient, subject, body, options). So in your case, option is used as 3rd argument. I think that this is the reason of [object Object]. And, the key name of htmlbody is required to be htmlBody. When these points are reflected to your script, it becomes as follows.

From:

GmailApp.sendEmail("[email protected]", "Pending task on your Task List", {htmlbody: htmlEmailbody});

To:

GmailApp.sendEmail("[email protected]", "Pending task on your Task List", "", {htmlBody: htmlEmailbody});
  • In this modification, the text body is "". So when the email client cannot read HTML mail. The empty body is seen. Please be careful this.

Reference:

Upvotes: 1

Related Questions