如何使用Javascript在电子邮件中格式化HTML



我有一个javascript函数(服务器端(可以发送电子邮件。我希望电子邮件的正文是HTML的,这样它看起来比文本好看一点。

这是创建电子邮件的代码

sendTestEmail = (to) => {
var subject = 'Email Test';
var body = '';
body = '<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">';
body += '<html>';
body += '<head>';
body += '<meta http-equiv="content-type" content="text/html; charset=ISO-8859-1">';
body += '</head>';
body += '<body bgcolor="#ffffff" text="#000000">';
body += 'This should be body text <br>';
body += 'More body text <br>';
body += 'Even more body text<br>';
body += '<br>';
body += 'something that needs white space separation <br>';
body += '<br>';
body += '<br>';
body += '<div class="moz-signature"><i><br>';
body += 'signature<br>';
body += '</i></div>';
body += '</body>';
body += '</html>';
return send(to, subject, body);
};

当我拿到它时,它看起来是这样的:

-----Original Message-----
From: (removed) 
Sent: Monday, November 9, 2020 9:31 AM
To: (removed)
Subject: Email Test
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"><html><head><meta http-equiv="content-type" content="text/html; charset=ISO-8859-1"></head><body bgcolor="#ffffff" text="#000000">This should be body text <br>Even more body text<br><br>something that needs white space separation<br><br><br><div class="moz-signature"><i><br>signature<br></i></div></body></html>

这是我的"发送"功能中的代码

var mailOptions = {
from: from,
to: to,
subject: subject,
text: body,
};
transporter.sendMail(mailOptions, function (error, info) {
if (error) {
errors.message = error.message;
console.log(msgPrefix, 'ERROR', JSON.stringify(errors));
return errors;
} else {
errors.code = 200;
errors.data = info.response;
console.log(msgPrefix, 'Response', info.response);
return errors;
}
});

运输工具的定义如下:

const nodemailer = require('nodemailer');
const transporter = nodemailer.createTransport({
host: email_host,
secureConnection: email_secureConnection,
requireTLS: email_requireTLS,
port: email_port,
tls: {
ciphers: email_cipher,
},
auth: {
user: email_user,
pass: email_password,
},
});

好的。。因此,它是一个nodemailer选项来发送HTML或文本:

var mailOptions = {
from: from,
to: to,
subject: subject,
text: body,
html: body, // this fixes it !
};

相关内容

  • 没有找到相关文章

最新更新