Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

[fix][api] expose html mailing, and default templates to be html mail…

… / fix template state for the pool
  • Loading branch information...
commit 941d0a8ac419dd4f98d5797a1fd29d945321d169 1 parent 2e3e8b9
@bmeck bmeck authored
Showing with 25 additions and 6 deletions.
  1. +25 −6 lib/node_mailer.js
View
31 lib/node_mailer.js
@@ -50,7 +50,25 @@ SMTPClientPool.prototype.addClient = function(port,host,options) {
client.close();})
}
SMTPClientPool.prototype.send = function send(message, callback) {
- this.servers[message.SERVER.host][message.SERVER.user].sendMail(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);
}
function merge(x,y) {
@@ -67,7 +85,6 @@ function merge(x,y) {
var pool = new SMTPClientPool();
exports.send = function node_mail(message, callback) {
-
var server = {
host: message.host,
hostname: message.domain,
@@ -78,7 +95,6 @@ exports.send = function node_mail(message, callback) {
pass: message.password && message.password.toString(),
debug: true
};
-
if(message.username || message.password) {
pool.addClient(server.port, server.host, server);
}
@@ -96,6 +112,7 @@ exports.send = function node_mail(message, callback) {
sender: message.from,
subject: message.subject,
body: message.body,
+ html: message.html,
server: server,
debug: message.debug
})), callback);
@@ -114,7 +131,7 @@ exports.send = function node_mail(message, callback) {
fs.readFile(message.template, function(err, result){
if (err) {
- console.log(err);
+ callback(err);
return;
}
@@ -122,13 +139,14 @@ exports.send = function node_mail(message, callback) {
_templateCache[message.template].loaded = true;
// "Drain" the queue
+ _templateCache[message.template].queue.push(message);
_templateCache[message.template].queue.forEach(function(msg, i){
- msg.body = mustache.to_html(_templateCache[message.template].template, msg.data);
+ msg.html = mustache.to_html(_templateCache[message.template].template, msg.data);
pool.send(new EmailMessage(merge(msg,{
to: msg.to,
sender: msg.from,
subject: msg.subject,
- body: msg.body,
+ html: msg.html,
server: server,
debug: msg.debug
})), callback);
@@ -146,6 +164,7 @@ exports.send = function node_mail(message, callback) {
sender: message.from,
subject: message.subject,
body: message.body,
+ html: message.html,
server: server,
debug: message.debug
})), callback);
Please sign in to comment.
Something went wrong with that request. Please try again.