javascript - 如何使用 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;
}
});
transporter 定义如下:
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,
},
});
解决方案
好的..所以事实证明这是一个发送 HTML 或文本的 nodemailer 选项:
var mailOptions = {
from: from,
to: to,
subject: subject,
text: body,
html: body, // this fixes it !
};
推荐阅读
- json - Flutter 将 JSON 流 List 转换为 Map 对象
- blockchain - 找不到构建 go-ethereum 的软件包
- ios - iOS:使用 C/C++ 将文件写入 Documents 文件夹并使用 Files/iTunes 或第三方工具轻松检索
- php - 如何在laravel中插入一对多关系?
- angular - ng2-charts 在 ng serve 上失败
- node.js - 观看完成的视频编码的最佳做法是什么?
- bash - 合并多个文件的列,同时根据不同的列匹配顺序并为缺失值添加 0
- python - 什么是异步事件循环的独立于平台的 mypy 类型注释?
- sql - 我想通过选择用户在 Laravel 中输入的 id 来组合两个表
- postgresql - Postgres 添加月份至今