使用Nodemailer提交电子邮件时NodeJs出错

时间:2017-08-16 07:13:34

标签: node.js nodemailer

我已按照以下步骤设置nodemailer 1)允许访问gmail中安全性较低的应用程序 2)在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)点击“提交电子邮件”后,我收到以下错误

 { 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
 }

请查看以下2个视频了解更多详情

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

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

提前致谢

纳温

2 个答案:

答案 0 :(得分:1)

使用下面的代码从nodemailer发送电子邮件,在函数pass ur参数内,你会得到你的结果。

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

}

答案 1 :(得分:1)

根据我从@chetan mekha得到的回复,我改变了我的代码如下:

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

    }
});

但是还出现了另一个错误:{[错误:证书链中的自签名证书]代码:'ECONNECTION',命令:'CONN'}但是添加了行 rejectUnauthorized:密码下的假使其成功!所以对我有用的最终代码片段看起来像这样..

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

    }
});