使用 Node.js 发送 HTML 电子邮件 [英] Sending HTML email with Node.js

查看:77
本文介绍了使用 Node.js 发送 HTML 电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

可以使用 nodemailer 通过邮件发送 html 字符串,并且您的邮件客户端或 webmail 服务将其呈现为 html 而非字符串?

It is possible to send an html string via mail with nodemailer and that your mail client or webmail service renders it not as a string but as an html?

推荐答案

这里是我过去用过的一些代码...

Here is some code I have used in the past...

email.mustache

email.mustache

    <!DOCTYPE html>
<html lang="en-US">
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    </head>
    <body>
        hello {{{key}}}
    </body>
</html>

email.js

    var async = require("async"),
    nodemailer = require("nodemailer"),
    url = require("url"),
    _ = require("underscore");


module.exports = function (uri) {

    var api = Object.create(null),
        requiredEmailAttributes = [ "to", "from", "subject", "html" ],
        transport,
        options;

    function parseEmailURI(uri) {
        var options = null,
            urlParsed,
            authParts;

        urlParsed = url.parse(uri);
        if(urlParsed.protocol === "smtp:") {
            options = {};
            options.host = urlParsed.hostname;
            if(urlParsed.port) {
                options.port = urlParsed.port;
            }
            if(urlParsed.auth) {
                authParts = urlParsed.auth.split(":");
                if(authParts.length === 2) {
                    options.auth = { user: authParts[0], pass: authParts[1] };
                } else {
                    options = null;
                }
            }
        }
        return options;
    }

    if(!uri) {
        options = {};
    } else {
        options = parseEmailURI(uri);
    }
    if(!options) {
        return null;
    }

    transport = nodemailer.createTransport("SMTP",options);


    function requireAttributes(message, callback) {
        async.eachSeries(requiredEmailAttributes, function (name, next) {
            if ((message[name] === undefined)  ||
                (message[name] === null)) {
                next(new Error("The " + name + " attribute is required "+
                                "to be set and non-null"));
                return;
            }

            next();
        }, callback);
    }

    /*
        message format {to,from,subject,html}
    */
    api.sendMail = function(message,callback) {
        async.waterfall(
            [
                requireAttributes.bind(undefined, message),
                function (next) {
                    transport.sendMail( _.clone(message), next);
                }
            ],
            callback
        );
    };

    api.close = function(callback) {
        transport.close(callback);
    };

    return api;
};

app.js

    var email = require("./email"),
    fs = require("fs"),
    Hogan = require("hogan.js"),

var emailTemplate = "./email.mustache",
    mailer;

function sendEmail(to, callback) {
    async.waterfall(
        [
            function (next) {
                emailTemplate = result;
                fs.readFile(emailTemplate, "utf8", next);
            },
            function (templateData, next) {
                var template,
                    body;

                template = Hogan.compile(templateData);
                body = template.render({key: "value"});

                mailer = email(config.emailURI());
                mailer.sendMail({
                    to: to,
                    from: "me@company.com",
                    subject: "My Subject",
                    html: body
                }, next);
            },
            function(info,next) {
                mailer.close(next);
            }
        ], callback);
}

sendEmail("person@xyz.com", function() {
    console.log("email sent"); 
});

这篇关于使用 Node.js 发送 HTML 电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆