0
votes

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).

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

Here's an example to illustrate:

Running this function :

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("[email protected]", "test", text);
}

Will give an email that looks like this

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.

Thanks a lot in advance !

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

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.

1

1 Answers

2
votes

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: "[email protected]",
    subject: "test", 
    htmlBody: htmlText,
  });