Take the 2-minute tour ×

First time I put up a question here (been searching before posting) so please bear with possible mistakes done on my end.

To the problem: I am currently working on a website with google sites. Made some forms there and am adding a script to those forms to get the info input there emailed away once the form is submitted and saved on the spreadsheet, which works just fine, but the message inside the email that arrives is pretty messed up.

All the "\n" expressions in the code get simply ignored. I got the base for the code from www.labnol.org and just edited it a little.

For the start, 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 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 have been trying around a lot to place the \n in different places as alternative, though it gets ignored no matter where I place it.

the original loop looked like this:

  for(var i in headers)

Before I did some modifications to the code it worked just fine. But even though I didn't touch these lines, nor any of the other parts of the code that contribute to getting the message, the \n stopped working. I kept trying to fix it (as the messed up \n placing above shows), but without success.

So now I am trying to find a way to fix them, or at least a work-around and hoped that any of you might know what is going on with the \n's or how to get them working again.

Thanks in advance.

ps: if you need any more information on it, just let me know

share|improve this question

2 Answers 2

up vote 3 down vote accepted

A solution is to use the GmailApp.sendEmail method instead of the MailApp.sendEmail method. The GmailApp's sendMail handles \n correctly.

share|improve this answer
thanks a lot megabyte, this solution works for the \n problem, only thing left now is that, with GmailApp.sendEmail, the 4th argument (which should be the replyTo) is not yet working. I think I will have to specify it somehow... or rather, make an object for it, but I'm afraid I have no idea how to do the initializing of it in google script. –  m41n Oct 9 '12 at 13:06
@m41n. The 4rd argument is optional and defines not only the replyTo address but also other parameters like cc, bcc, etc. It is demonstrated in the examples how to specify these parameters. For the replyTo, the 4rd argument should looks like { replyTo : "me@example.com" } –  megabyte1024 Oct 9 '12 at 13:37
thanks a lot once more! that's all needed, things work just as planned now. marked your answers as the accepted one (since it after all gave me just the help I needed). If there's anything else for me to do to mark my question as answered, just tell me. Want to do things probably after all =) –  m41n Oct 9 '12 at 13:50

Here's an alternate solution to what you're doing: HtmlTemplate

Some sample code:

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's the corresponding template code in body.html (Click "File -> New -> Html File"):

 Body goes here
<?= someValue ?>
share|improve this answer

Your Answer


