Skip to content

Commit

Permalink
[api] started to create connection pool. added stub python smtp server
Browse files Browse the repository at this point in the history
  • Loading branch information
Marak committed Sep 14, 2010
1 parent d7230cd commit 8720aca
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 40 deletions.
22 changes: 15 additions & 7 deletions 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

});
96 changes: 63 additions & 33 deletions lib/node_mailer.js
Expand Up @@ -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;
Expand All @@ -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){
Expand Down
8 changes: 8 additions & 0 deletions 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

0 comments on commit 8720aca

Please sign in to comment.