0
votes

I have an apps script to automate some emails and want to send a body from a template. I figured I could store the template as a google doc, fetch it, and send it in the script. I have something like this:

var doc = DocumentApp.openById("DocumentID");
MailApp.sendEmail("toEmail", "fromEmail", "TPS report status", doc.getBody().getText());

This does work, except that the email body has new lines inserted in seemingly random areas, although it does retain the new paragraphs that were part of the original document. It's not as obvious in the image, but the red circles are where there are line breaks for something that should be one line. It's very obvious when viewed via gmail app. sample line breaks

2

2 Answers

1
votes

You need to actually format body text in html format and then can user mailapp with 'htmlBody' parameter to pass the body.

You need to get body's paragraphs and add a for loop and add
tag in the beginning of each para.

function getBody()
{
  try{
    var para=tempDoc.getBody().getParagraphs();
    var body=''
    for(var y=0;y<para.length;y++)
    {
        body+="<br>"+para[y].getText();
    }
    return body;
  }
  catch(ex)
  {
    Logger.log(ex)
  }
}
0
votes

To add to the above answer, your html body would look something like this:

body = "Good day, \n\nThe following course has been loaded for deployment:" + "\nCourse Name: " + courseName + "\nCourse Type ID: " + courseID + "\nContent Version: " + courseVersion +
"\nCourse Language: " + courseLanguage + "\n\nCourse Filename: " + title + "\nCourse File Location: " + fileLocation + "\nCourse Filesize: " + fileSize +
"\nDeployment Required By: " + deploymentDate + "\nCourse Live Date: " + courseLiveDate + "\n\n Kind regards\n Department Name";

MailApp.sendEmail(recipient, subject, body, {cc: carbonCopy, noReply: true});