6

I am trying to send mail in node.js using Nodemailer but it shows some error like { [Error: self signed certificate in certificate chain] code: 'ECONNECTION', command: 'CONN' }

My node.js code is

var express    =    require('express');
var app        =    express();
var nodemailer = require('nodemailer');

var transporter = nodemailer.createTransport('smtps://something%40gmail.com:password@smtp.gmail.com');

var mailOptions = {
  to: 'stevecameron2016@gmail.com',
  subject: 'Hello ?', 
  text: 'Hello world ??', 
  html: '<b>Hello world ??</b>' 
};

transporter.sendMail(mailOptions, function(error, info){
  if(error){
     return console.log(error);
  }
  console.log('Message sent: ' + info.response);
});

var server     =    app.listen(8900,function(){
  console.log("We have started our server on port 8900");
});
Kevin
  • 653
  • 2
  • 13
  • 34
  • 4
    Are you running this code from your computer? If this is so, then most probably your antivirus acts as a MITM and breaks the Gmail certificate. In normal circumstances you should never get "self signed certificate" error when connecting to Gmail. Try to turn off your antivirus or run the code in some other machine. – Andris Jul 18 '16 at 20:30
  • 2
    disable anti virus . it worked for me. – siddmuk2005 Sep 19 '17 at 18:43
  • whre can i get the username and pasword ? – Usman Iqbal Oct 18 '17 at 09:47

5 Answers5

20

try https://github.com/nodemailer/nodemailer/issues/406

add tls: { rejectUnauthorized: false } to your transporter constructor options

p.s It's not a good idea to post your mail server address, if it's a real one

Narcotics
  • 313
  • 1
  • 8
  • `var transporter = nodemailer.createTransport('smtps://something%40gmail.com:password@smtp.gmail.com', 'tls:{ rejectUnauthorized: false}');` I have added like this. But its not working. – Kevin Jul 18 '16 at 09:05
  • Have you ever read the document? If you want to set extra options, you gotta `createTransport(options)`, `options` is an Object and `tls: { rejectUnauthorized: false }` is one of the properties of this object. – Narcotics Jul 19 '16 at 02:48
  • 1
    @Narcotics Thanks a lot. You saved my day. – Nitesh Chauhan Sep 04 '17 at 11:21
3

To allow to send an email via “less secure apps”, go to the link and choose “Turn on”.

(More info about less secure apps)

var nodemailer = require('nodemailer');
var smtpTransport = require('nodemailer-smtp-transport');

var mailAccountUser = '<YOUR_ACCOUNT_USER>'
var mailAccountPassword = '<YOUR_ACCOUNT_PASS>'

var fromEmailAddress = '<FROM_EMAIL>'
var toEmailAddress = 'TO_EMAIL'

var transport = nodemailer.createTransport(smtpTransport({
    service: 'gmail',
    auth: {
        user: mailAccountUser,
        pass: mailAccountPassword
    }
}))

var mail = {
    from: fromEmailAddress,
    to: toEmailAddress,
    subject: "hello world!",
    text: "Hello!",
    html: "<b>Hello!</b><p><a href=\"http://www.yahoo.com\">Click Here</a></p>"
}

transport.sendMail(mail, function(error, response){
    if(error){
        console.log(error);
    }else{
        console.log("Message sent: " + response.message);
    }

    transport.close();
});
Yevhen Dubinin
  • 4,657
  • 3
  • 34
  • 57
  • I have turned on the less secure apps and i used above code but it showing error like `{ [Error: self signed certificate in certificate chain] code: 'ECONNECTION', command: 'CONN' }`. – Kevin Jul 18 '16 at 09:22
  • 5
    add tls: { rejectUnauthorized: false } to your transporter constructor options – Spl2nky Mar 06 '17 at 19:14
  • Gmail is a waste of time, it is incurable, you can fix it locally but it will break on the server – Toolkit May 20 '17 at 04:57
  • Why you are importing nodemailer-smtp-transport at line # 2? You are not using it anywhere in the code. – Zullu Sep 15 '19 at 07:58
1

what @user3985565 said is correct. However , if you are using gmail you also need to change some settings in your gmail account. More specifically you need to "allow less secure apps" in you gmail account. To do that just follow these steps:

  1. test nodemailer as it is
  2. node will throw an error and gmail will send you a security alert email informing you that "an unsafe app tried to access your account"
  3. in this email you need to click on "check activity" and then, in the folowing screen, you must unswer "yes"
  4. Your next clicks in the following screens are "more information" and then "less secure apps".
  5. finally you will see a toggle switch and you must turn it on.
JSmith
  • 4,519
  • 4
  • 29
  • 45
0

i was in this trouble too, what i did is next code line:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

just before creating the smpttransport

for example, in your code just put this:

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; var transporter = nodemailer.createTransport('smtps://something%40gmail.com:password@smtp.gmail.com');

0

it worked for me.

process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";

Tính Ngô Quang
  • 4,400
  • 1
  • 33
  • 33