Alternative or fix for "\ n" in google script

The first time I asked a question here (I did a search before publishing), so please bear with possible errors made at my end.

To the problem: I am currently working on a site with google sites. Made some forms there, and I add a script to these forms to get the information that was sent there by email, after the form was submitted and saved in a spreadsheet, which works very well, but the message inside the email is pretty confused.

All expressions "\ n" in the code are simply ignored. I got the code base from www.labnol.org and edited it a bit.

To get started, the code:

function sendFormByEmail(e) { //I took the two mail addresses out here, but they are working in the original var email = "first mail address"; var email2 = "second mail address"; var subject = "New Announce your visit form submitted"; var s = SpreadsheetApp.getActiveSheet(); var headers = s.getRange(1,1,1,s.getLastColumn()).getValues()[0]; var message = "A new 'Announce your visit' form has been submitted on the website: \n\n" + "\n\n"; for(var i in headers) { message = message + "\n \n"; message += headers[i] + ' = '+ e.namedValues[headers[i]].toString() + "\n\n"; } var senderEmail = e.namedValues[headers[6]].toString(); MailApp.sendEmail(email, senderEmail, subject, message); MailApp.sendEmail(email2, senderEmail, subject, message); } 

As you can see, I tried a lot to place \ n in different places as an alternative, although it is ignored no matter where I place it.

The original loop looked like this:

  for(var i in headers) message += headers[i] + ' = '+ e.namedValues[headers[i]].toString() + "\n\n"; 

Before I made some changes to the code, it worked perfectly. But even if I did not touch these lines or other parts of the code that contribute to receiving the message, \ n stops working. I tried to fix it all the time (as I mixed up \ n to post above the show), but to no avail.

So now I'm trying to find a way to fix them, or at least workaround and hope that any of you can know what is happening with \ n or how to make them work again.

Thanks in advance.

ps: if you need more info on this just let me know

+4
source share
2 answers

The solution is to use the GmailApp.sendEmail method instead of MailApp.sendEmail . GmailApp sendMail correctly processes \n .

+4
source

Here's an alternative solution to what you are doing: HtmlTemplate

Code example:

 function sendEmailWithTemplateExample() { var t = HtmlService.createTemplateFromFile("body.html"); t.someValue = "some dynamic value"; var emailBody = t.evaluate().getContent(); MailApp.sendEmail(" your@email.here ", "test email", emailBody); } 

And here is the corresponding template template in body.html (click "File → Create → HTML File"):

  Body goes here <?= someValue ?> 
+2
source

Source: https://habr.com/ru/post/1438624/


All Articles