问题
I'm using node.js mailer module to send email from node server. I used template file for that but I got all spaces trimmed when the message was sent. I tried to add '\n' at the end line of the template but it doesn't work also. Here is an example of template file:
Hi {{username}},\\n
Thanks for creating an account with us
I tried \n and \n , nothing all doesn't work. Any help?
Thanks,
回答1:
It's a known behavior, mustache is made to work with HTML templates, in HTML, new lines will be contracted as one space only.
You can try doing something like {{new_line}}
instead your \\n
, and define new_line: "\n\xA0"
in your datas. \xA0
is the non-breakable space, it can separate line if you want to make two new lines.
An other solution is to not use the template, but just get the content of the file as text with fs.readFileSync(filename)
and use a regexp to replace {{xxx}}
by an object content.
回答2:
nodemailer
supports two types of templates, i.e. text
and html (or templateFn() - case of loopback)
.
The option text
is used for email clients which don't support HTML rendering so that \n
should be used in this option for a new line.
On the contrary, you should swap \n
with <br>
in the option html
.
Hope this would help you.
来源:https://stackoverflow.com/questions/10226390/new-line-in-node-js-mailer-template