Browse files

bit of cleaning, fixed pre-connected write issue

  • Loading branch information...
1 parent dcaeb7d commit 2f419bf93f1c6008f0a78973b5d077d010b1037d @tmpvar tmpvar committed Oct 31, 2010
Showing with 17 additions and 118 deletions.
  1. +17 −110 lib/node_mailer.js
  2. +0 −8 test/stubSMTP
View
127 lib/node_mailer.js
@@ -49,17 +49,27 @@ Connection.prototype = {
_processing : false,
_process : function() {
var self = this;
- if (!self._stream) {
- self.connect(function() {
- self._process();
- });
+ if (!self._stream ||
+ ['open', 'writeOnly'].indexOf(self._stream.readyState) === -1)
+ {
+ return;
}
-
+
if (!self._processing) {
var dequeue = function() {
+ if (!self._stream ||
+ ['open', 'writeOnly'].indexOf(self._stream.readyState) === -1)
+ {
+ self.connect(function() {
+ self._process;
+ });
+ return;
+ }
+
var email = self._queue.shift();
if (email) {
+ // TODO: incremental sending.. (think drain + pause/resume)
self._stream.write("mail from: " + email.options.from + "\r\n");
self._stream.write("rcpt to: " + email.options.to + "\r\n");
self._stream.write("data\r\n");
@@ -99,8 +109,8 @@ Connection.prototype = {
stream.write("helo " + self.options.domain + "\r\n");
if(self.options.authentication === "login") {
stream.write("auth login\r\n");
- stream.write(options.username + "\r\n");
- stream.write(options.password + "\r\n");
+ stream.write(self.options.username + "\r\n");
+ stream.write(self.options.password + "\r\n");
}
if (typeof fn === 'function') {
@@ -116,7 +126,6 @@ Connection.prototype = {
stream.on("end", function() {
self._connecting = false;
-// self._stream.destroy();
self._stream = null;
});
} else if (typeof fn === 'function') {
@@ -194,105 +203,3 @@ exports.send = function(options) {
return new Email(options);
};
-
-
-
-/*
-var email = {
-
- connections: {},
-
- getConnection: function (options, callback) {
-
- var key = options.host + ':' + options.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.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);
- callback(email.connections[ key ]);
- }
- },
- createConnection: function (port, host, callback) {
-
- var key = host + ':' + port;
- console.log("attempting connection" , key);
- var connection = tcp.createConnection(port, host);
- connection.setEncoding('utf8');
-
- connection.addListener("connect", function () {
- //sys.puts('got connected');
- email.connections[ key ].ready = true;
- email.drainQueue( connection, key );
- //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);
- });
-
- return connection;
-
- },
- send: function (options) {
- //sys.puts('send()');
- // setup some default config options
-
-
- email.getConnection(options, function(connection){
-
- 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);
- }
-
- });
-
- },
-
-
- parseResponse:function(data){
- var d = data.split("\r\n");
- d.forEach(function(itm){
- if(itm.indexOf("250 OK id=") != -1){
- return true;
- }
- });
- return false;
- },
-
-
-}
-*/
View
8 test/stubSMTP
@@ -1,8 +0,0 @@
-#!/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 2f419bf

Please sign in to comment.