0

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

                }
        })
    })
}); 

1 Answer 1

2

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: '[email protected]',
    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: '[email protected]',
                to: '[email protected]',
                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.

Sign up to request clarification or add additional context in comments.

2 Comments

If you are using an email service other than gmail, you can manually create transport by providing host and port
For storing password securely for nodemailer please refer this question stackoverflow.com/questions/26475136/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.