Skip to content

Commit

Permalink
[api] connection pooling per server and port seems to be working now.
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Sep 14, 2010
1 parent 67c2ee8 commit 9309ac3
Show file tree
Hide file tree
Showing 2 changed files with 90 additions and 31 deletions.
16 changes: 16 additions & 0 deletions demo.js
Expand Up @@ -18,5 +18,21 @@ for(var i = 0; i < 5; i++){
});


email.send({
host : "localhost", // smtp server hostname
port : "1026", // smtp server port
domain : "localhost", // domain used by client to identify itself to server
to : "marak.squires@gmail.com",
from : "obama@whitehouse.gov",
subject : "node_mailer test email 2",
body : "hello this is a test email from the node_mailer",

authentication : "login", // auth login is supported; anything else is no auth
username : "dXNlcm5hbWU=", // Base64 encoded username
password : "cGFzc3dvcmQ=", // Base64 encoded password

});


}

105 changes: 74 additions & 31 deletions lib/node_mailer.js
Expand Up @@ -25,38 +25,50 @@ OTHER DEALINGS IN THE SOFTWARE.

var tcp = require('net'),
sys = require('sys'),
colors = require('colors');
colors = require('colors'), eyes = require('eyes');

var email = {

connections: [],
connections: {},

getConnection: function (options, callback) {

var key = options.host + ':' + options.port;
// perform lookup to determine if connection already exists for server / port

// is a connection for this host / port pair already defined ?
if(typeof email.connections[ key ] == 'undefined'){
sys.puts('no connection found for: ' + key.yellow);
email.createConnection(options.port, options.host, function(connection){
var key = options.host + ':' + options.port;
email.connections[ key ] = {};
email.connections[ key ].connection = connection;
callback(connection);
});
//sys.puts('no connection found for: ' + key.yellow);

email.connections[ key ] = {};
email.connections[ key ].queue = [];
email.connections[ key ].ready = false;

(function( key ){
email.createConnection(options.port, options.host, function(connection){
email.connections[ key ].connection = connection;

callback(connection);
});
})( key );

}
else{
sys.puts('connection found for: ' + key.yellow)
//sys.puts('connection found for: ' + key.yellow);
callback(email.connections[ key ]);
}
},
createConnection: function (port, host, callback) {

var key = host + ':' + port;

var connection = tcp.createConnection(port, host);
connection.setEncoding('utf8');

connection.addListener("connect", function () {
callback(connection);
//sys.puts('got connected');
email.connections[ key ].ready = true;
email.drainQueue( connection, key );
//callback(connection);
});

connection.addListener("end", function() {
Expand All @@ -76,7 +88,7 @@ var email = {

},
send: function (options) {

//sys.puts('send()');
// setup some default config options
var options = typeof(options) == "undefined" ? {} : options;
options.to = typeof(options.to) == "undefined" ? "marak.squires@gmail.com" : options.to;
Expand All @@ -89,30 +101,61 @@ var email = {

email.getConnection(options, function(connection){

/* the smtp payload */
connection.write("helo " + options.domain + "\r\n");
if(options.authentication === "login") {
connection.write("auth login\r\n");
connection.write(options.username + "\r\n");
connection.write(options.password + "\r\n");
var key = options.host + ':' + options.port;
//sys.puts('conne' + connection.ready);
if(connection.ready){
connection.ready = false;
if(email.connections[ key ].queue.length) {
var msg = email.connections[ key ].queue.pop();
email.pushMessage( connection, options );
}
}
else{
email.connections[ key ].queue.push(options);
}
connection.write("mail from: " + options.from + "\r\n");
connection.write("rcpt to: " + options.to + "\r\n");
connection.write("data\r\n");
connection.write("From: " + options.from + "\r\n");
connection.write("To: " + options.to + "\r\n");
connection.write("Subject: " + options.subject + "\r\n");
connection.write("Content-Type: text/html\r\n");
connection.write(email.wordwrap(options.body) + "\r\n");
connection.write(".\r\n");
connection.write("quit\r\n");

});

},

drainQueue: function ( connection, key ) {
if(email.connections[ key ].queue.length){
var msg = email.connections[ key ].queue.pop();
email.pushMessage( connection, msg );
}
},
pushMessage: function ( connection, options ){

//sys.puts('pushMessage');
//eyes.inspect(options);
/* the smtp payload */
connection.write("helo " + options.domain + "\r\n");
if(options.authentication === "login") {
connection.write("auth login\r\n");
connection.write(options.username + "\r\n");
connection.write(options.password + "\r\n");
}
connection.write("mail from: " + options.from + "\r\n");
connection.write("rcpt to: " + options.to + "\r\n");
connection.write("data\r\n");
connection.write("From: " + options.from + "\r\n");
connection.write("To: " + options.to + "\r\n");
connection.write("Subject: " + options.subject + "\r\n");
connection.write("Content-Type: text/html\r\n");
connection.write(email.wordwrap(options.body) + "\r\n");
connection.write(".\r\n");
connection.write("quit\r\n");

connection.ready = true;

var key = options.host + ':' + options.port;

if(email.connections[ key ].queue.length){
var msg = email.connections[ key ].queue.pop();
email.pushMessage( connection, msg );
}

},

parseResponse:function(data){
var d = data.split("\r\n");
d.forEach(function(itm){
Expand Down

0 comments on commit 9309ac3

Please sign in to comment.