2015-03-11 5 views
0

Можно отправить строку html по почте с помощью nodemailer и что ваш почтовый клиент или служба веб-почты отображают его не как строку, а как html?Отправка HTML-адреса электронной почты с помощью Node.js

+0

В этом вопросе есть пример, на который я ответил некоторое время назад: https://stackoverflow.com/questions/21654051/how-to-send-an-html-page-as-email-in-nodejs/21654249#21654249 – Ryan

ответ

2

Вот код, я использовал в прошлом ...

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"); 
}); 
Смежные вопросы