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 4d88fb9..1e8aadd 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,11 +64,20 @@ 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); }) - .connect(this.port, this.host); + .connect(this.port, this.host, function() { + self.sock.removeAllListeners('timeout'); + }); } return this; @@ -150,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.