0

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?

1

1 Answer 1

3

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

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: "[email protected]",
                    subject: "My Subject",
                    html: body
                }, next);
            },
            function(info,next) {
                mailer.close(next);
            }
        ], callback);
}

sendEmail("[email protected]", function() {
    console.log("email sent"); 
});
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.