1

我使用 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/

4

2 回答 2

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();
于 2013-10-30T14:20:05.390 回答
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);
        });
于 2013-10-31T18:41:08.823 回答