我已经在 nodejs 中使用 nodemailer 编写了用于用户注册的电子邮件验证代码,但它仅在本地主机上运行时才能正常工作, 一旦我将它放在服务器上,它就无法正常工作。我使用服务器 IP 地址而不是 localhost:8080 然后也是同样的问题。
片段是
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");
}
});
用户将在其中输入电子邮件 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>");
}
});
});
<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>
最佳答案
我之前在我的服务器上遇到过完全相同的问题。以下是修复它的步骤:
- 转到:https://www.google.com/settings/security/lesssecureapps启用从“不太安全的应用程序”登录 - 你做了什么
- 如果当前在列表中被阻止,请确保您的服务器已启用:https://myaccount.google.com/security#activity
对我来说,错误日志是这样说的
534-5.7.14 <https-//accounts.google.com/ContinueSignIn ..... Please log in via your web browser and then try again...
.我相信它告诉我从服务器的浏览器登录 GMail!所以我尝试使用Lynx - 用于终端登录 GMail 的浏览器(您也可以从:https://accounts.google.com/login 登录)。然后……呜呼!成功了!
祝你好运!
关于javascript - 电子邮件验证 nodemailer 适用于本地主机但不适用于服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30273843/