I use nodejs nodemail to send mail so I have in a part I want to create event error and success when a mail is send instead of using the callback

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

then a call the mail service

var Mail = require('./mail');
var mail = new Mail();
    mail.from = "email@mail.com";
    mail.to = "email@mail.com";
    ...
    mail.send(); //the mail is send

the mail is send but I don't know if the mail send or not

    mail.send()
     .on("error", function(err){
         console.log("Mail not send");
     })
     .on("success", function(success){
         console.log("Mail send");
     });

I followed the following tuto http://www.sitepoint.com/nodejs-events-and-eventemitter/

有帮助吗?

解决方案

Something like this should work:

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

Then use it like this:

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

I don't have access to gmail at the moment to test the sending etc, but this should work in principal.

There are various syntax issues in your initial example which I fixed.

The main thing to bare in mind is that you need to expose the EventEmitter to the client code so that they can register for callbacks and be sure to register the callbacks before sending the email.

You can do this without using the EventEmitter as well, by passing in success and failure callback functions. Which will make it a bit simpler.

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

Calling it looks like this:

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

其他提示

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;

Calling :

  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);
        });
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top