电子邮件验证 nodemailer 适用于本地主机,但不适用于服务器

Dia*_*kar 5 javascript node.js express

我已经在 nodejs 中使用 nodemailer 编写了用于用户注册电子邮件验证的代码,但它只有在本地主机上运行时才能正常工作,一旦我将它放在服务器上,它就无法正常工作。 而不是 localhost:8080 我使用了服务器 IP 地址,然后也一样问题。

片段是

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

app.get('/send', function (req, res) {
    rand = Math.floor((Math.random() * 100) + 54);
    hash = bcrypt.hashSync(rand, 8);
    console.log("hash key " + hash);
    host = req.get('host');
    console.log("Host -" + host);
    link = "http://" + req.get('host') + "/verify?id=" + hash;
    mailOptions = {
        to: req.query.to,
        subject: "Verify your Email account",
        html: "Hello,<br> Please Click on the link to verify your email.<br><a href=" + link + ">Click here to verify</a>"
    }
    global.recip = mailOptions.to;
    console.log("recipt =" + recip)
    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");
        }
        smtpTransport.close();
    });
});

app.get('/verify', function (req, res) {
    console.log(req.protocol + ":/" + req.get('host'));
    console.log("rand " + hash);
    console.log("id -" + req.query.id);
    if ((req.protocol + "://" + req.get('host')) == ("http://" + host)) {
        console.log("Domain is matched. Information is from Authentic email");
        if (req.query.id == hash) {
            console.log("email is verified");
            res.end("<h1 style=margin-top:200px;margin-left:200px;>Email            " + mailOptions.to + " is been Successfully verified <br><a href='/password'>Reset Password</a>");
        } else {
            console.log("email is not verified");
            res.end("<h1 style=margin-top:200px;margin-left:200px;>Please enter    your email again </h1>");
        }
    } else {
        res.end("<h1>Request is from unknown source");
    }
});
Run Code Online (Sandbox Code Playgroud)

用户将在其中输入电子邮件 ID 进行验证的页面。

$(document).ready(function () {
    var from, to, subject, text;
    $("#send_email").click(function () {
        to = $("#to").val();
        if (to == '') {
            alert("Please enter a valid email");
        } else {
            $("#message").text("Sending E-mail...Please wait");
        }
        $.get("http://23.253.245.25/send", {
            to: to
        }, function (data) {
            if (data == "sent") {
                $("#message").empty().html("<h4><br> Email is been sent at " + to + " .          Please check inbox !</h4>");
            }
        });
    });
Run Code Online (Sandbox Code Playgroud)
var smtpTransport = nodemailer.createTransport("SMTP", {
    service: "Gmail",
    auth: {
        user: "abc@gmail.com",
        pass: "12345"
    }
});
var rand, mailOptions, host, link;

app.get('/send', function (req, res) {
    rand = Math.floor((Math.random() * 100) + 54);
    hash = bcrypt.hashSync(rand, 8);
    console.log("hash key " + hash);
    host = req.get('host');
    console.log("Host -" + host);
    link = "http://" + req.get('host') + "/verify?id=" + hash;
    mailOptions = {
        to: req.query.to,
        subject: "Verify your Email account",
        html: "Hello,<br> Please Click on the link to verify your email.<br><a href=" + link + ">Click here to verify</a>"
    }
    global.recip = mailOptions.to;
    console.log("recipt =" + recip)
    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");
        }
        smtpTransport.close();
    });
});

app.get('/verify', function (req, res) {
    console.log(req.protocol + ":/" + req.get('host'));
    console.log("rand " + hash);
    console.log("id -" + req.query.id);
    if ((req.protocol + "://" + req.get('host')) == ("http://" + host)) {
        console.log("Domain is matched. Information is from Authentic email");
        if (req.query.id == hash) {
            console.log("email is verified");
            res.end("<h1 style=margin-top:200px;margin-left:200px;>Email            " + mailOptions.to + " is been Successfully verified <br><a href='/password'>Reset Password</a>");
        } else {
            console.log("email is not verified");
            res.end("<h1 style=margin-top:200px;margin-left:200px;>Please enter    your email again </h1>");
        }
    } else {
        res.end("<h1>Request is from unknown source");
    }
});
Run Code Online (Sandbox Code Playgroud)

小智 0

您必须启用要从中发送邮件的 Gmail 帐户的以下设置

https://www.google.com/settings/security/lesssecureapps

希望它有效:)