1 var spawn = require('child_process').spawn;
  2 
  3 // Expose to the world
  4 module.exports = SendmailTransport;
  5 
  6 /**
  7  * <p>Generates a Transport object for Sendmail</p>
  8  * 
  9  * @constructor
 10  * @param {String} [config] path to the sendmail command
 11  */
 12 function SendmailTransport(config){
 13     this.path = typeof config=="string"?config:"sendmail";
 14 }
 15 
 16 /**
 17  * <p>Spawns a <code>'sendmail -t'</code> command and streams the outgoing
 18  * message to sendmail stdin. Return callback checks if the sendmail process
 19  * ended with 0 (no error) or not.</p>
 20  * 
 21  * @param {Object} emailMessage MailComposer object
 22  * @param {Function} callback Callback function to run when the sending is completed
 23  */
 24 SendmailTransport.prototype.sendMail = function(emailMessage, callback) {
 25 
 26     // sendmail strips this header line by itself
 27     emailMessage.options.keepBcc = true;
 28     
 29     var sendmail = spawn(this.path, ["-t"]);
 30     
 31     sendmail.on('exit', function (code) {
 32         callback(code?new Error("Sendmail exited with "+code):null);
 33     });
 34     
 35     emailMessage.pipe(sendmail.stdin);
 36     emailMessage.streamMessage();
 37     
 38 };