From 8720acabec307d22f78c530af3d489987574aa48 Mon Sep 17 00:00:00 2001 From: Marak Squires Date: Tue, 14 Sep 2010 02:55:11 -0400 Subject: [PATCH] [api] started to create connection pool. added stub python smtp server --- demo.js | 22 +++++++---- lib/node_mailer.js | 96 ++++++++++++++++++++++++++++++---------------- test/stubSMTP | 8 ++++ 3 files changed, 86 insertions(+), 40 deletions(-) create mode 100644 test/stubSMTP diff --git a/demo.js b/demo.js index b9a58a3..7aa952e 100644 --- a/demo.js +++ b/demo.js @@ -1,14 +1,22 @@ -var email = require("mailer"); +var email = require("./lib/node_mailer"); + +for(var i = 0; i < 5; i++){ + + + +} email.send({ host : "localhost", // smtp server hostname - port : "25", // smtp server port + port : "1025", // smtp server port domain : "localhost", // domain used by client to identify itself to server - authentication : "login", // auth login is supported; anything else is no auth - username : "dXNlcm5hbWU=", // Base64 encoded username - password : "cGFzc3dvcmQ=", // Base64 encoded password to : "marak.squires@gmail.com", from : "obama@whitehouse.gov", subject : "node_mailer test email", - body : "hello this is a test email from the node_mailer" -}); + 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 + +}); \ No newline at end of file diff --git a/lib/node_mailer.js b/lib/node_mailer.js index ef2cb5f..8f7fd65 100644 --- a/lib/node_mailer.js +++ b/lib/node_mailer.js @@ -23,11 +23,52 @@ OTHER DEALINGS IN THE SOFTWARE. */ -var tcp = require('net'); -var sys = require('sys'); +var tcp = require('net'), + sys = require('sys'), + colors = require('colors'); var email = { + + connections: [], + + getConnection: function (options, callback) { + // perform lookup to determine if connection already exists for server / port + if(typeof email.connections[ options.host + ':' + options.port ] == 'undefined'){ + email.createConnection(options.port, options.host, function(connection){ + callback(connection); + }); + } + else{ + callback(email.connections[ options.host + ':' + options.port ]); + } + }, + createConnection: function (port, host, callback) { + + connection = tcp.createConnection(port, host); + connection.setEncoding('utf8'); + + connection.addListener("connect", function () { + callback(connection); + }); + + connection.addListener("end", function() { + connection.end(); + }); + + connection.addListener("data", function (data) { + if(email.parseResponse(data)){ + sys.puts("SUCC"); + } else{ + sys.puts("ERR"); + } + sys.puts(data); + }); + + + }, send: function (options) { + + // setup some default config options var options = typeof(options) == "undefined" ? {} : options; options.to = typeof(options.to) == "undefined" ? "marak.squires@gmail.com" : options.to; options.from = typeof(options.from) == "undefined" ? "obama@whitehouse.gov" : options.from; @@ -36,42 +77,31 @@ var email = { options.host = typeof(options.host) == "undefined" ? "localhost" : options.host; options.domain = typeof(options.domain) == "undefined" ? "localhost" : options.domain; options.port = typeof(options.port) == "undefined" ? 25 : options.port; - - var self = this; + + email.getConnection(options, function(connection){ - this.connection = tcp.createConnection(options.port, options.host); - this.connection.setEncoding('utf8'); - this.connection.addListener("connect", function () { - self.connection.write("helo " + options.domain + "\r\n"); + /* the smtp payload */ + connection.write("helo " + options.domain + "\r\n"); if(options.authentication === "login") { - self.connection.write("auth login\r\n"); - self.connection.write(options.username + "\r\n"); - self.connection.write(options.password + "\r\n"); + connection.write("auth login\r\n"); + connection.write(options.username + "\r\n"); + connection.write(options.password + "\r\n"); } - self.connection.write("mail from: " + options.from + "\r\n"); - self.connection.write("rcpt to: " + options.to + "\r\n"); - self.connection.write("data\r\n"); - self.connection.write("From: " + options.from + "\r\n"); - self.connection.write("To: " + options.to + "\r\n"); - self.connection.write("Subject: " + options.subject + "\r\n"); - self.connection.write("Content-Type: text/html\r\n"); - self.connection.write(email.wordwrap(options.body) + "\r\n"); - self.connection.write(".\r\n"); - self.connection.write("quit\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"); + }); + + - this.connection.addListener("end", function() { - self.connection.end(); - }); - - this.connection.addListener("data", function (data) { - if(email.parseResponse(data)){ - sys.puts("SUCC"); - } else{ - sys.puts("ERR"); - } - sys.puts(data); - }); }, parseResponse:function(data){ diff --git a/test/stubSMTP b/test/stubSMTP new file mode 100644 index 0000000..b736d97 --- /dev/null +++ b/test/stubSMTP @@ -0,0 +1,8 @@ +#!/usr/bin/env bash +if [ -z $1 ] +then port=1025 +else port=$1 +fi + +echo "Starting dumb mail server on localhost:$port" +python -m smtpd -n -c DebuggingServer localhost:$port \ No newline at end of file