node_mailer/lib/node_mailer.js at master · Marak/node_mailer · GitHub

Sign up Sign in

- Explore
- Features
- Enterprise
- Blog

This repository
This repository
All repositories

- Star 386
- Fork 70

# public Marak / node_mailer

- Code
- Issues 33
- Pull Requests 11

- Pulse
- Graphs
- Network

### HTTPS clone URL

### Subversion checkout URL

You can clone with HTTPS, or Subversion.

Clone in Desktop Download ZIP

Show File Finder
branch: master
Switch branches/tags

- Branches
- Tags

0.3.0
debug-option
master
smtp-fix
Nothing to show
v0.6.7
Nothing to show
node_mailer / lib / node_mailer.js
shripadk September 19, 2011
fixed HTML template issue (templates from cache not sent as HTML). si…

10 contributors

## Users who have contributed to this file

- Marak
- bmeck
- indutny
- tmpvar
- bentomas
- saikat
- deepthawtz
- chapel
- deitch
- shripadk

file 162 lines (147 sloc) 5.657 kb
Open Edit Raw Blame History
Delete

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161

    /* Copyright (c) 2009-2010 Marak Squires, Elijah Insua, Fedor Indutny - http://github.com/marak/node_mailer Permission is hereby granted, free of charge, to any personobtaining a copy of this software and associated documentationfiles (the "Software"), to deal in the Software withoutrestriction, including without limitation the rights to use,copy, modify, merge, publish, distribute, sublicense, and/or sellcopies of the Software, and to permit persons to whom theSoftware is furnished to do so, subject to the followingconditions: The above copyright notice and this permission notice shall beincluded in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIESOF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE ANDNONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHTHOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISINGFROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OROTHER DEALINGS IN THE SOFTWARE.*/
    var SMTPClient = require("nodemailer").SMTPClient;var EmailMessage = require("nodemailer").EmailMessage;var fs = require('fs');var mustache = require('../vendor/mustache');var _templateCache = {};
    function SMTPClientPool() {  this.servers = {};}SMTPClientPool.prototype.addClient = function(port,host,options) {  if(this.servers[host] && this.servers[host][options.user]) return;  var hostClients = this.servers[host] || (this.servers[host] = {});  var pool = this;  var client = hostClients[options.user] = new SMTPClient(host,port,options);  client.on("close",function() {    if(client == hostClients[options.user]) {      //only because this could be crazy long lived and dynamic      delete hostClients[options.user];      if(Object.keys(hostClients).length == 0) {        delete pool.servers[host]      }    }  })  client.on("empty",function(){        delete hostClients[options.user];        client.close();})}SMTPClientPool.prototype.send = function send(message, callback) {  var hostpool = this.servers[message.SERVER.host]  if(!hostpool) hostpool = {};  var client = hostpool[message.SERVER.user]  if(!client) {    client = hostpool[message.SERVER.user] = new SMTPClient(message.SERVER.host,message.SERVER.port,message.SERVER);    client.on("close",function() {      if(client == hostClients[options.user]) {          //only because this could be crazy long lived and dynamic          delete hostClients[options.user];          if(Object.keys(hostClients).length == 0) {       delete pool.servers[host]          }        }    })    client.on("empty",function(){     delete hostClients[options.user];     client.close();})  }  client.sendMail(message,callback);  client.on('error', callback);}
    function merge(x,y) {  var z = {};  for(var k in x) {    z[k] = x[k];  }  for(var k in y) {    z[k] = y[k];  }  return z;}
    var pool = new SMTPClientPool();
    exports.send = function node_mail(message, callback) {  var server = {    host: message.host,    hostname: message.domain,    port: + message.port,    use_authentication: message.authentication,    ssl: message.ssl,    user: message.username && message.username.toString(),    pass: message.password && message.password.toString(),    debug: message.debug || false  };  if(message.username || message.password) {    pool.addClient(server.port, server.host, server);  }
      function dispatchMail(message, server, callback) {      var _message = {          to: message.to,          sender: message.from,          subject: message.subject,          server: server,          debug: message.debug      };      if(message.html)_message.html = message.html;      pool.send(new EmailMessage(merge(message, _message)), callback);  }
      // If a template was passed in as part of the message  if (message.template) {    // If the template path is in the cache    if (_templateCache[message.template]) {      // If the template is already fully loaded in the cahe      if (_templateCache[message.template].loaded) {        // Use the cached template and send the email        message.html = mustache.to_html(_templateCache[message.template].template, message.data);        dispatchMail(message, server, callback);      }      else {        // We've started to load the template, but it's not loaded yet. queue up this message to be sent later        _templateCache[message.template].queue.push(message);      }    }    else {      // The template path wasn't found in the cache, start to load the template      _templateCache[message.template]          = {};      _templateCache[message.template].loaded   = false;      _templateCache[message.template].template = '';      _templateCache[message.template].queue    = [];
          fs.readFile(message.template, function(err, result){        if (err) {          callback(err);          return;        }
            _templateCache[message.template].template = result.toString();        _templateCache[message.template].loaded   = true;
            // "Drain" the queue        _templateCache[message.template].queue.push(message);        _templateCache[message.template].queue.forEach(function(msg, i){          msg.html = mustache.to_html(_templateCache[message.template].template, msg.data);          dispatchMail(msg, server, callback);        });
            // Clear the queue out        _templateCache[message.template].queue = [];      });    }  }  else { // No template being used    dispatchMail(message, server, callback);  }};

Jump to Line

- Status
- API
- Training
- Shop
- Blog
- About

- © 2014 GitHub, Inc.
- Terms
- Privacy
- Security
- Contact

Something went wrong with that request. Please try again.