Format text in auto email script

馋奶兔 提交于 2019-12-11 00:31:54

问题


I use the following script to send auto confirmations from a spreadsheet which is synced with a google form.

function formSubmitReply(e) {
  var userEmail = e.values[2];
  MailApp.sendEmail(userEmail,
                    "Thanks for Volunteering",
                     " Hello\n\n"+
                     "Thanks you\n\n"+
                     "Have a great day\n\n",
                     {name:"ABC"});
                             }​

The \n takes the text to the next line. However, it's all left aligned. Is there anything I can add to make it right aligned? something like \r? Thanks


回答1:


The text body of an email supports no formatting. You could embed tabs (\t) or spaces to try to push the text to the right; but how would you know how wide the recipient's display is?

If you want to control the presentation of your email, you need to use the htmlBody option described here.

You can affect basic formatting with embedded css styles, by wrapping the text of the email in <div> tags, and placing css property / value pairs in a style attribute. For example, this would produce right-aligned text (go ahead, "run" it and see):

<div style='text-align:right'>
  This is the text <br> that we want to have <br> right-aligned.
</div>

This example wraps the text version of the email body in <div> tags with our selected style, replacing the newline characters to html <br> tags. When we send this email, both the text and html versions are included; recipients with email clients that support html will see the right-aligned html version, others will see the plain text.

function formSubmitReply(e) {
  var userEmail = e.values[2];
  var text = "Hello\n\n"+
             "Thanks you\n\n"+
             "Have a great day\n\n";
  var html = "<div style='text-align:right'>"
           + text.replace(/\\n/g,"<br>")   // Replace newlines with breaks
           + "</div>"
  MailApp.sendEmail(userEmail,
                    "Thanks for Volunteering",
                    text,
                    { name: "ABC"
                      htmlBody: html });
}​


来源:https://stackoverflow.com/questions/24050046/format-text-in-auto-email-script

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