taleesita
taleesita

Reputation: 83

Send Email using HTML via Google Scripts

I've created a script that will populate info from a Google Form into a template and email a generated PDF. I want to be able to customize the email it sends using HTML, but for some reason I can't get it to work the way I have with similar scripts in the past.

Code below. With this code, the email outputs "Please find your ML enclosed. <_br/>" (but without the _... stackoverflow is actually reading the HTML and inputs a line break for me without it)

I want the output to be "Please find your ML enclosed.

[linebreak]"

    function onSubmit(e) {
  const rg = e.range;
  const sh = rg.getSheet();
  
  //Get all the form submitted data
  //Note: This data is dependent on the headers. If headers, are changed update these as well.
  const Email= e.namedValues['Email Address'][0];
  const Team = e.namedValues['Team'][0];
  const Name = e.namedValues['Name'][0];
  const Time1 = e.namedValues['time1'][0];
  const Adjective1 = e.namedValues['adjective1'][0];
  const LengthofTime1 = e.namedValues['lengthoftime1'][0];
  const Adjective2 = e.namedValues['adjective2'][0];
  const Number1 = e.namedValues['number1'][0];
  const Verb1 = e.namedValues['verb1'][0];
  const Verb2 = e.namedValues['verb2'][0];
  const Noun1 = e.namedValues['noun1'][0];
  
  //Build a new invoice from the file
  //Folder and file IDs
  const MLFolderID = '<Folder ID>';
  const MLFolder = DriveApp.getFolderById(MLFolderID);
  
  const TemplateFileID = '<template ID>';
  const newFilename = 'ML -' + TemplateFileID;
  
  //Make a copy of the template file
  const newTemplateFileID = DriveApp.getFileById(TemplateFileID).makeCopy(newFilename, MLFolder).getId();;
  
  //Get the invoice body into a variable
  var document = DocumentApp.openById(newTemplateFileID);
  var body = document.getBody();
  
  //Replace all the {{ }} text in the invoice body
  body.replaceText('{{team}}', Team);
  body.replaceText('{{name}}', Name);
  body.replaceText('{{time1}}', Time1);
  body.replaceText('{{adjective1}}', Adjective1);
  body.replaceText('{{lengthoftime1}}', LengthofTime1);
  body.replaceText('{{adjective2}}', Adjective2);
  body.replaceText('{{number1}}', Number1);
  body.replaceText('{{verb1}}', Verb1);
  body.replaceText('{{verb2}}', Verb2);
  body.replaceText('{{noun1}}', Noun1);
  
  
  document.saveAndClose();

// define email variables
var subject = 'Subject ML';
var msgHtml = 
"Please find your ML enclosed." + "<br/>";
var attachment = DriveApp.getFileById(newTemplateFileID);

//send email with the file
GmailApp.sendEmail(Email, subject, msgHtml, {attachments: [attachment.getAs(MimeType.PDF)],   from:'[email protected]'});
  }

Upvotes: 0

Views: 427

Answers (1)

taleesita
taleesita

Reputation: 83

Instead of

GmailApp.sendEmail(Email, subject, msgHtml, {attachments: [attachment.getAs(MimeType.PDF)],   from:'[email protected]'});

Use

GmailApp.sendEmail(Email, subject, msgHtml, {htmlBody: msgHtml, attachments: [attachment.getAs(MimeType.PDF)], from:'[email protected]'}); } 

Upvotes: 1

Related Questions