gpt4 book ai didi

node.js - Nodejs Nodemailer 创建事件

转载 作者:太空宇宙 更新时间:2023-11-03 23:42:56 24 4
gpt4 key购买 nike

我使用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");
});

我遵循了以下教程 http://www.sitepoint.com/nodejs-events-and-eventemitter/

最佳答案

这样的事情应该有效:

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();

关于node.js - Nodejs Nodemailer 创建事件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19682001/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com