With Google Apps Script, how to make a line break in a variable to send mail?
If you're not sending an HTML formatted message, use "\n". I personally despise HTML formatted e-mail.
Newline in msgBox
:
Browser.msgBox('line 1 \\n line 2');
Please note you need to escape '\n' with additional backslash.
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
});
}
___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 You can use \r
to change line for the GmailApp
\\n
to change line for the msgBox.
I usually use table in my mail but I think <br />
should work
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.
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.
© 2022 - 2024 — McMap. All rights reserved.