52 lines
1.7 KiB
JavaScript
52 lines
1.7 KiB
JavaScript
|
|
import nodemailer from 'nodemailer'
|
|||
|
|
import config from './config/index.js'
|
|||
|
|
import * as log4js from './log4js.js'
|
|||
|
|
|
|||
|
|
const transporter = nodemailer.createTransport({
|
|||
|
|
host: config.mail.host,
|
|||
|
|
port: config.mail.port,
|
|||
|
|
secure: config.mail.secure,
|
|||
|
|
auth: {
|
|||
|
|
user: config.mail.from,
|
|||
|
|
pass: config.mail.password,
|
|||
|
|
},
|
|||
|
|
})
|
|||
|
|
|
|||
|
|
/**
|
|||
|
|
* 发送验证码邮件
|
|||
|
|
* @param {string} to 收件人邮箱
|
|||
|
|
* @param {string} account 游戏账号
|
|||
|
|
* @param {string} code 验证码
|
|||
|
|
* @param {number} type 1=注册 2=找回密码
|
|||
|
|
*/
|
|||
|
|
export async function sendCodeMail(to, account, code, type) {
|
|||
|
|
const typeNames = { 1: '注册', 2: '找回密码' }
|
|||
|
|
const typeName = typeNames[type] || '验证'
|
|||
|
|
const subject = `【${config.game.name}】${typeName}`
|
|||
|
|
const html = `
|
|||
|
|
<div style="background:#000;padding:50px;min-height:300px;">
|
|||
|
|
<div style="background:rgba(0,0,0,.7);border-radius:6px;color:#fff;padding:25px;max-width:450px;">
|
|||
|
|
<h3>${subject}</h3>
|
|||
|
|
<p>您的${config.account.name}${config.account.nameSuffix}:<strong>${account}</strong></p>
|
|||
|
|
<p>您的验证码:<span style="font-weight:700;font-size:18px;text-decoration:underline;color:#f5bd10;">${code}</span></p>
|
|||
|
|
<p style="color:#aaa;">用于${typeName}验证,5分钟内有效。</p>
|
|||
|
|
<hr style="border-color:#333;"/>
|
|||
|
|
<p style="font-size:12px;color:#666;">${config.game.name} · ${config.game.description}</p>
|
|||
|
|
</div>
|
|||
|
|
</div>
|
|||
|
|
`
|
|||
|
|
try {
|
|||
|
|
await transporter.sendMail({
|
|||
|
|
from: `"${config.game.name}" <${config.mail.from}>`,
|
|||
|
|
to,
|
|||
|
|
subject,
|
|||
|
|
html,
|
|||
|
|
})
|
|||
|
|
log4js.koa.info(`验证码邮件发送成功 -> ${to}`)
|
|||
|
|
return true
|
|||
|
|
} catch (err) {
|
|||
|
|
log4js.koa.error(`验证码邮件发送失败 -> ${to}`, err.message)
|
|||
|
|
return false
|
|||
|
|
}
|
|||
|
|
}
|