Permalink
Browse files

邮件重试逻辑

  • Loading branch information...
1 parent 46d23ad commit ab6ac38614fb735a8ad780ab67cfff5afc4a41cb @alsotang alsotang committed Nov 8, 2016
Showing with 19 additions and 7 deletions.
  1. +16 −5 common/mail.js
  2. +3 −2 config.default.js
View
@@ -5,6 +5,7 @@ var util = require('util');
var logger = require('./logger');
var transporter = mailer.createTransport(smtpTransport(config.mail_opts));
var SITE_ROOT_URL = 'http://' + config.host;
+var async = require('async')
/**
* Send an email
@@ -14,13 +15,23 @@ var sendMail = function (data) {
if (config.debug) {
return;
}
- // 遍历邮件数组,发送每一封邮件,如果有发送失败的,就再压入数组,同时触发mailEvent事件
- transporter.sendMail(data, function (err) {
+
+ // 重试5次
+ async.retry({times: 5}, function (done) {
+ transporter.sendMail(data, function (err) {
+ if (err) {
+ // 写为日志
+ logger.error('send mail error', err, data);
+ return done(err);
+ }
+ return done()
+ });
+ }, function (err) {
if (err) {
- // 写为日志
- logger.error(err);
+ return logger.error('send mail finally error', err, data);
}
- });
+ logger.info('send mail success', data)
+ })
};
exports.sendMail = sendMail;
View
@@ -65,11 +65,12 @@ var config = {
// 邮箱配置
mail_opts: {
host: 'smtp.126.com',
- port: 465,
+ port: 25,
auth: {
user: 'club@126.com',
pass: 'club'
- }
+ },
+ ignoreTLS: true,
},
//weibo app key

0 comments on commit ab6ac38

Please sign in to comment.