html - 带有 CSS 的外部 HTML 文件作为带有 Styliner 的 nodemailer 中的电子邮件内容
问题描述
我正在尝试使用 Styliner 和 Nodemailer 将一些外部 CSS 文件和相对路径嵌入到 HTML 电子邮件模板中,但我无法使其工作。
我按照这个例子,但仍然没有工作。问题是我没有得到内联 CSS 或绝对路径。就像样式器功能之前的 HTML。
我的应用程序结构是:
/myapp
/public
/css
style.css
/img
logo.png
/routes
mail.js
/views
mail.html
这是我的一些相关代码:
邮件.html:
<!DOCTYPE html>
<html>
<head>
...
<link rel="stylesheet" href="../public/css/style.css">
</head>
<body>
...
<img src="../public/img/logo.png">
</body>
</html>
邮件.js:
// prependUNCPath is a function called by Styliner for every
// link that is found in the HTML.
function prependUNCPath(path, type) {
return __dirname + path;
}
function trasportEmail (html, opt) {
// See http://styliner.slaks.net/#about for Styliner options
const options = { url: prependUNCPath, noCSS : true };
const styliner = new Styliner(path.join(__dirname, '/../views'), options);
// Do the reading of the original index.html, and kick everything off.
fs.readFile(__dirname + `/../views/${html}.html`, 'utf8', function (err, data) {
if (err) {
return console.error(err);
}
const htmlTemplate = handlebars.compile(data);
styliner.processHTML(data).then(function (processedSource) {
sendEmail(processedSource, opt);
});
});
}
function sendEmail(source, opt) {
const mailOptions = {
from: opt.from`,
to: opt.to,
subject: opt.subject,
html: source
}
let smtpTransport = nodemailer.createTransport(senderEmailOptions);
// send mail with defined transport object
smtpTransport.sendMail(mailOptions, function (error, info) {
if (error) {
console.log(error);
} else {
console.log('Message sent: ' + info.response);
}
});
}
解决方案
推荐阅读
- html - 从另一个位置获取跨度数据时覆盖跨度标记
- vba - Excel VBA代码将表单中的列复制粘贴到合并电子表格中的行
- java - 黑屏安卓应用
- python - AttributeError:“NoneType”对象在 Flask、SQLAlchemy 中没有属性“time_recorded”
- regex - 弹性搜索的错误正则表达式查询
- api - 带有 HTTPURLConnection 的 Docker 容器之间的 400 错误 HTTP GET 请求
- tensorflow-serving - TensorFlow Serving mobilenet_1.0_224_quant
- android - 如何从json中删除html标签?
- c# - asp.net core 2 身份“记住我”不起作用
- python - Scipy 树状图叶节点排序