1 var SendmailTransport = require("./engines/sendmail"), 2 SMTPTransport = require("./engines/smtp"), 3 SESTransport = require("./engines/ses"); 4 5 6 // Expose to the world 7 module.exports.Transport = Transport; 8 9 /** 10 * <p>Generates a Transport object that can be used to deliver e-mail.</p> 11 * 12 * <p>All transport objects need to have <code>sendMail</code> property defined 13 * and if needed, also an <code>close</code> method</p> 14 * 15 * @constructor 16 * @param {String} type The type of the transport, currently available: SMTP, SES and Sendmail 17 */ 18 function Transport(type, options){ 19 20 this.options = options; 21 22 switch((type || "").toString().trim().toUpperCase()){ 23 case "SMTP": 24 this.transport = new SMTPTransport(this.options); 25 break; 26 case "SES": 27 this.transport = new SESTransport(this.options); 28 break; 29 case "SENDMAIL": 30 this.transport = new SendmailTransport(this.options); 31 break; 32 default: 33 this.transport = false; 34 } 35 36 } 37 38 /** 39 * <p>Forwards the generated mailcomposer object to the selected transport 40 * object for message delivery</p> 41 * 42 * @param {Object} emailMessage MailComposer object 43 * @param {Function} callback Callback function to run when the sending is completed 44 */ 45 Transport.prototype.sendMail = function(emailMessage, callback){ 46 if(!this.transport){ 47 return callback(new Error("Invalid transport method defined")); 48 } 49 50 this.transport.sendMail(emailMessage, callback); 51 }; 52 53 /** 54 * <p>Closes the transport when needed, useful with SMTP (which uses connection 55 * pool) but not so much with SES or Sendmail</p> 56 * 57 * @param {Function} Callback function to run when the connection is closed 58 */ 59 Transport.prototype.close = function(callback){ 60 if(!this.transport){ 61 return callback(new Error("Invalid transport method defined")); 62 } 63 64 if(typeof this.transport.close == "function"){ 65 this.transport.close(callback); 66 }else{ 67 callback(null); 68 } 69 };