NodeJs Error When Submitting Email using Nodemailer

六月ゝ 毕业季﹏ 提交于 2019-12-23 01:33:50

问题


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


回答1:


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

}




回答2:


With the response I got from @chetan mekha I changed my code as follows:

var  smtpTransport=nodemailer.createTransport({
   host: 'smtp.gmail.com',
    port: '587',
    auth: {
        user: "**",
        pass: "**"
    },
    secureConnection: 'false',
    tls: {
        ciphers: 'SSLv3'

    }
});

But there another error came up saying : { [Error: self signed certificate in certificate chain] code: 'ECONNECTION', command: 'CONN' } But adding the line rejectUnauthorized: false under ciphers made it work! so final code snippet that worked for me looks like this..

var  smtpTransport=nodemailer.createTransport({
   host: 'smtp.gmail.com',
    port: '587',
    auth: {
        user: "**",
        pass: "**"
    },
    secureConnection: 'false',
    tls: {
        ciphers: 'SSLv3',
        rejectUnauthorized: false

    }
});


来源:https://stackoverflow.com/questions/45707235/nodejs-error-when-submitting-email-using-nodemailer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!