可以使用
nodemailer
这个模块提供的能力完成一些诸如邮箱登录、找回密码等功能的开发。 项目地址:https://github.com/Ewall1106/mall
qq邮箱 > 设置 > 账户
中,我们将SMTP服务
开启。(默认是关闭的)授权码
。$ npm install nodemailer
'use strict';
const nodemailer = require('nodemailer');
async function main() {
let transporter = nodemailer.createTransport({
// 使用qq的smtp服务器
host: 'smtp.qq.com',
port: 587,
secure: false,
auth: {
user: '这里填入你的邮箱',
pass: '这里填入上一步生成得到的授权码',
},
});
// 配置邮件标题、内容等
// 这里我自己给自己发送一封 Test 测试邮件
let info = await transporter.sendMail({
from: '认证邮件',
to: '123456@qq.com',
subject: 'Test',
text: 'Hello world',
html: '<b>Hello world</b>',
});
console.log('Message sent:', info.messageId);
console.log('Preview URL:', nodemailer.getTestMessageUrl(info));
}
main().catch(console.error);
js
文件:$ node mail.js
async sendMail(ctx) {
const { email } = ctx.request.body;
try {
// 随机生成一个验证码
const code = 1234;
// 将上面 `nodemailer` 发送邮箱的方法封装后在这里调用
sendMail()
// 设置缓存key-value键值对并设置过期时间
setValue(email, code, 60 * 60 * 24);
ctx.body = {
code: 200,
entry: '邮箱验证码已经发送成功',
};
} catch (err) {
// handle error...
}
}
注册
按钮的时候对其校验。async registry(ctx, next) {
const { email, password,mailcode } = ctx.request.body;
// 从redis缓存中获取code
const code = await getValue(email);
// 比对验证
if (!code) {
ctx.body = {
code: 400,
message: '请点击发送验证码重新发送',
};
return;
}
if (code !== mailcode) {
ctx.body = {
code: 400,
message: '请输入正确的邮箱验证码',
};
return;
}
// ...
}
扫码关注腾讯云开发者
领取腾讯云代金券
Copyright © 2013 - 2025 Tencent Cloud. All Rights Reserved. 腾讯云 版权所有
深圳市腾讯计算机系统有限公司 ICP备案/许可证号:粤B2-20090059 深公网安备号 44030502008569
腾讯云计算(北京)有限责任公司 京ICP证150476号 | 京ICP备11018762号 | 京公网安备号11010802020287
Copyright © 2013 - 2025 Tencent Cloud.
All Rights Reserved. 腾讯云 版权所有