首页 > 解决方案 > 带有 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);
        }
    });
}

            

标签: htmlcssnode.jshandlebars.jsnodemailer

解决方案


推荐阅读