smtp-connection
Advanced tools
Comparing version 0.1.4 to 0.1.5
@@ -155,5 +155,3 @@ 'use strict'; | ||
this._connectionTimeout = setTimeout((function() { | ||
var error = new Error('Connection timeout'); | ||
error.code = 'ETIMEDOUT'; | ||
this.emit('error', error); | ||
this.emit('error', this._formatError('Connection timeout', 'ETIMEDOUT')); | ||
this.close(); | ||
@@ -207,4 +205,2 @@ }).bind(this), this.options.connectionTimeout || 60 * 1000); | ||
SMTPConnection.prototype.login = function(authData, callback) { | ||
var err; | ||
this._auth = authData || {}; | ||
@@ -224,4 +220,4 @@ | ||
case 'XOAUTH2': | ||
this._currentAction = function(response) { | ||
this._actionAUTHComplete(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionAUTHComplete(str, callback); | ||
}.bind(this); | ||
@@ -231,4 +227,4 @@ this._sendCommand('AUTH XOAUTH2 ' + this._buildXOAuth2Token(this._auth.user, this._auth.xoauth2)); | ||
case 'LOGIN': | ||
this._currentAction = function(response) { | ||
this._actionAUTH_LOGIN_USER(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionAUTH_LOGIN_USER(str, callback); | ||
}.bind(this); | ||
@@ -238,4 +234,4 @@ this._sendCommand('AUTH LOGIN'); | ||
case 'PLAIN': | ||
this._currentAction = function(response) { | ||
this._actionAUTHComplete(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionAUTHComplete(str, callback); | ||
}.bind(this); | ||
@@ -249,4 +245,4 @@ this._sendCommand('AUTH PLAIN ' + new Buffer( | ||
case 'CRAM-MD5': | ||
this._currentAction = function(response) { | ||
this._actionAUTH_CRAM_MD5(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionAUTH_CRAM_MD5(str, callback); | ||
}.bind(this); | ||
@@ -257,14 +253,15 @@ this._sendCommand('AUTH CRAM-MD5'); | ||
err = new Error('Unknown authentication method "' + authMethod + '"'); | ||
err.code = 'EAUTH'; | ||
return callback(err); | ||
return callback(this._formatError('Unknown authentication method "' + authMethod + '"', 'EAUTH')); | ||
}; | ||
/** | ||
* Sends a message | ||
* | ||
* @param {Object} envelope Envelope object, {from: addr, to: [addr]} | ||
* @param {Object} message String, Buffer or a Stream | ||
* @param {Function} callback Callback to return once sending is completed | ||
*/ | ||
SMTPConnection.prototype.send = function(envelope, message, callback) { | ||
var err; | ||
if (!message) { | ||
err = new Error('Empty message'); | ||
err.code = 'EMESSAGE'; | ||
return callback(err); | ||
return callback(this._formatError('Empty message', 'EMESSAGE')); | ||
} | ||
@@ -276,7 +273,7 @@ | ||
} | ||
var stream = this._createSendStream(function(err, response) { | ||
var stream = this._createSendStream(function(err, str) { | ||
if (err) { | ||
return callback(err); | ||
} | ||
info.response = response; | ||
info.response = str; | ||
return callback(null, info); | ||
@@ -315,5 +312,3 @@ }); | ||
if (this._socket && !this._destroyed && this._currentAction === this._actionGreeting) { | ||
var error = new Error('Greeting never received'); | ||
error.code = 'ETIMEDOUT'; | ||
this.emit('error', error); | ||
this.emit('error', this._formatError('Greeting never received', 'ETIMEDOUT')); | ||
this.close(); | ||
@@ -368,11 +363,29 @@ } | ||
this.emit('error', this._formatError(err, type, data)); | ||
this.close(); | ||
}; | ||
SMTPConnection.prototype._formatError = function(message, type, response) { | ||
var err; | ||
if (/Error\]$/i.test(Object.prototype.toString.call(message))) { | ||
err = message; | ||
} else { | ||
err = new Error(message); | ||
} | ||
if (type && type !== 'Error') { | ||
err.code = type; | ||
} | ||
if (data) { | ||
err.data = data; | ||
if (response) { | ||
err.response = response; | ||
} | ||
this.emit('error', err); | ||
this.close(); | ||
var responseCode = typeof response === 'string' && Number((response.match(/^\d+/) || [])[0]) || false; | ||
if (responseCode) { | ||
err.responseCode = responseCode; | ||
} | ||
return err; | ||
}; | ||
@@ -462,5 +475,5 @@ | ||
var response = (this._responseQueue.shift() || '').toString(); | ||
var str = (this._responseQueue.shift() || '').toString(); | ||
if (/^\d+\-/.test(response.split('\n').pop())) { | ||
if (/^\d+\-/.test(str.split('\n').pop())) { | ||
// keep waiting for the final part of multiline response | ||
@@ -473,3 +486,3 @@ return; | ||
type: 'server', | ||
message: response.trim() | ||
message: str.trim() | ||
}); | ||
@@ -482,6 +495,6 @@ } | ||
if (typeof action === 'function') { | ||
action.call(this, response); | ||
action.call(this, str); | ||
setImmediate(this._processResponse.bind(this, true)); | ||
} else { | ||
return this._onError(new Error('Unexpected Response'), 'EPROTOCOL', response); | ||
return this._onError(new Error('Unexpected Response'), 'EPROTOCOL', str); | ||
} | ||
@@ -525,4 +538,2 @@ }; | ||
SMTPConnection.prototype._setEnvelope = function(envelope, callback) { | ||
var err; | ||
this._envelope = envelope || {}; | ||
@@ -536,5 +547,3 @@ this._envelope.from = this._envelope.from && this._envelope.from.address || this._envelope.from || ''; | ||
if (!this._envelope.to.length) { | ||
err = new Error('No recipients defined'); | ||
err.code = 'EAUTH'; | ||
return callback(err); | ||
return callback(this._formatError('No recipients defined', 'EENVELOPE')); | ||
} | ||
@@ -547,4 +556,4 @@ | ||
this._currentAction = function(response) { | ||
this._actionMAIL(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionMAIL(str, callback); | ||
}.bind(this); | ||
@@ -557,4 +566,4 @@ this._sendCommand('MAIL FROM:<' + (this._envelope.from) + '>'); | ||
this._currentAction = function(response) { | ||
this._actionStream(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionStream(str, callback); | ||
}.bind(this); | ||
@@ -711,14 +720,9 @@ | ||
SMTPConnection.prototype._actionAUTH_LOGIN_USER = function(str, callback) { | ||
var err; | ||
if (str !== '334 VXNlcm5hbWU6') { | ||
err = new Error('Invalid login sequence while waiting for "334 VXNlcm5hbWU6"'); | ||
err.data = str; | ||
err.code = 'EAUTH'; | ||
callback(err); | ||
callback(this._formatError('Invalid login sequence while waiting for "334 VXNlcm5hbWU6"', 'EAUTH', str)); | ||
return; | ||
} | ||
this._currentAction = function(response) { | ||
this._actionAUTH_LOGIN_PASS(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionAUTH_LOGIN_PASS(str, callback); | ||
}.bind(this); | ||
@@ -741,9 +745,5 @@ | ||
var challengeString = ''; | ||
var err; | ||
if (!challengeMatch) { | ||
err = new Error('Invalid login sequence while waiting for server challenge string'); | ||
err.data = str; | ||
err.code = 'EAUTH'; | ||
return callback(err); | ||
return callback(this._formatError('Invalid login sequence while waiting for server challenge string', 'EAUTH', str)); | ||
} else { | ||
@@ -762,4 +762,4 @@ challengeString = challengeMatch[1]; | ||
this._currentAction = function(response) { | ||
this._actionAUTH_CRAM_MD5_PASS(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionAUTH_CRAM_MD5_PASS(str, callback); | ||
}.bind(this); | ||
@@ -779,9 +779,4 @@ | ||
SMTPConnection.prototype._actionAUTH_CRAM_MD5_PASS = function(str, callback) { | ||
var err; | ||
if (!str.match(/^235\s+/)) { | ||
err = new Error('Invalid login sequence while waiting for "235"'); | ||
err.data = str; | ||
err.code = 'EAUTH'; | ||
return callback(err); | ||
return callback(this._formatError('Invalid login sequence while waiting for "235"', 'EAUTH', str)); | ||
} | ||
@@ -801,13 +796,8 @@ | ||
SMTPConnection.prototype._actionAUTH_LOGIN_PASS = function(str, callback) { | ||
var err; | ||
if (str !== '334 UGFzc3dvcmQ6') { | ||
err = new Error('Invalid login sequence while waiting for "334 UGFzc3dvcmQ6"'); | ||
err.data = str; | ||
err.code = 'EAUTH'; | ||
return callback(err); | ||
return callback(this._formatError('Invalid login sequence while waiting for "334 UGFzc3dvcmQ6"', 'EAUTH', str)); | ||
} | ||
this._currentAction = function(response) { | ||
this._actionAUTHComplete(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionAUTHComplete(str, callback); | ||
}.bind(this); | ||
@@ -826,7 +816,5 @@ | ||
SMTPConnection.prototype._actionAUTHComplete = function(str, callback) { | ||
var err; | ||
if (str.substr(0, 3) === '334') { | ||
this._currentAction = function(response) { | ||
this._actionAUTHComplete(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionAUTHComplete(str, callback); | ||
}.bind(this); | ||
@@ -838,6 +826,3 @@ this._sendCommand(new Buffer(0)); | ||
if (str.charAt(0) !== '2') { | ||
err = new Error('Invalid login'); | ||
err.data = str; | ||
err.code = 'EAUTH'; | ||
return callback(err); | ||
return callback(this._formatError('Invalid login', 'EAUTH', str)); | ||
} | ||
@@ -855,20 +840,12 @@ | ||
SMTPConnection.prototype._actionMAIL = function(str, callback) { | ||
var err; | ||
if (Number(str.charAt(0)) !== 2) { | ||
err = new Error('Mail command failed'); | ||
err.data = str; | ||
err.code = 'EENVELOPE'; | ||
return callback(err); | ||
return callback(this._formatError('Mail command failed', 'EENVELOPE', str)); | ||
} | ||
if (!this._envelope.rcptQueue.length) { | ||
err = new Error('Can\'t send mail - no recipients defined'); | ||
err.data = str; | ||
err.code = 'EENVELOPE'; | ||
return callback(err); | ||
return callback(this._formatError('Can\'t send mail - no recipients defined', 'EENVELOPE')); | ||
} else { | ||
this._envelope.curRecipient = this._envelope.rcptQueue.shift(); | ||
this._currentAction = function(response) { | ||
this._actionRCPT(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionRCPT(str, callback); | ||
}.bind(this); | ||
@@ -885,4 +862,2 @@ this._sendCommand('RCPT TO:<' + this._envelope.curRecipient + '>'); | ||
SMTPConnection.prototype._actionRCPT = function(str, callback) { | ||
var err; | ||
if (Number(str.charAt(0)) !== 2) { | ||
@@ -897,16 +872,13 @@ // this is a soft error | ||
if (this._envelope.rejected.length < this._envelope.to.length) { | ||
this._currentAction = function(response) { | ||
this._actionDATA(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionDATA(str, callback); | ||
}.bind(this); | ||
this._sendCommand('DATA'); | ||
} else { | ||
err = new Error('Can\'t send mail - all recipients were rejected'); | ||
err.data = str; | ||
err.code = 'EENVELOPE'; | ||
return callback(err); | ||
return callback(this._formatError('Can\'t send mail - all recipients were rejected', 'EENVELOPE', str)); | ||
} | ||
} else { | ||
this._envelope.curRecipient = this._envelope.rcptQueue.shift(); | ||
this._currentAction = function(response) { | ||
this._actionRCPT(response, callback); | ||
this._currentAction = function(str) { | ||
this._actionRCPT(str, callback); | ||
}.bind(this); | ||
@@ -923,11 +895,6 @@ this._sendCommand('RCPT TO:<' + this._envelope.curRecipient + '>'); | ||
SMTPConnection.prototype._actionDATA = function(str, callback) { | ||
var err; | ||
// response should be 354 but according to this issue https://github.com/eleith/emailjs/issues/24 | ||
// some servers might use 250 instead, so lets check for 2 or 3 as the first digit | ||
if ([2, 3].indexOf(Number(str.charAt(0))) < 0) { | ||
err = new Error('Data command failed'); | ||
err.data = str; | ||
err.code = 'EENVELOPE'; | ||
return callback(err); | ||
return callback(this._formatError('Data command failed', 'EENVELOPE', str)); | ||
} | ||
@@ -947,10 +914,5 @@ | ||
SMTPConnection.prototype._actionStream = function(str, callback) { | ||
var err; | ||
if (Number(str.charAt(0)) !== 2) { | ||
// Message failed | ||
err = new Error('Message failed'); | ||
err.data = str; | ||
err.code = 'EMESSAGE'; | ||
return callback(err); | ||
return callback(this._formatError('Message failed', 'EMESSAGE', str)); | ||
} else { | ||
@@ -957,0 +919,0 @@ // Message sent succesfully |
{ | ||
"name": "smtp-connection", | ||
"version": "0.1.4", | ||
"version": "0.1.5", | ||
"description": "Connect to SMTP servers", | ||
@@ -5,0 +5,0 @@ "main": "lib/smtp-connection.js", |
@@ -45,4 +45,4 @@ # smtp-connection | ||
* **'connect'** emitted when the connection is established | ||
* **'end'** when the instance is destroyed | ||
* **'log'** *(data)* emitted for all traffic when debug option is set to true | ||
* **'end'** when the instance is destroyed | ||
@@ -94,5 +94,9 @@ ### connect | ||
* **err** and error object if sending failed | ||
* **code** string code identifying the error, for example 'EAUTH' is returned when authentication fails | ||
* **response** is the last response received from the server (if the error is caused by an error response from the server) | ||
* **responseCode** is the numeric response code of the `response` string (if available) | ||
* **info** information object about accepted and rejected recipients | ||
* **acepted** and array of accepted recipient addresses | ||
* **rejected** and array of rejected recipient addresses | ||
* **response** is the last response received from the server | ||
@@ -99,0 +103,0 @@ ### quit |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
36523
119
901