guagay_wk
guagay_wk

Reputation: 28050

TLS error sending out mail with nodemailer

I am using node.js nodemailer v2.5.0 to send out email using gmail. I used this website https://community.nodemailer.com/ as reference.

Here is my code.

var nodemailer = require('nodemailer');

// create reusable transporter object using the default SMTP transport
var transporter = nodemailer.createTransport('smtp://user%40gmail.com:[email protected]');

// setup e-mail data with unicode symbols
var mailOptions = {
    from: '"Fred Foo ?" <[email protected]>', // sender address
    to: '[email protected], [email protected]', // list of receivers
    subject: 'Hello ✔', // Subject line
    text: 'Hello world ?', // plaintext body
    html: '<b>Hello world ?</b>' // html body
};

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

I received the following error after running the above code;

{ Error: self signed certificate in certificate chain
    at Error (native)
    at TLSSocket.<anonymous> (_tls_wrap.js:1079:38)
    at emitNone (events.js:86:13)
    at TLSSocket.emit (events.js:185:7)
    at TLSSocket._finishInit (_tls_wrap.js:603:8)
    at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:433:38) code: 'SELF_SIGNED_CERT_IN_CHAIN' }

Upvotes: 1

Views: 5668

Answers (1)

guagay_wk
guagay_wk

Reputation: 28050

When using nodemailer with gmail, you need to enable "less secure" feature on your google account. Your code uses plain-text password to login, so "less secure" feature needs to be enabled.

See https://community.nodemailer.com/using-gmail/ for more information.

Personally, I would prefer Oauth2 authentication to access gmail services. Since you are using node.js, this node module should be useful.

https://www.npmjs.com/package/gmail-node

Upvotes: 1

Related Questions