Search code examples
javascriptnode.jsemailconfigurationregistration

How to send an confirmation email after registration nodejs


I want to add the code to send email in this function.
I've also installed 2 libraries: jsonwebtoken and nodemailer. I've seen some code related to this topic but I'm new to javascript and nodejs and i could not seem to make the code work. I could use some help!
Thanks in advance!
This is my code.

app.post('/insertuser',function(_req,res){
    var data =JSON.parse(_req.body.data);
    var username = data.username;
    var age = data.age;
    var password = data.password;
    var fname = data.fname;
    var lname = data.lname;
    var address = data.address;
    var city = data.city;
    var email = data.email;
    var sq = data.sq;
    var answer = data.answer;
    var pnumber = data.pnumber;
    var dataentered = data.dataentered;

    mysqlConnection.connect(function(){
        var query = "Insert into Customer (Username,Age,Password,First_Name,Last_Name,Email,Address,City,Phone_No,SQ,Answer,Date_Entered) values('"+username+"','"+age+"','"+sha1(password)+"','"+fname+"','"+lname+"','"+email+"','"+address+"','"+city+"','"+pnumber+"','"+sq+"','"+answer+"','"+dataentered+"')";
        mysqlConnection.query (query,function(err,results,_fields){
            if(err)
                {
                    console.log(err);
                    res.send('Please try again!');
                }
            else{
                    if(results.affectedRows>0)
                    {
                        res.send('Thanks for registering! Please confirm your email! We have sent a link!'); 
                        //the code for affirmation

                    }
                    else{
                        res.send('Please try again!');
                    }

                }
        })
    })
}); 

Solution

  • You can use nodemailer library for sending emails. I will explain how to send emails from a gmail account. First you need to enable Less Secure App Access in your gmail account security section.enter image description here

    After that, create a transporter using nodemailer:

    const nodemailer = require('nodemailer');
    
    var transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
        user: 'myemail@gmail.com',
        pass: 'password'
      }
    });
    

    Then in your code, use created transporter to send mails.

    mysqlConnection.query (query,function(err,results,_fields){
        if(err) {
            console.log(err);
            res.send('Please try again!');
        } else {
            if(results.affectedRows>0) {
                //the code for affirmation
                var mailOptions = {
                    from: 'myemail@gmail.com',
                    to: 'toemail@gmail.com',
                    subject: 'Sending Email using Node.js',
                    text: 'That was easy!'
                };
    
                transporter.sendMail(mailOptions, function(error, info) {
                    if (error) {
                        console.log(error);
                        //Handle error here
                        res.send('Please try again!');
                    } else {
                        console.log('Email sent: ' + info.response);
                        res.send('Thanks for registering! Please confirm your email! We have sent a link!'); 
                    }
                });
            } else { 
                res.send('Please try again!');
            }
        }
    })
    

    You can refer to nodemailer documentation for more info.