With Google Apps Script, how to make a line break in a variable to send mail?
In order to add a line break, simply bring up the iOS keyboard, hold down the 'Shift' key and then press the 'Return' key at the same time. Refer to the screenshot below to see which two keys we are talking about here.
LF (character : \n, Unicode : U+000A, ASCII : 10, hex : 0x0a): This is simply the '\n' character which we all know from our early programming days. This character is commonly known as the 'Line Feed' or 'Newline Character'.
If you're not sending an HTML formatted message, use "\n". I personally despise HTML formatted e-mail.
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
});
}
Newline in msgBox
:
Browser.msgBox('line 1 \\n line 2');
Please note you need to escape '\n' with additional backslash.
I usually use table in my mail but I think <br />
should work
You can use \r
to change line for the GmailApp
\\n
to change line for the msgBox.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With