Skip to content

Commit

Permalink
Updated to use simplesmtp npm package instead of smtp, because of nod…
Browse files Browse the repository at this point in the history
…e 0.6.x support. npm version number updated to 0.4.0
  • Loading branch information
deitch committed Mar 25, 2012
1 parent a47289a commit 2c24450
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 46 deletions.
59 changes: 29 additions & 30 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
/*jslint node:true, nomen:false */

// get the authentication/sessionManager library and the authorization library
var smtp = require('smtp'), _ = require('underscore'), mimelib = require('mimelib-noiconv'), path = require('path'),
var smtp = require('simplesmtp'), _ = require('underscore'), mimelib = require('mimelib-noiconv'), path = require('path'),
mailSplitter, modules = {};


Expand Down Expand Up @@ -39,35 +39,34 @@ mailSplitter = function(data) {
module.exports = {
init: function(port) {
var msgs = {}, msgid = 1, handlers = {}, processMsg, smtpServer, that;
smtpServer = smtp.createServer(function(connection) {
var email = {sender: null, receivers: {}, data: ""}, body = "";
connection.on('DATA', function(message) {
// record a message from sender to receivers
email.sender = message.sender;
_.each(message.recipients,function(r){
/*jslint regexp:false */
email.receivers[r.address.replace(/^[^<]*</g,"").replace(/>[^>]*$/g,"")] = true;
/*jslint regexp:true */
});
// record the data
message.on('data', function(data) {
body += data;
});
// message is complete: accept the data, record we have a valid email
message.on('end', function() {
email.data = body;
message.accept();
_.extend(email,mailSplitter(email.data) || {});
// is this email encoded quotedprintable? decode it
if (email.headers && email.headers["Content-Transfer-Encoding"] && email.headers["Content-Transfer-Encoding"].toLowerCase() === "quoted-printable") {
email.body = mimelib.decodeQuotedPrintable(email.body);
}
msgs[msgid++] = email;
processMsg(email,msgid);
});
smtpServer = smtp.createServer();
smtpServer.on("startData",function(envelope){
envelope.body = "";
});
smtpServer.on("data",function(envelope,chunk){
envelope.body += chunk;
});
smtpServer.on("dataReady",function(envelope,callback){
var email = {sender: null, receivers: {}, data: ""};
email.sender = envelope.from;
_.each(envelope.to||[],function(rcpt){
/*jslint regexp:false */
email.receivers[rcpt.replace(/^[^<]*</g,"").replace(/>[^>]*$/g,"")] = true;
/*jslint regexp:true */
});
email.data = envelope.body;
_.extend(email,mailSplitter(email.data) || {});
// is this email encoded quotedprintable? decode it
if (email.headers && email.headers["Content-Transfer-Encoding"] && email.headers["Content-Transfer-Encoding"].toLowerCase() === "quoted-printable") {
email.body = mimelib.decodeQuotedPrintable(email.body);
}
msgs[msgid++] = email;
processMsg(email,msgid);
callback(null);
});

smtpServer.listen(port,function(err){
});
smtpServer.listen(port);

processMsg = function(email,msgid) {
// catch-all handlers
Expand Down Expand Up @@ -137,8 +136,8 @@ module.exports = {
removeAll: function() {
msgs = {};
},
stop: function() {
smtpServer.close();
stop: function(cb) {
smtpServer.end(cb);
},
module: function(name) {
var mod, filename = __dirname+'/modules/'+name+'.js', success = false, args = Array.prototype.slice.call(arguments,1);
Expand Down
9 changes: 7 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
{
"name": "smtp-tester",
"description": "Quick and dirty smtp server, that accepts handlers to process messages",
"version": "0.3.0",
"version": "0.4.0",
"url": "http://github.com/deitch/smtp-tester",
"author": "Avi Deitcher <avi@deitcher.net>",
"engines": ["node >= 0.4"],
"main": "./lib/index.js",
"dependencies": {"smtp":">=0.1.4","underscore":">=1.1.6","mimelib-noiconv":">=0.1.2","nodemailer":">=0.1.8"},
"dependencies": {
"underscore":">=1.1.6",
"mimelib-noiconv":">=0.1.2",
"nodemailer":">=0.1.8",
"simplesmtp":">=0.1.5"
},
"devDependencies": {"nodeunit":">=0.5.0"},
"test":"./test/test.js",
"repository": {
Expand Down
28 changes: 14 additions & 14 deletions test/test.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
/*jslint node:true, nomen:false */
var ms = require('../lib/index'), nodeunit = require('nodeunit'), mailer = require('nodemailer'), mailPort = 4025,
mailServer, testFn, sendmail, from = "mailtest@bar.com";
mailServer, testFn, sendmail, from = "smtpmailtest@gmail.com";

//mailServer = ms.init(mailPort);

Expand All @@ -27,16 +27,15 @@ testFn = {
callback();
},
tearDown: function(callback) {
mailServer.stop();
callback();
mailServer.stop(callback);
},
// not logged in should give unauthenticated
specificHandler : function(test) {
var handler, checkDone, count = 0, expected = 2, addr = "foo@bar.com", subject = "email test", body = "This is a test email";
var handler, checkDone, count = 0, expected = 2, addr = "foo@gmail.com", subject = "email test", body = "This is a test email";
// bind a handler
handler = function(address,id,email) {
test.equal(address,addr,"Should have address sent to handler as '"+addr+"'");
test.equal(email.body,body+"\r\n","Body should match");
test.equal(email.body,body,"Body should match");
test.equal(email.headers.To,addr,"Should have header address To match");
test.equal(email.headers.From,from,"Should have header address From match");
checkDone();
Expand All @@ -61,11 +60,11 @@ testFn = {
});
},
catchAllHandler : function(test) {
var handler, checkDone, count = 0, expected = 2, addr = "foo@bar.com", subject = "email test", body = "This is a test email";
var handler, checkDone, count = 0, expected = 2, addr = "foo@gmail.com", subject = "email test", body = "This is a test email";
// bind a handler
handler = function(address,id,email) {
test.equal(address,null,"Should have address 'null' sent to handler");
test.equal(email.body,body+"\r\n","Body should match");
test.equal(email.body,body,"Body should match");
test.equal(email.headers.To,addr,"Should have header address To match");
test.equal(email.headers.From,from,"Should have header address From match");
checkDone();
Expand Down Expand Up @@ -96,22 +95,23 @@ testFn = {
callback();
},
tearDown: function(callback) {
mailServer.stop();
callback();
mailServer.stop(callback);
},
logAll : function(test) {
var success, addr = "foo@bar.com", subject = "email test", body = "This is a test email", _log = console.log, message;

message = "From: mailtest@bar.com\nTo: foo@bar.com\nSubject: email test\nThis is a test email\r\n\n\n";
var success, addr = "foo@gmail.com", subject = "email test", body = "This is a test email", _log = console.log, message;
message = "From: smtpmailtest@gmail.com\nTo: foo@gmail.com\nSubject: email test\nThis is a test email\n\n";

// load the module
success = mailServer.module("logAll");
test.equal(success,true,"Should have success loading module");
// send a mail, see that it ends up on the console
// but first capture the console
console.log = function(msg) {
// expect the message - but the date can change, so remove it
test.equal(msg.replace(/\nDate:.*\nSubject/,"\nSubject"),message,"Should be a specific message");
//_log(msg);
if (msg && typeof(msg) === "string") {
// expect the message - but the date can change, so remove it
test.equal(msg.replace(/\nDate:.*\nSubject/,"\nSubject"),message,"Should be a specific message");
}
console.log = _log;
test.done();
};
Expand Down

0 comments on commit 2c24450

Please sign in to comment.