电子邮件验证nodemailer适用于localhost,但不适用于服务器

时间:2015-05-16 09:11:08

标签: javascript node.js express

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

摘录

OS.File.read

用户将输入用于验证的电子邮件ID的页面。

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

2 个答案:

答案 0 :(得分:0)

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

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

希望它有效:)

答案 1 :(得分:0)

您可以使用 APP PASSWORD,无需在您的安全级别启用 less secure app

可以为您的自定义应用程序创建应用程序密码,例如:My Custom App

在密码字段中使用新生成的密码作为您的 Gmail 密码。

支持链接:https://support.google.com/accounts/answer/185833?hl=en