使用Node.js发送HTML电子邮件

时间:2015-03-11 17:58:30

标签: node.js nodemailer

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

1 个答案:

答案 0 :(得分:3)

以下是我过去使用的一些代码......

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"); 
});
相关问题