2016-07-08 134 views
1

我正在填寫表格,填寫申請表需要在填寫表格之前創建一個帳戶。唯一的問題是如何阻止垃圾郵件申請人使用假郵件創建帳戶。是否有可能通過航行驗證電子郵件。我使用節點郵件工具快速完成了這項工作。電子郵件在sail.js中驗證

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"); 
}); 

任何幫助將不勝感激謝謝

+0

請不要發佈鏈接,如果你能幫助我用一個例子或一些東西,解釋我的查詢好得多感謝失敗.. –

回答

0

你應該能夠在航行使用nodemailer幾乎相同,只是改變了app.get s轉換相應的控制器動作。

MailController.js:

  1. 爲第一用戶請求:

    module.exports = { 
    
        sendVerificationMail: function(req, res) { 
         // your app.get('/send') code 
        }, 
    
        verifyEmail: function(req, res) { 
         // your app.get('/verify') code 
        } 
    
    } 
    

    作爲一個側面說明,您的驗證邏輯,當另一個用戶試圖第一個已經完成了他在登記前登記有點兒打破電子郵件驗證,例如rand = 34

  2. 第二位用戶請求郵件驗證,rand = 58
  3. 首先用戶試圖驗證自己的電子郵件id=34,驗證因爲34 !== 58
+0

謝謝對於這個偉大的份額,你能幫我出一些驗證技巧,這樣可以使這個更強大。 –