nodemailer - How can I form a wrapper for another module in Node.JS? -


i have following custom module using wrapper nodemailer

var nodemailer = require("nodemailer");  function emailer(to, subject, message) {   this.to = to;   this.subject = subject;   this.message = message;   this.smtptransport = nodemailer.createtransport("smtp",{       service: "gmail",  // sets automatically host, port , connection security settings       auth: {         user: "*******",         pass: "*******"       }     });   this.send = send;   function send() {     this.smtptransport.sendmail({  //email options       from: "******* <*****>", // sender address.  must same authenticated user if using gmail.       to: this.to, // receiver       subject: this.subject, // subject       text: this.message // body     }, function(error, response){  //callback       if(error){         //console.log(error);       }else{         //console.log("message sent: " + response.message);       }        smtptransport.close(); // shut down connection pool, no more messages.  comment line out continue sending emails.     });   }; }  module.exports = emailer; 

i implementing this:

var emailer = require('./models/emailer.js'); var myemailer = new emailer('--------', 'my subject', 'my message'); myemailer.send(); 

it works, still error:

referenceerror: smtptransport not defined     @ mailcomposer.returncallback (/users/drewwyatt/sites/js/node/tutorials/email/models/emailer.js:28:7) 

what doing wrong?

the problem line:

smtptransport.close();  

you can avoid referenceerror binding callback context of mailer.

this.smtptransport.sendmail({ /* options */ }, function (err, response) {   // stuff   this.smtptransport.close(); }.bind(this)); 

it make more sense put send method on prototype, because uses state of instance.

emailer.prototype.send = function () {   // this.smtptransport ... }; 

finally, name email instead of emailer :)


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -