首页 / NODEJS / NodeJs实现邮箱验证
NodeJs实现邮箱验证
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了NodeJs实现邮箱验证,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含3646字,纯文字阅读大概需要6分钟。
内容图文
![NodeJs实现邮箱验证](/upload/InfoBanner/zyjiaocheng/633/c3c5d2ab4b6848f79ecfca23871c780b.jpg)
要想实现发送邮件功能,主要是运用nodemailer
这个依赖包,官网地址:https://nodemailer.com/about/
utils.js 代码:
module.exports = {
mail
}
/**
* 发送邮件
* @param {string} to 收件方邮箱
* @param {string} title 内容标题
* @param {string} content 邮件内容
* @param {Function} callback 回调函数(内置参数)
*
*/
function mail(to,title,content,callback) {
const nodemailer = require('nodemailer'); //引入依赖
/**
* 详细配置文件地址: node_modules/lib/well-known/services
*/
let transporter = nodemailer.createTransport({
host: 'smtp.163.com',
port: 465,
secure: true,
auth: {
user: 'xxxx@163.com', //发送方邮箱
pass: 'xxx' //发送方邮箱的授权码,一般去邮箱设置里面找,应该可以找到
}
});
let info = {
from: 'xxxx@163.com',//发送方邮箱
to: to,
subject: title,
text: content
//html: '<h1>这里内容</h1>',text和html任选其一即可
}
//发送邮件
transporter.sendMail(info,(err,data) => {
callback && callback(err,data)
})
}
NodeJs代码:
const http = require('http');
const path = require('path');
const fs = require('fs');
const url = require('url');
const utils = require(path.resolve(__dirname,'./utils.js'));
let time = null; //全局存储时间戳
let code = null; //全局存储验证码
const server = http.createServer(function (req,res) {
if (req.url == '/') {
fs.readFile('./index.html','utf8',function (err,data) {
res.writeHead(200,{'Content-type': 'text/html'});
if (err) {
res.write('文件读取错误!');
} else {
res.write(data);
}
res.end();
})
}
if (url.parse(req.url).pathname == '/date') {
let query = url.parse(req.url,true).query;
let mail = query.mail; //获取前端传递的邮箱信息
code = Math.floor(Math.random() * 1000); //存入验证码
time = new Date().getTime(); //存入发送验证码的时间戳
utils.mail(mail,'验证码',code + '',function (err,data) {
res.writeHead(200);
if (err) {
res.write('验证码发送失败')
} else {
res.write('验证码发送成功')
}
res.end();
});
}
if (url.parse(req.url).pathname == '/code') {
let query = url.parse(req.url,true).query;
let cd = query.code; //传递用户输入的验证码
let ct = query.time; //传递用户点击提交验证码的时间戳
res.writeHead(200);
console.log('code:' + code,'cd:' + cd, 'time:' + time, 'ct:' + ct)
//判断是否超过规定时间
if (ct - time >= 1 * 60 * 1000) {
res.write('验证码已过期')
} else {
if (cd == code) {
res.write('验证通过')
} else {
res.write('验证码错误')
}
}
res.end();
}
});
server.listen('80','localhost',() => {
console.log('start server!');
})
html代码:
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>邮箱注册验证</title>
<script src="https://code.jquery.com/jquery-2.2.4.js" integrity="sha256-iT6Q9iMJYuQiMWNd9lDyBUStIq/8PuOW33aOqmvFpqI=" crossorigin="anonymous"></script>
</head>
<body>
<input type="text" placeholder="请输入邮箱" class="input">
<button class="sendCode">发送验证码</button>
<input type="text" placeholder="邮箱验证码" class="code">
<button class="btn">提交</button>
<script>
var inp = document.getElementsByClassName('input')[0];
var code = document.getElementsByClassName('code')[0];
var btn = document.getElementsByClassName('btn')[0];
var sendCode = document.getElementsByClassName('sendCode')[0];
sendCode.onclick = function () {
console.log('发送验证码');
$.ajax({
url: 'http://localhost/date',
async: false,
data: {
mail: inp.value
},
success: function (res) {
console.log(res);
},
error: function (err) {
console.log(err);
}
})
}
//提交验证码
btn.onclick = function () {
$.ajax({
url: 'http://localhost/code',
async: false,
data: {
code: code.value,
time: new Date().getTime()
},
success: function (res) {
console.log(res);
},
error: function (err) {
console.log(err);
}
})
}
</script>
</body>
</html>
目录结构
'|-- 邮箱验证',
' |-- index.html',
' |-- index.js',
' |-- package-lock.json',
' |-- utils.js',
内容总结
以上是互联网集市为您收集整理的NodeJs实现邮箱验证全部内容,希望文章能够帮你解决NodeJs实现邮箱验证所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。