0

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

rsp
  • 811
  • 2
  • 12
  • 32

2 Answers2

1

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)
  }
}
shabnam bharmal
  • 589
  • 5
  • 13
  • This does work, kind of. If I pass the output "body" into the parameter "htmlBody" in sendMail then it works. However, I can't seem to send a basic string alternative in the "body" parameter. I tried the same getBody function but using "\n" for each new paragraph but it is still introducing line breaks as I originally demonstrated. Any thoughts on passing a string in body? – rsp Feb 09 '19 at 18:12
0

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});
New_2_Code
  • 330
  • 2
  • 18