[英]email verification nodemailer works on localhost but not on server
I have written code for email verification on user registration using nodemailer in nodejs but it is working fine only when running on localhost, once I put this on server it is not working.Instead of localhost:8080 I used the server IP address then also same problem.我已经在 nodejs 中使用 nodemailer 编写了用于用户注册电子邮件验证的代码,但它仅在本地主机上运行时才能正常工作,一旦我将其放在服务器上,它就无法正常工作。 而不是 localhost:8080 我使用了服务器 IP 地址,然后也一样问题。
The snippet is片段是
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");
}
});
The page where user will enter the email id for verification.用户将在其中输入电子邮件 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>
I had exactly the same issue before on my server.我之前在我的服务器上遇到过完全相同的问题。 Here are steps to fix it:
以下是修复它的步骤:
For me the error log was saying对我来说,错误日志说
534-5.7.14 <https-//accounts.google.com/ContinueSignIn ..... Please log in via your web browser and then try again...
. 534-5.7.14 <https-//accounts.google.com/ContinueSignIn ..... Please log in via your web browser and then try again...
I believed it told me to login to GMail from my server's browser!我相信它告诉我从我服务器的浏览器登录到 GMail! So I tried to use Lynx - a browser for terminal to login to GMail (You can also login from: https://accounts.google.com/login ).
所以我尝试使用Lynx - 终端浏览器登录 GMail (您也可以从: https : //accounts.google.com/login登录)。 Then... Woo Hoo!, it works!
然后...... Woo Hoo!,它起作用了!
Good luck!祝你好运!
You have to enable below settings of gmail account from which you want to send mails您必须启用要从中发送邮件的 Gmail 帐户的以下设置
https://www.google.com/settings/security/lesssecureapps https://www.google.com/settings/security/lesssecureapps
Hope it works :)希望它有效:)
You can use APP PASSWORD , not required to enable less secure app
at your security level.您可以使用APP PASSWORD ,不需要在您的安全级别启用
less secure app
性less secure app
。
App password can be created for your custom applications eg: My Custom App
可以为您的自定义应用程序创建应用程序密码,例如:
My Custom App
Use the newly generated password as your Gmail Password in your password field.在您的密码字段中使用新生成的密码作为您的 Gmail 密码。
Support Link: https://support.google.com/accounts/answer/185833?hl=en支持链接: https : //support.google.com/accounts/answer/185833?hl=en
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.