尝试使用NodeJS实施电子邮件验证

时间:2017-06-22 06:26:00

标签: jquery node.js html5

这是我的示例代码..工作正常..命名为server.js

var nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
let transporter = nodemailer.createTransport({
    host: 'XXXXXX',
    port: 465,
    secure: true, // secure:true for port 465, secure:false for port 587
    auth: {
        user: 'XXXXXXX',
        pass: 'XXXXXXX'
    }
});

// setup email data with unicode symbols
let mailOptions = {
    from: '"me " <aXXXXXXX.com>', // sender address
    to: 'XXXXXXXX.com', // list of receivers
    subject: 'Test Mail from ME using NodeJS', // Subject line
    text: 'Hello world ?', // plain text body
    html: '<b>Hello world ?</b>' // html body
};

// send mail with defined transport object
transporter.sendMail(mailOptions, (error, info) => {
    if (error) {
        return console.log(error);
    }
    console.log('Message %s sent: %s', info.messageId, info.response);
});

使用上面的例子我计划使用网页实现。

这是一个修改过的..命名为server1.js

var express=require('express');
var nodemailer = require("nodemailer");
var app=express();
var bodyParser = require('body-parser');
app.use(bodyParser.json()); // for parsing application/json
app.use(bodyParser.urlencoded({ extended: true })); 
/*
    Here we are configuring our SMTP Server details.
    STMP is mail server which is responsible for sending and recieving email.
*/
var smtpTransport = nodemailer.createTransport({
    host: 'XXXXXX',
    port: 465,
    secure: true, //secure : true for 465, secure: false for port 587 
    auth: {
        user: 'XXXXXXXXXX',
        pass: 'XXXXXXXXXX'
    }
});
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, (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(3000,function(){
    console.log("Express Started on Port 3000");
});

现在是Index.HTML

<html>
<head>
<title>Node.JS Email application</title>
<script src="//ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>
<script>
$(document).ready(function(){
    var from,to,subject,text;
    $("#send_email").click(function(){      
        to=$("#to").val();      
        $("#message").text("Sending E-mail...Please wait");
        $.get("/send",{to: to},function(data){
        if(data=="sent")
        {
            $("#message").empty().html("<p>Email is been sent at "+to+" . Please check inbox !</p>");
        }

});
    });
});
</script>
<style>
#container
{
    margin-left:400px;
    margin-top:50px;
}
#to,#subject,#content
{
    font-family: "Segoe UI";
    font-size:18px;
    width:530px;
}
h1
{
    font-family: "Segoe UI";
    font-size:40px; 
    color: #3b5998;
}
p
{
    color:green;
}
#send_email
{
    font-size:15px;
    font-family: "Segoe UI";
}
#message
{
    font-size:18px;
}
</style>
</head>
<body>
<div id="container">
<h1>Email-verification System in Node.js</h1>
<input type="text" id="to" placeholder="Enter E-mail which you want to verify"><br>
<button id="send_email">Send Email</button><br>
<span id="message"></span>
</div>
</body>
</html>

当我输入电子邮件并单击发送时,它会在网页以及控制台中显示成功消息

但是,我没有收到任何邮件..其中server.js的实现工作得很好..

我不知道我错过了什么,请帮助我解决它。

5 个答案:

答案 0 :(得分:1)

只需按

更改smtpTransport功能
    smtpTransport.sendMail(mailOptions, function(error, response)  {
    if (error) {
        console.log(error);
        res.end("error");
    } else {
        console.log('Message sent: ' + response.message);
        res.end("sent");
    };
});

那应该是有效的。

答案 1 :(得分:0)

使用nodemailer发送的电子邮件可能被电子邮件公司阻止,例如&#39; GMAIL&#39;因为它无法确定该方发送的邮件的安全性。

我遇到了与nodemailer类似的问题,我不得不让gmail用户允许来自不信任方的电子邮件。

答案 2 :(得分:0)

某些域(例如GMail)阻止未知域。在nodejs中使用Mailgun的API将邮件发送到GMail时,我也遇到同样的问题。

答案 3 :(得分:0)

您需要确保两件事

  1. 打开安全性较低的应用程序。

点击此链接 less secure apps

图片- less secure apps - image

  1. 您使用的是应用密码,而不是真实密码。

应用密码看起来像这样的“ caccfssrbxgzvmabb”

要生成一个,您需要启用2因子身份验证。

这是它的链接 Two Factor Authentication

完成此操作后,您将获得下图所示的应用密码选项

App Password - image

生成一个新密码,并在您的配置文件中使用它。

这是我用于gmail帐户的配置。

var smtpTransport = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    secure: true, //secure : true for 465, secure: false for port 587 
    auth: {
        user: '{{email}}',
        pass: '{{app_password}}'
    }
});

注意:就我而言,我还使用了CORS。如有需要,请检查一下。

这是它的链接-CORS NPM

答案 4 :(得分:0)

Web应用程序通常要求用户使用有效的电子邮件地址进行注册。正常工作的电子邮件地址对于诸如重置密码和帐户管理之类的常见任务至关重要。电子邮件验证对于确保注册来自真实用户也至关重要。

相关问题