Skip to content

Commit bb4284e

Browse files
sinchangJacksonTian
authored andcommitted
refactor: mail (#49)
* refactor: mail * chore: lint * ci: fix * test: f * chroe: update config * chore: update package.json * chore: logger
1 parent ce9247a commit bb4284e

File tree

5 files changed

+112
-2
lines changed

5 files changed

+112
-2
lines changed

app/service/mail.js

Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
'use strict';
2+
3+
const Service = require('egg').Service;
4+
const mailer = require('nodemailer');
5+
const smtpTransport = require('nodemailer-smtp-transport');
6+
7+
8+
class MailService extends Service {
9+
async sendMail(data) {
10+
const { config, logger } = this;
11+
12+
if (config.debug) {
13+
return;
14+
}
15+
16+
const transporter = mailer.createTransport(smtpTransport(config.mail_opts));
17+
18+
for (let i = 1; i < 6; i++) {
19+
try {
20+
await transporter.sendMail(data);
21+
logger.info('send mail success', data);
22+
break;
23+
} catch (err) {
24+
if (i === 5) {
25+
logger.error('send mail finally error', err, data);
26+
throw new Error(err);
27+
}
28+
logger.error('send mail error', err, data);
29+
}
30+
}
31+
}
32+
33+
async sendActiveMail(who, token, name) {
34+
const { config } = this;
35+
const from = `${config.name} <${config.mail_opts.auth.user}>`;
36+
const to = who;
37+
const subject = config.name + '社区帐号激活';
38+
const html = '<p>您好:' + name + '</p>' +
39+
'<p>我们收到您在' + config.name + '社区的注册信息,请点击下面的链接来激活帐户:</p>' +
40+
'<a href = "' + config.host + '/active_account?key=' + token + '&name=' + name + '">激活链接</a>' +
41+
'<p>若您没有在' + config.name + '社区填写过注册信息,说明有人滥用了您的电子邮箱,请删除此邮件,我们对给您造成的打扰感到抱歉。</p>' +
42+
'<p>' + config.name + '社区 谨上。</p>';
43+
44+
await this.sendMail({
45+
from,
46+
to,
47+
subject,
48+
html,
49+
});
50+
}
51+
52+
53+
async sendResetPassMail(who, token, name) {
54+
const { config } = this;
55+
const from = `${config.name} <${config.mail_opts.auth.user}>`;
56+
const to = who;
57+
const subject = config.name + '社区密码重置';
58+
const html = '<p>您好:' + name + '</p>' +
59+
'<p>我们收到您在' + config.name + '社区重置密码的请求,请在24小时内单击下面的链接来重置密码:</p>' +
60+
'<a href="' + config.host + '/reset_pass?key=' + token + '&name=' + name + '">重置密码链接</a>' +
61+
'<p>若您没有在' + config.name + '社区填写过注册信息,说明有人滥用了您的电子邮箱,请删除此邮件,我们对给您造成的打扰感到抱歉。</p>' +
62+
'<p>' + config.name + '社区 谨上。</p>';
63+
64+
await this.sendMail({
65+
from,
66+
to,
67+
subject,
68+
html,
69+
});
70+
}
71+
}
72+
73+
module.exports = MailService;
74+

config/config.default.js

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,16 @@
33
module.exports = appInfo => {
44
const config = {};
55

6+
config.name = 'CNode';
7+
68
// debug 为 true 时,用于本地调试
79
config.debug = true;
810

911
// use for cookie sign key, should change to your own and keep security
1012
config.keys = appInfo.name + '_1519887194138_3450';
1113

14+
config.host = 'http://cnodejs.org';
15+
1216
config.session_secret = 'node_club_secret'; // 务必修改
1317

1418
// add your config here
@@ -103,5 +107,16 @@ module.exports = appInfo => {
103107
passwordField: 'pass',
104108
};
105109

110+
// 邮箱配置
111+
config.mail_opts = {
112+
host: 'smtp.126.com',
113+
port: 25,
114+
auth: {
115+
116+
pass: 'club',
117+
},
118+
ignoreTLS: true,
119+
};
120+
106121
return config;
107122
};

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,8 @@
1818
"loader-koa": "^2.0.1",
1919
"lodash": "^4.17.5",
2020
"markdown-it": "^8.4.1",
21+
"nodemailer": "^4.6.2",
22+
"nodemailer-smtp-transport": "^2.7.4",
2123
"uuid": "^3.2.1",
2224
"validator": "^9.4.1",
2325
"xmlbuilder": "^9.0.7"
@@ -39,7 +41,7 @@
3941
"start": "egg-scripts start --daemon --title=egg-server-cnode",
4042
"stop": "egg-scripts stop --title=egg-server-cnode",
4143
"docker": "egg-scripts start --title=egg-server-cnode",
42-
"dev": "egg-bin dev",
44+
"dev": "egg-bin dev",
4345
"debug": "egg-bin debug",
4446
"test": "npm run lint -- --fix && npm run test-local",
4547
"test-local": "egg-bin test",

test/app/controller/sign.test.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ describe('test/app/controller/sign.test.js', () => {
137137
});
138138

139139
assert(res.statusCode === 200);
140-
assert(res.text.includes('欢迎加入 cnode!我们已给您的注册邮箱发送了一封邮件,请点击里面的链接来激活您的帐号'));
140+
assert(res.text.includes('欢迎加入 CNode!我们已给您的注册邮箱发送了一封邮件,请点击里面的链接来激活您的帐号'));
141141
});
142142

143143
it('should POST /signup user or email in use', async () => {

test/app/service/mail.test.js

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
'use strict';
2+
3+
const { app } = require('egg-mock/bootstrap');
4+
5+
describe('test/app/service/mail.js', () => {
6+
describe('sendActiveMail', () => {
7+
it('should ok', async () => {
8+
const ctx = app.mockContext();
9+
await ctx.service.mail.sendActiveMail('[email protected]', 'token', 'sinchang');
10+
});
11+
});
12+
13+
describe('sendResetPassMail', function() {
14+
it('should ok', async () => {
15+
const ctx = app.mockContext();
16+
await ctx.service.mail.sendResetPassMail('[email protected]', 'token', 'sinchang');
17+
});
18+
});
19+
});

0 commit comments

Comments
 (0)