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