Naveen DINUSHKA Naveen DINUSHKA -3 years ago 128
Node.js Question

NodeJs Error When Submitting Email using Nodemailer

I have followed these steps to setup nodemailer
1) Allow access to less secure apps in gmail
2) Written the following in app.js

app.post('/reachus/send',function(req,res){
var transporter=nodemailer.createTransport({
service:'Gmail',
auth: {
user:'my@gmail.com',
pass:'***'
}
});

var mailOptions={
from:'Naveen DK <my@gmail.com>',
to:'my@gmail.com',
subject:'Email Sent from your website',
text:'You have a submission with the following details.. Name: '+req.body.name +'Email: '+req.body.email+' Message: '+ req.body.message,
html: '<p>You have a submission with the following details..</p><ul><li> Name :'+req.body.name + ' </li><li>Email: '+req.body.email +'</li><li>Message ' + req.body.message +'</li></ul>'
};

transporter.sendMail(mailOptions,function(error,info){
if(error){
console.log(error);
res.redirect('/');
} else{
console.log('Message Sent ' + info.response);
res.redirect('/');
}
});
});


3) Once I Click on Submit Email I get the following error

{ Error: read ECONNRESET
at exports._errnoException (util.js:1026:11)
at TLSWrap.onread (net.js:569:26)
code: 'ECONNECTION',
errno: 'ECONNRESET',
syscall: 'read',
command: 'CONN
}


Please find the below 2 vids for more details

1 https://www.dropbox.com/s/nc1zvivlfpabj6h/HowMyCodeLooksLike.wmv?dl=0

2 https://www.dropbox.com/s/tfsqu6ir90s682h/ErrorOnceSubmissionDone.wmv?dl=0

Thanks in advance

Naveen

Answer Source

use below code for sending email from nodemailer, inside function pass ur parameter and you will get ur result.

var AppConfig = {
'sendEmailID': 'useremail',
'sendEmailFromName': 'senderemail',
'sendEmailPassword': 'password'

}

function SendEmail(toEmail, Subject, html) {
// create reusable transporter object using the default SMTP transport 
var transporter = nodemailer.createTransport({
    host: 'smtp.gmail.com',
    port: '587',
    auth: {
        user: "username",
        pass: AppConfig.sendEmailPassword
    },
    secureConnection: 'false',
    tls: {
        ciphers: 'SSLv3'
    }

});

// setup e-mail data with unicode symbols 
var mailOptions = {
    from: AppConfig.sendEmailFromName, // sender address 
    to: toEmail, // list of receivers 
    subject: Subject, // Subject line 
    html: html // html body 
};

// send mail with defined transport object 
transporter.sendMail(mailOptions, function (error, info) {
    if (error) {
        return console.log("ERROR----" + error);
    }
    console.log('Message sent: ' + info.response);
});

}

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download