How do I send an email in google apps script with basic text formatting?

久未见 提交于 2021-02-11 14:00:38

问题


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.


回答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)
  }
}



回答2:


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});


来源:https://stackoverflow.com/questions/54586334/how-do-i-send-an-email-in-google-apps-script-with-basic-text-formatting

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!