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
});
}
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.
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 = 'abc#xyz.com,cde#xyz.com'
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.
Related
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
});
}
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.
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 = 'abc#xyz.com,cde#xyz.com'
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.
i am trying to send mails to some list of user using google apps-script.
I am taking the first draft mail and then mailing it to list of users present in the spreadsheet. But when i use ".getplainbody(); " function. It only copies the plain text in the draft.
function sendmail()
{
var drafts = GmailApp.getDraftMessages();
Logger.log(drafts.length);
var draft = drafts[0].getPlainBody();
Logger.log(draft);
GmailApp.sendEmail('abc#gmail.com', 'subject', 'Hello' + '\n ' + draft);
}
I have also tried using the getbody() and then html with the message.
function sendmail()
{
var drafts = GmailApp.getDraftMessages();
Logger.log(drafts.length);
var draft = drafts[0].getBody();
Logger.log(draft);
GmailApp.sendEmail('abc#gmail.com', 'subject', 'Hello' + '\n ' + {html: draft});
}
But this also gives me "[object Object]" in the inbox.
Is there any other option to send the draft mail (not in plain text format)with proper format.
Thanks
Try: GmailApp.sendEmail('abc#gmail.com', 'subject', 'plaintext body', {htmlBody: draft});
You are missing the fourth parameter, 'Hello' + '\n ' + {html: draft} actually concatenates Hello \n with the {html: draft} object.
I am using google scripts to send text messages. I would like to use the newline character to format messages properly. I would expect the code below to display:
Hello.
How are you?
Instead, the following is displayed:
Hello.How are you?
var response = "Hello.\nHow are you?"
MailApp.sendEmail(recipient, subject, response);
Please note recipient is in the format:
"(956) xxx-xxxx" <1740xxxxxxx.1956xxxxxxx.dhJVMRb1ZG#txt.voice.google.com>
That is, this is being sent as sms via gmail.
UPDATE, I tried:
function myFunction()
{
GmailApp.sendEmail(
'(956) xxx-xxxx <1262xxxxxxx.1956xxxxxxx.7ljPdnVXKg#txt.voice.google.com>',
'test mailApp', 'test3', { htmlBody: "break3<br />line"});
}
and the result was:
test3
which tells me that any email sent to txt.voice.google.com is processed as text only, not html. I hope there's some workaround.
jimpudar's answer wont work!
The text of email will be "Hello.How are you?"
Instead use this:
GmailApp.sendEmail("me#gmail.com", "Email", "This won't be displayed", { htmlBody:
"Hello</br>How are you?"
}
);
Or you can do:
var recipient = "example#gmail.com";
var subject = "This email";
var body = "Hello.</br>How are you?";
GmailApp.sendEmail(recipient, subject, "This won't be displayed",{htmlBody:
body
}
);
Both will work ;)
As you maybe noticed, you can use any html code inside the {htmlBody: whatever}
Good luck, and have fun with scripting :)
I am not 100% sure on this, but I seem to remember using HTML tags to accomplish this.
var response = "Hello.<br>How are you?";
This probably won't work if your email is being sent as text. It worked for me when sending the text as an HTML email.
I have set up a script to notify users when a new entry has been submitted to a shared spreadsheet. I am trying the following, but the emails are getting sent as plain text:
function formSubmitReply(e) {
var emailAddress = ' *email addresses* ';
var URL = ' *URL to send as link* ';
var message = "<HTML><BODY>"
+ "<P>" + " A new guest complaint has been entered into the database."
+ '<P>To view the spreadhseet, <A HERF="' + URL + '">click here</A>.'
+ "</HTML></BODY>";
MailApp.sendEmail(emailAddress, "New Guest Complaint", "", {htmlBody: message});
}
Any suggestions?
See the sample in the official docs, where it uses the htmlBody parameter: https://developers.google.com/apps-script/reference/mail/mail-app#sendEmail(Object)
You are including html and body tags when the parameter expects the body html not an html page. Remove those tags. Also, note that your tags at the end are reversed (/html then /body)
I am getting the data from a form and writing in a google Document. It has fields like Name, Age, Address and so on. Address is of type paragraph text. My question is that while writing the data in a google Doc through a on-submit trigger script, how do I remove manual line breaks in the Address as input by the person filling the form. i.e. I want address to be a continuous string, without the breaks that the person filling the form may have put while typing his address.
ok here are more details.. the Address field in my form is called 'Address', i have a onformsubmit triggered script that reads all the data entered by the person in the form and puts in a google Doc App.
i read the values typed in the form as :
for(var field in e.namedValues) {
message += field + ' :: ' + e.namedValues[field].toString().replace("\n",", ") + "\n";
}
But in my google doc, the lines for the value of Address sre still broken where the person filling the form has broken the lines. i want to eliminate these line breaks and substitute it with commas so the address is continuous.
Change part of the line with the replace function to
e.namedValues[field].toString().replace(/(\r\n|\n|\r)/gm, ", ");
It works!
Remove the line breaks by taking the string that has the text from the paragraph, and passing it through string.Replace("\n", ""); If that doesn't work, then you will need to provide more details.
Also you can work with the range class:
function editRow(r){
var sheet = SpreadsheetApp.getActiveSheet();
var relevantRange = sheet.getRange(r, 21, 1, 1);
artefactRange.setValue(relevantRange.getValue().replace(/(\n)/g, ', '));
}