2013-10-30 51 views
0

我用的NodeJS nodemail發送郵件,所以我有我想要創建事件錯誤和成功的一部分,當郵件發送使用回調的NodeJS nodemailer創建活動

var util = require('util'); 
var event = require('events'); 
var nodemailer = require("nodemailer"); 
var smtpTransport = nodemailer.createTansport("SMTP", { 
    service: "GMAIL", 
    auth: { 
     user : "[email protected]", 
     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 protected]"; 
    mail.to = "[email protected]"; 
    ... 
    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/

+0

到底在哪要補充的回調?您需要確保在發送電子郵件之前註冊成功和錯誤回調。 –

+0

當我運行mail.send()我不知道郵件是否發送。所以我想在運行smtpTransport.sendMail()時添加事件錯誤和成功。所以當我聽mail.send()的錯誤和成功時,我可以知道郵件是否被髮送 – goten

+0

你是否設法讓這個工作@goten? –

回答

1

像這樣的東西應該工作:

var util = require('util'); 
var events = require('events'); 
var nodemailer = require("nodemailer"); 
var smtpTransport = nodemailer.createTansport("SMTP", { 
    service: "GMAIL", 
    auth: { 
     user : "[email protected]", 
     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 protected]"; 
mail.to = "[email protected]"; 

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 protected]", 
    to : "[email protected]", 
    subject : "subject", 
    successCallback : function(success){ 
      console.log("Mail send"); 
     }, 
    errorCallback : function(err){ 
      console.log("Mail not send"); 
     } 
}); 

mail.send(); 
+0

感謝您的回答,我正在尋找更多像第一個一樣的東西。但第二個對於我想要做的其他事情也很有意思 – goten

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 protected]", 
     to : "[email protected]", 
     subject : "subject" 
     }); 
     mail.send(); 
     mail 
     .on('success', function(res){ 
      console.log(res.message); 
     });