Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

The snippet is

var smtpTransport = nodemailer.createTransport("SMTP", {
    service: "Gmail",
    auth: {
        user: "[email protected]",
        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.

$(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>
like image 375
Dia Sarkar Avatar asked Nov 01 '22 04:11

Dia Sarkar


1 Answers

I had exactly the same issue before on my server. Here are steps to fix it:

  1. Go to: https://www.google.com/settings/security/lesssecureapps to enable logging in from "Less secure apps" - What you've done
  2. Ensure that your server was enabled if it's currently being blocked in the list: https://myaccount.google.com/security#activity
  3. 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....

    I believed it told me to login to GMail from my server's browser! So I tried to use Lynx - a browser for terminal to login to GMail (You can also login from: https://accounts.google.com/login). Then... Woo Hoo!, it works!

Good luck!

like image 102
Ducky Avatar answered Nov 02 '22 23:11

Ducky