From cc65d4e5f557f07aea9f69f9e24ee621dcf8a92f Mon Sep 17 00:00:00 2001 From: hugdubois Date: Sat, 20 Aug 2011 21:33:56 +0200 Subject: [PATCH 1/4] Add error listener on the smtp socket to prevent ECONNREFUSED error --- lib/smtp.js | 3 +++ 1 file changed, 3 insertions(+) diff --git a/lib/smtp.js b/lib/smtp.js index 4d88fb9..c101745 100644 --- a/lib/smtp.js +++ b/lib/smtp.js @@ -63,6 +63,9 @@ Client.prototype.connect = function() { this.emit('error', new Error('Session already started.')); else { this.sock + .on('error', function(err) { + self.emit('error', err); + }) .once('connect', function() { self.reset(220); }) From a6045d85692fac2455645e404e19f4756152c857 Mon Sep 17 00:00:00 2001 From: hugdubois Date: Sat, 20 Aug 2011 21:57:23 +0200 Subject: [PATCH 2/4] Add an option to set a smtp socket timeout --- README.md | 1 + lib/smtp.js | 6 ++++++ 2 files changed, 7 insertions(+) diff --git a/README.md b/README.md index 29ffd56..31ff333 100644 --- a/README.md +++ b/README.md @@ -50,6 +50,7 @@ Other options: + `port`: server listens on this port (default: 587 or 25) + `domain`: the domain of the sender (default: `os.hostname()`) + `mimeTransport`: `7BIT` or `8BITMIME` (default: `8BITMIME`) + + `timeout`: timeout for server connection (default: no timeout) ### Mail.message(headers) ### diff --git a/lib/smtp.js b/lib/smtp.js index c101745..6428788 100644 --- a/lib/smtp.js +++ b/lib/smtp.js @@ -23,6 +23,8 @@ function Client(opt) { this.options = opt; + this.timeout = opt.timeout || 0 + if (!(this.host = (opt && opt.host))) throw new Error('missing required host'); @@ -62,10 +64,14 @@ Client.prototype.connect = function() { else if (this.sock.readyState != 'closed') this.emit('error', new Error('Session already started.')); else { + this.sock.setTimeout(this.timeout); this.sock .on('error', function(err) { self.emit('error', err); }) + .on('timeout', function(err) { + self.emit('error', new Error('Conection timeout')); + }) .once('connect', function() { self.reset(220); }) From b639a1ed13b91b7c8f99a55e9798379ddef27494 Mon Sep 17 00:00:00 2001 From: hugdubois Date: Sat, 20 Aug 2011 22:31:31 +0200 Subject: [PATCH 3/4] Bug fix on timeout option remove timeout listener on success connection --- lib/smtp.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/smtp.js b/lib/smtp.js index 6428788..6f085c8 100644 --- a/lib/smtp.js +++ b/lib/smtp.js @@ -75,7 +75,9 @@ Client.prototype.connect = function() { .once('connect', function() { self.reset(220); }) - .connect(this.port, this.host); + .connect(this.port, this.host, function() { + self.sock.removeAllListeners('timeout'); + }); } return this; From 9c18b40a6066858a395e5dec93777ab156cdfafd Mon Sep 17 00:00:00 2001 From: hugdubois Date: Sat, 20 Aug 2011 23:18:37 +0200 Subject: [PATCH 4/4] Prevent to Socket is not writable Exception --- lib/smtp.js | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/lib/smtp.js b/lib/smtp.js index 6f085c8..1e8aadd 100644 --- a/lib/smtp.js +++ b/lib/smtp.js @@ -161,8 +161,12 @@ Client.prototype.puts = function(data) { // Write some data. Client.prototype.write = function(data) { - U.debug('SEND (((%s)))', data); - return this.sock.write(data); + try { + U.debug('SEND (((%s)))', data); + return this.sock.write(data); + } catch(e) { + this.emit('error', e); + } }; // Write some final data, terminate the connection.