Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

prevent ECONNREFUSED error and socket timeout options #19

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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) ###

Expand Down
21 changes: 18 additions & 3 deletions lib/smtp.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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.
Expand Down