admin管理员组文章数量:1356930
I am using Nodemailer which is a NodeJS module for sending mails. However, it does not send the mails with line breaks but I have tried to print out the variable that contains the text I am trying to send and there are line breaks.
I get the text I write in a text area:
site.js:
$("#send-mail").click(function(){
$.post("/sendMail",{fieldHeader: $('#field-header').val()}, function(data){
window.location.href ="/";
});
});
server.js:
app.post('/sendMail', function(req, res) {
mailSender.init(req.body.fieldHeader);
});
mailSender.js
function init(fieldHeader) {
console.log(fieldHeader);
if(fieldHeader!=null) {
var mailer = require("nodemailer");
var smtpTransport = mailer.createTransport({
host: "smtp.gmail",
service: "Gmail",
tls:{
rejectUnauthorized: false
},
auth: {
user: "...",
pass: "..."
}
});
var mail = {
from: "...",
to: "...",
subject: "Send Email Using Node.js",
text: "Node.js New world for me",
html: fieldHeader
}
smtpTransport.sendMail(mail, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close();
});
}
}
module.exports.init = init;
As I said, I print out the variable that contains the text I want to send in the mail (it's the console.log() you see at the beggining of the mailSender.js
) and this is the result:
But the received mail, does not contain any line break:
I have been thinking about replacing the line breaks with <br>
using a loop that would look and check each character in the text, but I am not sure if it is a good idea.
I am using Nodemailer which is a NodeJS module for sending mails. However, it does not send the mails with line breaks but I have tried to print out the variable that contains the text I am trying to send and there are line breaks.
I get the text I write in a text area:
site.js:
$("#send-mail").click(function(){
$.post("/sendMail",{fieldHeader: $('#field-header').val()}, function(data){
window.location.href ="/";
});
});
server.js:
app.post('/sendMail', function(req, res) {
mailSender.init(req.body.fieldHeader);
});
mailSender.js
function init(fieldHeader) {
console.log(fieldHeader);
if(fieldHeader!=null) {
var mailer = require("nodemailer");
var smtpTransport = mailer.createTransport({
host: "smtp.gmail.",
service: "Gmail",
tls:{
rejectUnauthorized: false
},
auth: {
user: "...",
pass: "..."
}
});
var mail = {
from: "...",
to: "...",
subject: "Send Email Using Node.js",
text: "Node.js New world for me",
html: fieldHeader
}
smtpTransport.sendMail(mail, function(error, response){
if(error){
console.log(error);
}else{
console.log("Message sent: " + response.message);
}
smtpTransport.close();
});
}
}
module.exports.init = init;
As I said, I print out the variable that contains the text I want to send in the mail (it's the console.log() you see at the beggining of the mailSender.js
) and this is the result:
But the received mail, does not contain any line break:
I have been thinking about replacing the line breaks with <br>
using a loop that would look and check each character in the text, but I am not sure if it is a good idea.
- Possible duplicate of New line in node.js mailer template – Manjeet Singh Commented Oct 16, 2019 at 11:34
- Maybe try $('#field-header').html() instead of .val() – Untimely Answers Commented Oct 16, 2019 at 11:49
- I've tried with .hml() and apparently it only gets its value with .val(). But I've managed to solve it. I was sending the text in the html property but I deleted that and I put it in the text property instead and it works now. Thanks anyway. – user10021033 Commented Oct 16, 2019 at 11:59
2 Answers
Reset to default 5Try adding break tags where you want line breaks. <br>
or <br />
var fieldheader = `hi <br> how are you ? <br> this is testing <br>`
var mail = {
from: "...",
to: "...",
subject: "Send Email Using Node.js",
text: "Node.js New world for me",
html: fieldHeader
}
It will solve your problem.
Just put \n
where you want a line break.
本文标签: javascriptNodemailer does not send the text with line breaksStack Overflow
版权声明:本文标题:javascript - Nodemailer does not send the text with line breaks - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744034832a2579575.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论