I have the following custom module I am using as a wrapper for NodeMailer
我有以下自定义模块作为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 and connection security settings
auth: {
user: "*******",
pass: "*******"
}
});
this.send = send;
function send() {
this.smtpTransport.sendMail({ //email options
from: "******* <*****>", // sender address. Must be the same as 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 the connection pool, no more messages. Comment this line out to continue sending emails.
});
};
}
module.exports = Emailer;
I am implementing it like this:
我这样实现它:
var emailer = require('./models/emailer.js');
var myEmailer = new emailer('--------', 'my subject', 'my message');
myEmailer.send();
It works, but I still get this error:
它工作,但我仍然得到这个错误:
ReferenceError: smtpTransport is not defined
at MailComposer.returnCallback (/Users/drewwyatt/Sites/JS/Node/Tutorials/email/models/emailer.js:28:7)
What am I doing wrong?
我究竟做错了什么?
2 个解决方案
#1
2
The problem is this line:
问题是这一行:
smtpTransport.close();
You can avoid the ReferenceError
by binding the callback to the context of your mailer.
您可以通过将回调绑定到邮件程序的上下文来避免ReferenceError。
this.smtpTransport.sendMail({ /* options */ }, function (err, response) {
// do stuff
this.smtpTransport.close();
}.bind(this));
It would also make more sense to put send
as a method on the prototype, because it uses the state of the instance.
将send作为方法放在原型上也更有意义,因为它使用了实例的状态。
Emailer.prototype.send = function () {
// this.smtpTransport ...
};
Finally, I would name it Email
instead of Emailer
:)
最后,我将它命名为Email而不是Emailer :)
#2
1
...
var self = this;
function send() {
this.smtpTransport.sendMail({ //email options
from: "******* <*****>", // sender address. Must be the same as 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);
}
self.smtpTransport.close(); // shut down the connection pool, no more messages. Comment this line out to continue sending emails.
});
#1
2
The problem is this line:
问题是这一行:
smtpTransport.close();
You can avoid the ReferenceError
by binding the callback to the context of your mailer.
您可以通过将回调绑定到邮件程序的上下文来避免ReferenceError。
this.smtpTransport.sendMail({ /* options */ }, function (err, response) {
// do stuff
this.smtpTransport.close();
}.bind(this));
It would also make more sense to put send
as a method on the prototype, because it uses the state of the instance.
将send作为方法放在原型上也更有意义,因为它使用了实例的状态。
Emailer.prototype.send = function () {
// this.smtpTransport ...
};
Finally, I would name it Email
instead of Emailer
:)
最后,我将它命名为Email而不是Emailer :)
#2
1
...
var self = this;
function send() {
this.smtpTransport.sendMail({ //email options
from: "******* <*****>", // sender address. Must be the same as 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);
}
self.smtpTransport.close(); // shut down the connection pool, no more messages. Comment this line out to continue sending emails.
});