我使用nodejs nodemail发送邮件,所以我想在发送邮件而不是使用回调时创建事件错误和成功
var util = require('util');
var event = require('events');
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTansport("SMTP", {
service: "GMAIL",
auth: {
user : "mail@mail.com",
pass : "pass"
}
});
mail.exports = function(from, to, subject ...){
this.from = from;
this.to = to;
this.subject = subject;
....
this.send = function(){
var options = {
from = this.from;
to = this.to;
};
var obj = this.send;
event.Eventemitter.call(obj);
smtpTransport.sendMail(options, function(err, success) {
if(err){
obj.emit('error', err);
}
if(success){
obj.emit('success', success);
}
});
}
};
然后调用邮件服务
var Mail = require('./mail');
var mail = new Mail();
mail.from = "email@mail.com";
mail.to = "email@mail.com";
...
mail.send(); //the mail is send
邮件已发送,但我不知道邮件是否发送
mail.send()
.on("error", function(err){
console.log("Mail not send");
})
.on("success", function(success){
console.log("Mail send");
});
我跟着下面的tuto http://www.sitepoint.com/nodejs-events-and-eventemitter/
答案 0 :(得分:2)
这样的事情应该有效:
var util = require('util');
var events = require('events');
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTansport("SMTP", {
service: "GMAIL",
auth: {
user : "mail@mail.com",
pass : "pass"
}
});
module.exports = function(from, to, subject){
this.from = from;
this.to = to;
this.subject = subject;
this.events = new events.EventEmitter();
//....
this.send = function(){
var options = {
from : this.from,
to : this.to
};
smtpTransport.sendMail(options, function(err, success) {
if(err){
this.events.emit('error', err);
}
if(success){
this.events.emit('success', success);
}
});
}
};
然后像这样使用它:
var Mail = require('./mail');
var mail = new Mail();
mail.from = "email@mail.com";
mail.to = "email@mail.com";
mail.events.on("error", function(err){
console.log("Mail not send");
});
mail.events.on("success", function(success){
console.log("Mail send");
});
mail.send();
我目前无法访问gmail来测试发送等,但这应该是正常的。
我修复的初始示例中存在各种语法问题。
要记住的主要事情是你需要将EventEmitter暴露给客户端代码,以便他们可以注册回调并确保在发送电子邮件之前注册回调。
通过传入成功和失败回调函数,您也可以在不使用EventEmitter的情况下执行此操作。这将使它更简单。
module.exports = function(params){
this.from = params.from;
this.to = params.to;
this.subject = params.subject;
this.successCallback = params.successCallback;
this.errorCallback= params.errorCallback;
this.send = function(){
var options = {
from : this.from,
to : this.to
};
smtpTransport.sendMail(options, function(err, success) {
if(err){
this.errorCallback(err);
}
else{
this.successCallback(success);
}
});
}
};
调用它看起来像这样:
var Mail = require('./mail');
var mail = new Mail({
from : "email@mail.com",
to : "email@mail.com",
subject : "subject",
successCallback : function(success){
console.log("Mail send");
},
errorCallback : function(err){
console.log("Mail not send");
}
});
mail.send();
答案 1 :(得分:0)
var nodemailer = require("nodemailer");
var smtpTransport = nodemailer.createTransport("SMTP",{
service: params.data.service,
auth: {
user: params.data.user,
pass: params.data.pass
}
});
/*
* Events
*/
var event = require('events').EventEmitter;
var util = require('util');
/*
* Export of the module mail
*/
function Mail(from, to, cc, bcc, replyTo, subject, text, html, generateTextFromHTML, headers){
this.from = from;
this.to = to;
this.cc = cc;
this.bcc = bcc;
this.replyTo = replyTo;
this.subject = subject;
this.text = text;
this.html = html;
this.generateTextFromHTML = generateTextFromHTML;
this.headers = headers;
var self = this;
event.call(self);
this.send = function() {
var options = {
from : this.from,
to : this.to,
cc : this.cc,
bcc : this.bcc,
replyTo : this.replyTo,
subject : this.subject,
text : this.text,
html : this.html,
generateTextFromHTML : this.generateTextFromHTML,
headers : this.headers
};
smtpTransport.sendMail(options, function(err, success){
if(err){
self.emit('error', err);
}
if(success){
self.emit('success', success);
}
});
};
}
util.inherits(Mail, event);
module.exports = Mail;
致电:
var Mail = require('mail');
var mail = new Mail({
from : "email@mail.com",
to : "email@mail.com",
subject : "subject"
});
mail.send();
mail
.on('success', function(res){
console.log(res.message);
});