I'm trying to write a dumb (braindead) smtp server in node.js to act as a stub server for testing other applications.
I have a basic set of responses, and it works great with telnet
.
However, when my php/Codeigniter app uses it, it runs so slowly as to be useless, and I get occasional fwirite() broken socket
errors.
Am I missing a critical or obvious step? Is there something about sockets or system pointer resources that I'm mishandling?
It's pretty quick & dirty but that was the point:
var net = require('net');
net.createServer(function (socket) {
var mode = 'nodata';
socket.on('data', function(data) {
var data_string = data.toString();
console.log(data_string);
if (mode == 'nodata') {
if (data_string.substring(0, 4) == 'EHLO') {
socket.write('hello ' + data_string.substring(5, data_string.length - 1) + '\r\n');
} else if (data_string.substring(0, 10) == 'AUTH LOGIN') {
socket.write('250 ok\r\n');
} else if (data_string.substring(0, 10) == 'MAIL FROM:') {
socket.write('250 ok\r\n');
} else if (data_string.substring(0, 8) == 'RCPT TO:') {
to = data_string.substring(8, data_string.length - 1);
socket.write('250 ok its for ' + to + '\r\n');
} else if (data_string.substring(0, 4) == 'DATA') {
socket.write('354 ok send it\r\n');
mode = 'data';
} else if (data_string.substring(0, 4) == 'QUIT') {
socket.write('221 Goodbye\r\n');
socket.end();
return;
}
} else if (mode == 'data') {
if (data_string.substring(0, 1) == '.') {
socket.write('250 message queued\r\n');
mode = 'nodata';
}
}
});
}).listen(3434);
You are missing much of the linebreak logic I would expect: not sure if that would cause the behavior issues you're observing, but you might want to use something a bit more full featured (like this one, perhaps) in case you have some edge case that your implementation isn't really handling right. Does it work with ANY other mail client besides telnet?