Line break in a message
Asked Answered
F

7

13

With Google Apps Script, how to make a line break in a variable to send mail?

Fumble answered 23/5, 2012 at 13:21 Comment(0)
U
11

If you're not sending an HTML formatted message, use "\n". I personally despise HTML formatted e-mail.

Unless answered 23/5, 2012 at 14:29 Comment(2)
Also important to note that many spam filters will not like htmlBresee
hi I used this in my email message which was a string, but i didn't get a new line in the email. do you know what the problem could be?Epigene
R
11

Newline in msgBox:

Browser.msgBox('line 1 \\n line 2');

Please note you need to escape '\n' with additional backslash.

Renal answered 16/11, 2015 at 9:23 Comment(0)
E
8

You should use the <br> tag when sending the HTML portion of the email .

Below is a sample on how I compose the same email body, but formatted differently for HTML & plain text. (Not the best code but hopefully it illustrates the point)

function onFormSubmit(e) {
  var subject = "Subject";

  // Collect user data
  var name = e.values[0];
  var email = e.values[1];   // Where user enters his/her email address

  // Generate content - Replace this with what you're composing
  var content = [];
  content.push("Hi " + name);
  content.push("Thanks for submitting the survey!___LINE_BREAK___");
  content.push("Survey Team");

  // Combine content into a single string
  var preFormatContent = content.join('___LINE_BREAK___');

  // Replace text with \n for plain text
  var plainTextContent = preFormatContent.replace('___LINE_BREAK___', '\n');
  // Replace text with <br /> for HTML
  var htmlContent = preFormatContent.replace('___LINE_BREAK___', '<br />');

  MailApp.sendEmail(email , 
                    subject, 
                    plainTextContent ,                    
                    { 
                      name: "Survey Team", 
                      html: htmlContent 
                    });  
}
Elvia answered 23/5, 2012 at 15:30 Comment(3)
The replace only searches for the first instance, though a bit messy my solution is preFormatContent.split('LINE_BREAK').join('\n');Vaporetto
@Hovo, thanks, sometimes a comment fulfill the requirement.Arhat
@Elvia Thanks for this neat solution. There is minor bug in the example. The above example just replace the first ___LINE_BREAK___. So, in order for complete replacement, just add the global replacement option i.e. var plainTextContent = preFormatContent.replace(/___LINE_BREAK___/g, '\n'); and var htmlContent = preFormatContent.replace(/___LINE_BREAK___/g, '<br />'); @Vaporetto You may want to take note of this, instead of doing split and join back.Shoat
F
3

You can use \r to change line for the GmailApp \\n to change line for the msgBox.

Foredo answered 12/12, 2021 at 16:9 Comment(1)
This does not provide an answer to the question. Once you have sufficient reputation you will be able to comment on any post; instead, provide answers that don't require clarification from the asker. - From ReviewBridgman
B
2

I usually use table in my mail but I think <br /> should work

Bisitun answered 23/5, 2012 at 13:38 Comment(1)
<br/> or <hr/> depending on the expected behavior.Dissemblance
T
0

For HTML, <br> should work. For javascript/appscript in other google apps like sheetApp,use template literal as MailApp does not respond \n. (i.e) Type the content between two backticks and use 'enter' for new line as follows. No need of using single or double quotes

const recipient = '[email protected],[email protected]'
const sub = 'some subject'
const line1 = 'some line1 content'// say 'Name'
const line2 = 'some line2 content'// say 'Contact Number'
const line3 = 'some line3 content'//say 'EMail Id'
const body = `Name: ${line1}
              Contact Number: ${line2}
              EMail Id: ${line3}`
MailApp.sendEmail(recipient,sub,body)


// There was a mistake with the original answer; I've added "$" for making the code above work.
  

    
Teirtza answered 5/10, 2021 at 0:26 Comment(2)
Why did you include line1, line2, line3 constants? The question contains nothing about them. Why did you mention incorrect "/n" sequence? You should know "\n" instead and it works!Heffron
'\n' only. Edited. for MailApp, Even '\n' doesn't work. With GmailApp, it works. Used line1, line2, line3 as const to show how to use template literal. By mentioning so, one can understand how to use const inside template literal in a single example. With backtick, you don't need to add '\n' or '<br>' and you can just type the next line by typing 'enter'Teirtza
C
0

I try all the answers but they don't work on my computer. I found it better to use GmailApp.sendEmail() instead of MailApp.sendEmail(). It is almost the same, but GmailApp.sendEmail() can change line using \n, while MailApp.sendEmail() can't.

var message = "";
message += "First line of email." + "\n";
message += "Second line of email." + "\n";
message += "Third line of email." + "\n";
message += "Fourth line of email." + "\n";

GmailApp.sendEmail(Session.getActiveUser().getEmail(),"Look at this Title, it work!",message)

This code will send an email to whoever runs this script. It works fine for me.

Cyclone answered 17/10, 2022 at 2:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.