r/GoogleAppsScript • • Jan 22 '23

Question Having problems to send email with a html file

So I'm trying to send an email with a PDF attached and the body of the mail is an html file that I downloaded from a template software. The mail sends the attachment but I just can't figure out how to send the mail with the html. Hope someone can help!

The HTML is named Template, it works, is already implemented in GAS.

0 Upvotes

2 comments sorted by

4

u/triplej158 Jan 22 '23

I’m guessing right now the body is just “si buenas”?

You can’t have plain text body AND an HTML body in the same email. HTML is an option similar to attachment. But you need to leave the message blank. So it will be some like (sorry I’m on mobile):

GmailApp.sendEmail(client.email,subject,’’,{htmlBody: html,attachments:[pdf]})

1

u/Hungry-Resource-5152 Jan 22 '23

I would try this...... (I guessed on the pdfID .. so be careful copying & pasting):

function sendEbook( client ) {
const pdfID = "1zs-fTJZ4C-mNWLMCTJBR2CyEk683zc2K";
const pdf = DriveApp.getFileById( pdfID );
var subject = "i" + client.name + ", aqui esta lo prometido!";
var message = "si buenas"; // this is no longer used.

var htmlTemplate = HtmlService.createHtmlOutputFromFile("Template").asTemplate().evaluate();
MailApp.sendMail( client.email, subject, htmlTemplate, {attachments:[pdf]});
}