Skip to content

Commit

Permalink
Merge branch 'master' of github.com:jbaylina/simplesmtp into jbaylina…
Browse files Browse the repository at this point in the history
…-master
  • Loading branch information
andris9 committed Feb 26, 2014
2 parents e8ff335 + ee4b1de commit 5153e18
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ The following connection options can be used with `simplesmtp.connect`:
* **connectionTimeout** (system default if not set) - Time to wait in ms until the socket is opened to the server * **connectionTimeout** (system default if not set) - Time to wait in ms until the socket is opened to the server
* **rejectUnathorized** (defaults to false) - if set to true accepts only valid server certificates. You can override this option with the `tls` option, this is just a shorthand * **rejectUnathorized** (defaults to false) - if set to true accepts only valid server certificates. You can override this option with the `tls` option, this is just a shorthand
* **dsn** - An object with methods `success`, `failure` and `delay`. If any of these are set to true, DSN will be used * **dsn** - An object with methods `success`, `failure` and `delay`. If any of these are set to true, DSN will be used
* **disableDotEscaping** set to true if you want to bypass the dot escaping at the begining of each line. Default to false


### Connection events ### Connection events


Expand Down
49 changes: 43 additions & 6 deletions lib/client.js
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -59,6 +59,8 @@ function SMTPClient(port, host, options){
this.options.secureConnection = !!this.options.secureConnection; this.options.secureConnection = !!this.options.secureConnection;
this.options.auth = this.options.auth || false; this.options.auth = this.options.auth || false;
this.options.maxConnections = this.options.maxConnections || 5; this.options.maxConnections = this.options.maxConnections || 5;
this.options.disableDotEscaping = this.options.disableDotEscaping || false;



if(!this.options.name){ if(!this.options.name){
// defaul hostname is machine hostname or [IP] // defaul hostname is machine hostname or [IP]
Expand Down Expand Up @@ -130,6 +132,9 @@ SMTPClient.prototype._init = function(){
* @private * @private
*/ */
this._lastDataBytes = new Buffer(2); this._lastDataBytes = new Buffer(2);
this._lastDataBytes[0] = 0x0D;
this._lastDataBytes[1] = 0x0A;



/** /**
* Function to run if a data chunk comes from the server * Function to run if a data chunk comes from the server
Expand Down Expand Up @@ -425,12 +430,16 @@ SMTPClient.prototype.write = function(chunk){
chunk = new Buffer(chunk, "utf-8"); chunk = new Buffer(chunk, "utf-8");
} }


if(chunk.length > 2){ if (this.options.disableDotEscaping) {
this._lastDataBytes[0] = chunk[chunk.length-2]; if (chunk.length>=2) {
this._lastDataBytes[1] = chunk[chunk.length-1]; this._lastDataBytes[0] = chunk[chunk.length-2];
}else if(chunk.length == 1){ this._lastDataBytes[1] = chunk[chunk.length-1];
this._lastDataBytes[0] = this._lastDataBytes[1]; } else if (chunk.length==1) {
this._lastDataBytes[1] = chunk[0]; this._lastDataBytes[0] = this._lastDataBytes[1]
this._lastDataBytes[1] = chunk[0];
}
} else {
chunk = this._escapeDot(chunk);
} }


if(this.options.debug){ if(this.options.debug){
Expand Down Expand Up @@ -476,6 +485,9 @@ SMTPClient.prototype.end = function(chunk){
}else{ }else{
this.socket.write(new Buffer("\r\n.\r\n")); this.socket.write(new Buffer("\r\n.\r\n"));
} }
this._lastDataBytes[0] = 0x0D;
this._lastDataBytes[1] = 0x0A;



// end data mode // end data mode
this._dataMode = false; this._dataMode = false;
Expand Down Expand Up @@ -1076,3 +1088,28 @@ SMTPClient.prototype.log = function(str) {
} }
}); });
}; };

/**
* <p>Inserts an extra dot at the begining of a line if it starts with a dot
* See RFC 2821 Section 4.5.2</p>
*
* @param {Buffer} chunk The chunk that will be send.
*/
SMTPClient.prototype._escapeDot = function(chunk) {
var pos, OutBuff, i;
OutBuff = new Buffer(chunk.length * 2);
pos = 0;

for (i=0; i<chunk.length; i++) {
if (this._lastDataBytes[0] == 0x0D && this._lastDataBytes[1] == 0x0A && chunk[i] == 0x2E) {
OutBuff[pos] = 0x2E;
pos +=1;
}
OutBuff[pos] = chunk[i];
pos += 1;
this._lastDataBytes[0] = this._lastDataBytes[1];
this._lastDataBytes[1] = chunk[i];
}

return OutBuff.slice(0,pos);
}

0 comments on commit 5153e18

Please sign in to comment.