本文介绍了如何阻止MailApp.sendEmail()每隔80个字符将换行符添加到电子邮件正文中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用附加到Google云端硬盘中Google工作表的Google脚本来发送电子邮件。我注意到,MailApp.sendEmail()发送的邮件正文看起来与原始邮件看起来不太像:它每隔〜75个字符添加一个换行符(不打扰单词)。

I am using a google script attached to a google sheet in my Google Drive to send emails. I have noticed that MailApp.sendEmail() sends emails with a body that have doesn't quite look like the original: it adds a line break every ~75 characters (without interrupting words).

如何阻止MailApp.sendEmail()函数执行此操作?

How can I stop the MailApp.sendEmail() function from doing this?

下面是一个示例来说明:

Here's an example to illustrate:

运行此函数:

function sendTestEmail(){
  var text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.";
  MailApp.sendEmail("test@gmail.com", "test", text);
}

将给出如下所示的电子邮件

Will give an email that looks like this

如果有用,我做了一些测试:一个76个字符的句子留在一个行(不再),但是添加一个额外的单词会使此新句子的最后两个单词进入新行。

In case it's useful, I tested a bit: a 76 character sentence stays on one line (no more), but adding an extra word made the last two words of this new sentence go to a new line.

请多多感谢!

编辑1 :这些换行符不会出现在mac邮件或雷鸟中,但会出现在iphone gmail应用程序中。

EDIT 1: These line breaks do not appear in mac mail or thunderbird, but do appear on the iphone gmail app.

我也尝试使用html而不是纯文本,它确实消除了不必要的换行符:太好了!但是,除非我手动输入< br> ,否则它还会删除所有换行符。

Also I tried using html instead of plain text, and it does remove the unwanted line breaks: great! But it also removes all line breaks unless I put <br> manually.

推荐答案

我可能需要微调细节,但建议如下对我有用。需要 text.replace 来使我可能故意在HTML电子邮件中出现的换行符。

I might need to fine tune details, but as suggested the following works for me. The text.replace is needed to make the line breaks I might put in intentionally to appear in the html email.

function sendTestEmail(){
  var text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
  var htmlText = text.replace(/\n/g,'\n<br>');
  MailApp.sendEmail({
    to: "test@gmail.com",
    subject: "test", 
    htmlBody: htmlText,
  });

这篇关于如何阻止MailApp.sendEmail()每隔80个字符将换行符添加到电子邮件正文中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-15 02:09