1

I have this nodejs app am running and am trying to send an email to newly register users like a verification link. On my local server it works well but when I deployed to heroku it always fails.

my nodejs code

var transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: 465,
    auth: {
      user: 'gmail.com',
      pass: 'password'
    }
  });

var mailOptions = {
                from: 'gmail@gmail.com',
                to: email,
                subject: 'Verification code',
                html: `<h1 style="color:blue,font-weight:bold,text-transform-uppercase"></h1></p>
                <p style="color:black,font-weight:bold,text-align:center, font-size:20px">${pin}</p>
                <span>this verification process helps comfirm that your the real owner of this account, so we can
                help protect you from scams</span>
                <p>click the link <a href="localhost:3000/${v_address}.verification">${v_address}</a> to go to the verification page</p>`
              };
              transporter.sendMail(mailOptions, async function(error, info){
                if (error) {
                  res.json({error:'failed please check details or network connection'});
                }else {
                    const newCrete = await Create.save()
                    if(newCrete){
                        const newVerify = new Verifyuser({
                        email:email,
                        pin:pin,
                        address:v_address
                    })
                    const Verified = await newVerify.save()
                    res.json({success:'success'})
                    }
                }
              });
Dominik
  • 6,078
  • 8
  • 37
  • 61
watch dog
  • 187
  • 2
  • 15
  • Google limits Heroku IPs. Have you checked out https://stackoverflow.com/questions/25693280/nodemailer-with-gmail-service-not-working-on-heroku && https://medium.com/@nickroach_50526/sending-emails-with-node-js-using-smtp-gmail-and-oauth2-316fe9c790a1 – Njuguna Mureithi Dec 06 '20 at 07:48
  • Does this answer your question? [Nodemailer with Gmail service not working on heroku](https://stackoverflow.com/questions/25693280/nodemailer-with-gmail-service-not-working-on-heroku) – Njuguna Mureithi Dec 06 '20 at 07:49
  • "it always fails" -- how does it fail? If you have an error message, please share it. – ggorlen May 19 '21 at 03:01

1 Answers1

0

Kindly use the NPM Package (two-step-auth)

This will take care of the whole verification process, you don't need to worry about the backend work :), This will give you an OTP and the client email an OTP and you can check if they match and you can verify the Email ID,

Kindly check the full procedures with example here

Usage

const {Auth} = require('two-step-auth');

async function login(emailId){
    const res = await Auth(emailId);
    // You can follw the above approach, But we recommend you to follow the one below, as the mails will be treated as important
    const res = await Auth(emailId, "Company Name");
    console.log(res);
    console.log(res.mail);
    console.log(res.OTP);
    console.log(res.success);
}

login("YourEmail@anyDomain.com")

Output

enter image description here This will help you a lot taking care of the process of verification under the HOOD :)

SARAN SURYA
  • 534
  • 5
  • 14