• NodeJs实现邮箱验证


    要想实现发送邮件功能,主要是运用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',
    
  • 相关阅读:
    JVM -- Full GC触发条件总结以及解决策略
    java实现-图的相关操作
    Integer的intValue()方法
    Java transient关键字
    Redis 单线程模型介绍
    String类的intern()方法 -- 重用String对象,节省内存消耗
    数据库阿里连接池 druid配置详解
    redis 实现发布/订阅模式
    Redis实现队列
    redis 实现分布式锁
  • 原文地址:https://www.cnblogs.com/fanzhikang/p/12836793.html
Copyright © 2020-2023  润新知