-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmail.js
65 lines (59 loc) · 1.63 KB
/
mail.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const nodemailer = require('nodemailer')
const db = require('./db.js')
const utils = require('./utils.js')
const Config = require('./config.js')
const sendMail = {
sendEmail: async mail => {
const transporter = nodemailer.createTransport({
service: '163',
auth: {
user: Config.mailAddress, // 发送方邮箱的账号
pass: Config.mailPassword, // 邮箱授权密码
},
})
const captchaCode = Math.random().toString().substring(2, 6)
// 定义transport对象并发送邮件
await transporter.sendMail({
from: `"Tab Extentions" <${Config.mailAddress}>`, // 发送方邮箱的账号
to: mail, // 邮箱接受者的账号
subject: 'Tab Extentions', // Subject line
html: utils.emailTemplate(captchaCode),
})
db.sendRegisterEmail(mail, captchaCode)
return captchaCode
},
verifyCode: async (mail, code) => {
if (!code) {
return {
error: 1,
msg: '验证码错误',
}
}
let result = {
error: 0,
msg: '验证成功',
}
// 判断验证码有效性
if (code === Config.codeWhite) return result
const hasValideData = await db.verifyRegisterCode(mail, code)
if (hasValideData) {
const sendTime = hasValideData.sendTime.getTime()
const now = Date.now()
const min_ms = 60 * 1000
const rang = (now - sendTime) / min_ms
if (rang > 15) {
result = {
error: 1,
msg: '验证码已过期',
}
}
} else {
result = {
error: 1,
msg: '验证码错误',
}
}
return result
},
}
module.exports = sendMail