【问题标题】:Email verify in sail.js在sail.js 中进行电子邮件验证
【发布时间】:2016-11-10 11:54:06
【问题描述】:

我正在尝试创建一个用于填写表格的门户,申请人需要在填写表格之前为其创建一个帐户。唯一的问题是我如何才能停止使用假邮件向创建帐户的申请人发送垃圾邮件。是否可以在航行中验证电子邮件。我已经使用节点邮件程序以快递方式完成了这项工作。

var express = require('express');
var nodemailer= require('nodemailer');
    var app = express();

    var smtpTransport = nodemailer.createTransport("SMTP", {
        service: "Gmail",
        auth: {
            user: "email",
            pass: "pass"
        }
    });
    var rand, mailOptions, host, link;

/*---SMTP OVER---*/

/*--Routing Started--*/
    app.get('/', function(req , res) {
        res.sendfile('index.html');
    });

    app.get('/send', function(req , res) {
        rand=Math.floor((Math.random() * 100) + 54);
        host= req.get(host);
        link="http://"+req.get('host')+"/verify?id="+rand;
        mailOptions={
            to : req.query.to,
            subject : "Please confirm your Email account",
            html : "Hello,<br> Please Click on the link to verify your email.<br><a href="+link+">Click here to verify</a>"
        }
        console.log(mailOptions);
        smtpTransport.sendMail(mailOptions, function(error, response){
            if(error){
                console.log(error);
                res.end("error");
            }else{
                console.log("Message sent: " + response.message);
                res.end("sent");
            }
        });
    });

app.get('/verify',function(req,res){
    console.log(req.protocol+":/"+req.get('host'));
    if((req.protocol+"://"+req.get('host'))==("http://"+host))
    {
        console.log("Domain is matched. Information is from Authentic email");
        if(req.query.id==rand)
        {
            console.log("email is verified");
            res.end("<h1>Email "+mailOptions.to+" is been Successfully verified");
        }
        else
        {
            console.log("email is not verified");
            res.end("<h1>Bad Request</h1>");
        }
    }
    else
    {
        res.end("<h1>Request is from unknown source");
    }
});

/*--------------------Routing Over----------------------------*/

app.listen(9999,function(){
    console.log("Express Started on Port 3000");
});

任何帮助将不胜感激谢谢

【问题讨论】:

  • 请不要发布链接,如果你能帮我举个例子或更好的东西来解释我的查询。谢谢

标签: angularjs node.js sails.js


【解决方案1】:

您应该可以在sails 中使用几乎相同的nodemailer,只需将app.gets 更改为相应的控制器操作即可。

MailController.js:

module.exports = {

    sendVerificationMail: function(req, res) {
        // your app.get('/send') code
    },

    verifyEmail: function(req, res) {
        // your app.get('/verify') code
    }

}

附带说明一下,当另一个用户在第一个用户完成注册之前尝试注册时,您的验证逻辑有点中断:

  1. 第一个用户请求电子邮件验证,例如rand = 34
  2. 第二个用户请求电子邮件验证,rand = 58
  3. 第一个用户尝试使用id=34 验证他的电子邮件,验证失败,因为34 !== 58

【讨论】:

  • 感谢您的精彩分享,您能否帮我提供一些验证提示,以便使其更加强大。
猜你喜欢
  • 2011-03-21
  • 1970-01-01
  • 1970-01-01
  • 2011-05-11
  • 2017-11-19
  • 2011-01-31
  • 2014-03-05
相关资源
最近更新 更多