【问题标题】:External HTML file with CSS as email content in nodemailer带有 CSS 的外部 HTML 文件作为 nodemailer 中的电子邮件内容
【发布时间】:2014-11-18 06:34:08
【问题描述】:

有谁知道如何告诉 Node.js 模块 nodemailer 使用外部 HTML 文件(带有样式表链接)作为电子邮件内容?

我可以输入 HTML 标记作为电子邮件内容,但我更喜欢加载完整的 HTML 文件。 我正在使用 nodemailer 1.3.0。

不幸的是,对于实际的 nodemailer 版本,我找不到任何涵盖此问题的示例。

想法?

【问题讨论】:

    标签: html node.js nodemailer


    【解决方案1】:

    使用http://styliner.slaks.net

    它是一个 node.js 包,允许你内联你的 css,这样你就可以使用 nodemailer 发送完整的 HTML 电子邮件。

    它还可以在您的电子邮件中添加/更新链接,使它们不再是相对链接。 (即,您可以将“coverage/local.html”之类的链接更改为“\myhostname\afolder\coverage\local.html

    这是一个粗略且现成的脚本,我敲出它以通过电子邮件发送伊斯坦布尔代码覆盖率的输出。

    /*
     * Use this script to send Istanbul code coverage results via email.
     * 
     * NPM REQUIREMENTS:
     *  The following packages need to be installed:
     *   - npm install styliner
     *   - npm install nodemailer
     *   - npm install nodemailer-smtp-transport
     * 
     * Usage: node sendNodeJSCoverageEmail.js <origfile> <basedir> <receipient>
     * 
     *  <origfile>  : This is the original Istanbul index.html from the code coverage report
     *  <basedir>   : TeamCity directory where the index.html resides. Must contain "DevTC" folder (see below)
     *  <recipient> : Email address to send the results to. 
     * 
     * What it does:
     *  Istanbul creates an html report for code coverage. It is a complete web page, and has an index.html,
     *  some CSS style sheets, and relative links to other coverage.
     *  
     *  This script takes the index.html, reads the referenced .css files, and inlines the css, so that external
     *  files are no longer needed. (this is because you can't send multiple files for an html email).
     *  Also, the relative links are prepended with the UNC path to the files: this means that the recipients 
     *  can click on the links in the html email and they will work. 
     *  
     *  NOTE: it assumes the TeamCity folder where the coverage reports reside is shared, and also contains the 
     *        folder DevTC (it looks for this folder to do the substitution). All build machines have this 
     *        folder structure. 
     *        
     */
    
    var nodemailer = require('nodemailer');
    var os = require("os");
    var hostname = os.hostname();
    
    var originalFile = process.argv[2].toString();
    var baseDir = process.argv[3].toString();
    var recipient = process.argv[4].toString();
    
    var Styliner = require('styliner');
    
    
    var uncDrive = '\\\\' + hostname + '\\DevTC';
    var uncPath = baseDir.replace(/.*DevTC/gi, uncDrive);
    
    
    // prependUNCPath is a function called by Styliner for every
    // link that is found in the HTML.
    function prependUNCPath(path, type) {
        return uncPath + path;
    }
    
    // See http://styliner.slaks.net/#about for Styliner options
    var options = { url : prependUNCPath, noCSS : true };
    var styliner = new Styliner(baseDir, options);
    
    function sendEmail(source) {
    
        var nodemailer = require('nodemailer');
        var smtpTransport = require('nodemailer-smtp-transport');
    
        // create reusable transporter object using SMTP transport
        var transport = nodemailer.createTransport(smtpTransport({
            host: 'your-smtp-server.local',  // FIXME: Change this!
            port: 25,
        }));
    
    
        // setup e-mail data with unicode symbols
        var mailOptions = {
            from: 'TeamCity <teamcity@company.com>', // sender address
            to: recipient, // list of receivers
            subject: 'Code Coverage results', // Subject line
            // text: 'Hello world ?', // plaintext body, not used
            html: source // html body
        };
    
        // send mail with defined transport object
        transport.sendMail(mailOptions, function (error, info) {
            if (error) {
                console.log(error);
            } else {
                console.log('Message sent: ' + info.response);
            }
        });
    
    
    }
    
    
    var fs = require('fs')
    
    // Do the reading of the original index.html, and kick everything off.
    fs.readFile(originalFile, 'utf8', function (err, data) {
        if (err) {
            return console.log(err);
        }
    
        styliner.processHTML(data)
        .then(function (source)
             {
    
            sendEmail(source);
    
            fs.writeFile("newindex.html", source, function (err) {
                if (err) {
                    return console.log(err);
                }
    
                console.log("The file was saved!");
            });
    
          }
    
        );
    
    });
    

    【讨论】:

    • 这应该被标记为答案。 +1 解决方案。
    【解决方案2】:

    您应该使用内联 css,而不是单独的文件,因为 head 元素的 HTML 呈现等是由提供者而不是由 nodemailer 定义的。

    我推荐mailChim's css inliner tool,它很方便,只需设置

    html: '<div style="font - family: verdana; max-width:500px; margin-left">' + 'more string and string'
    

    【讨论】:

    • mailchimp 刚刚救了我。非常感谢。
    【解决方案3】:

    我建议使用 fs 模块的 readFile 函数读取您的 HTML 文件,然后在回调中通过电子邮件发送。

    readFile 教程:http://docs.nodejitsu.com/articles/file-system/how-to-read-files-in-nodejs

    【讨论】:

    • 我只能获取 HTML 文件作为附件,但不能将其内容用作电子邮件内容。
    • 我试过你描述的方法。使用 fs 我可以将我的 HTML 文档的内容存储在一个变量中。但我不能将此变量分配给 nodemailer 的 mailOptions。 html : HTMLContent 不起作用。下一步该做什么?
    • 它应该可以正常工作。您是否仔细检查过您的 HTMLContent 变量是否包含正确的内容?你把它放在你的 mailOptions 的“html”属性中,而不是“text”属性中,对吧?如果您确定这些并且仍然无法正常工作,最后的努力是尝试在您的 mailOptions 中设置 {"Content-type": "text/html"} 的标题(请参阅“标题”的文档选项了解更多信息),看看是否有帮助。
    • 我尝试了很多选择,但都没有成功。但是我不知道你的意思是什么我应该在我的 mailOptions 中设置 {"Content-type": "text/html"} 的标题。你有具体的例子吗?顺便说一句,您提到的“标题”选项的文档在哪里?有链接吗?
    • mailOptions 是您传递给 sendMail() 函数的对象的示例名称,其中包含其所有选项值,例如 to、from、subject、html 等。官方的 nodemailer 文档将其解释为以及添加自己的标题:github.com/andris9/Nodemailer
    猜你喜欢
    • 1970-01-01
    • 2014-07-13
    • 1970-01-01
    • 2012-10-21
    • 2021-10-15
    • 2012-08-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多