电子邮件validationnodemailer在localhost上工作,但不在服务器上工作

我已经编写了用于在nodejs中使用nodemailer进行用户注册的电子邮件validation代码,但只有在本地主机上运行时才能正常工作,一旦我把它放在服务器上,它就不能正常工作。而不是本地主机: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"); } }); 

用户将input电子邮件ID进行validation的页面。

 $(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>"); } }); }); 
 <div class="form-group"> <input type="text" id="to" placeholder="Enter E-mail which you want to verify" onblur="validateEmail(this);" required /><br> <button id="send_email" style="margin-top:10px;">Send Email</button><br> <span id="message"></span> </div> 

我在我的服务器上有完全相同的问题。 这里是解决它的步骤:

  1. 请访问https://www.google.com/settings/security/lesssecureapps ,启用“安全性较低的应用”loginfunction – 您所做的
  2. 确保您的服务器在列表中被阻止: https : //myaccount.google.com/security#activity
  3. 对我来说,错误日志是在说

    534-5.7.14 <https-//accounts.google.com/ContinueSignIn ..... Please log in via your web browser and then try again...

    我相信它告诉我从我的服务器的浏览器login到GMail! 所以我尝试使用Lynx – 一个terminal浏览器loginGMail (您也可以login: https : //accounts.google.com/login )。 那么…呜呼!它的作品!

祝你好运!

您必须启用以下您想要发送邮件的gmail帐户的设置

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

希望它的作品:)