发送邮件时,不等待邮件发送成功先通知用户,避免用户等待时间过长。邮件压入队列,如果失败1分钟后再次发送

This commit is contained in:
dead-horse 2012-03-13 19:11:37 +08:00
parent 0bb2ecfe05
commit da44045184

View File

@ -1,6 +1,7 @@
var mailer = require('nodemailer');
var config = require('../config').config;
var EventProxy = require('eventproxy').EventProxy;
var util = require('util');
mailer.SMTP = {
host: config.mail_host,
port: config.mail_port,
@ -9,13 +10,75 @@ mailer.SMTP = {
pass: config.mail_pass
};
function send_mail(data,cb){
mailer.send_mail(data,function(err,success){
return cb(err,success);
});
/**
* keep all the mails to send
* @type {Array}
*/
var mails = [];
var timer;
/**
* control mailer
* @type {EventProxy}
*/
var mailEvent = new EventProxy();
/**
* when need to send an email, start to check the mails array and send all of emails.
*/
mailEvent.on("getMail", function() {
if(mails.length === 0) {
return;
} else {
//遍历邮件数组发送每一封邮件如果有发送失败的就再压入数组同时触发mailEvent事件
var failed = false;
for(var i = 0, len = mails.length; i != len; ++i) {
var message = mails[i];
mails.splice(i, 1);
i--;
len--;
var mail;
try {
message.debug = false;
mail = mailer.send_mail(message, function(error, success) {
if(error) {
mails.push(message);
failed = true;
}
});
} catch(e) {
mails.push(message);
failed = true;
}
if(mail) {
var oldemit = mail.emit;
mail.emit = function() {
oldemit.apply(mail, arguments);
}
}
}
if(failed) {
clearTimeout(timer);
timer = setTimeout(trigger, 60000);
}
}
});
/**
* trigger email event
* @return {[type]}
*/
function trigger() {
mailEvent.trigger("getMail");
}
/**
* send an email
* @param {mail} data [info of an email]
*/
function send_mail (data) {
mails.push(data);
trigger();
}
function send_active_mail(who,token,name,email,cb){
function send_active_mail(who, token, name, email, cb) {
var sender = config.mail_sender;
var to = who;
var subject = config.name + '社区帐号激活';
@ -31,12 +94,10 @@ function send_active_mail(who,token,name,email,cb){
subject: subject,
html: html
}
send_mail(data,function(err,success){
return cb(err,success);
});
cb (null, true);
send_mail(data);
}
function send_reset_pass_mail(who,token,name,cb){
function send_reset_pass_mail(who, token, name, cb) {
var sender = config.mail_sender;
var to = who;
var subject = config.name + '社区密码重置';
@ -53,11 +114,11 @@ function send_reset_pass_mail(who,token,name,cb){
html: html
}
send_mail(data,function(err,success){
return cb(err,success);
});
cb (null, true);
send_mail(data);
}
function send_reply_mail(who,msg){
function send_reply_mail(who, msg) {
var sender = config.mail_sender;
var to = who;
var subject = config.name + ' 新消息';
@ -76,10 +137,11 @@ function send_reply_mail(who,msg){
html: html
}
send_mail(data,function(err,success){});
send_mail(data);
}
function send_at_mail(who,msg){
function send_at_mail(who, msg) {
var sender = config.mail_sender;
var to = who;
var subject = config.name + ' 新消息';
@ -98,7 +160,7 @@ function send_at_mail(who,msg){
html: html
}
send_mail(data,function(err,success){});
send_mail(data);
}
exports.send_active_mail = send_active_mail;