Format text in auto email script

2019-07-19 11:19发布

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条回答
Summer. ? 凉城
2楼-- · 2019-07-19 12:02

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 });
}​
查看更多
登录 后发表回答