0

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@gmail.com'});
  }
  • I cannot understand about 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.. I apologize for this. In order to correctly understand about your question, can you provide the detail of your current issue and your goal? – Tanaike yesterday
  • @Tanaike I didn't realize StackOverflow was actually inserting a line break for me! Here is what I get now (but without the underscore) "Please find your ML enclosed. <_br/>". I want the output to be "Please find your ML enclosed." + a linebreak – taleesita yesterday
  • Instead of this GmailApp.sendEmail(Email, subject, msgHtml, {attachments: [attachment.getAs(MimeType.PDF)], from:'email@gmail.com'}); try this: GmailApp.sendEmail(Email, subject, '', {attachments: [htmlBody:msgHtml,attachment.getAs(MimeType.PDF)], from:'email@gmail.com'}); – Cooper yesterday
  • @Cooper - That didn't quite do the trick, but it clued me in! This ended up working - GmailApp.sendEmail(Email, subject, msgHtml, {htmlBody: msgHtml, attachments: [attachment.getAs(MimeType.PDF)], from:'email@gmail.com'}); } I'd never had to have the "htmlBody: msgHtml" part before, but that seemed to do the trick! – taleesita yesterday
0

Instead of

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

Use

GmailApp.sendEmail(Email, subject, msgHtml, {htmlBody: msgHtml, attachments: [attachment.getAs(MimeType.PDF)], from:'email@gmail.com'}); } 
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.