简体   繁体   中英

how to send email with nodejs from heroku

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'})
                    }
                }
              });

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

在此处输入图像描述 This will help you a lot taking care of the process of verification under the HOOD:)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM