【问题标题】:Trying to Implement Email Verification using NodeJS尝试使用 NodeJS 实现电子邮件验证
【发布时间】:2018-10-29 00:57:49
【问题描述】:

这是我的示例代码.. 工作正常.. 命名为 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");
});

现在索引.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 的实现工作正常..

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

【问题讨论】:

    标签: jquery node.js html


    【解决方案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");
        };
    });
    

    应该可以的。

    【讨论】:

    • 让我试试然后回复你
    【解决方案2】:

    使用 nodemailer 发送的电子邮件可能会被电子邮件公司(例如“GMAIL”)阻止,因为它无法识别该方发送的邮件的安全程度。

    我在使用 nodemailer 时遇到了类似的问题,我必须让 gmail 用户允许来自不受信任方的电子邮件。

    【讨论】:

    • 在 server.js 中,gmail 也可以正常工作,因为我更改了允许安全应用程序的选项。此外,我没有将 gmail 用于邮寄目的,为此有一些私人主机......这也适用于服务器。 js
    【解决方案3】:

    GMail 等一些域会阻止未知域。在 nodejs 中使用 Mailgun 的 API 向 GMail 发送邮件时,我也遇到了同样的问题。

    【讨论】:

    • 很抱歉我没有:(
    【解决方案4】:

    有两件事你需要确保

    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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-07-26
      • 2011-01-10
      • 2021-12-13
      • 1970-01-01
      • 2018-08-26
      • 2014-02-07
      • 2020-04-01
      相关资源
      最近更新 更多