0

I have written code to send gmail using nodemailer node.js rest api. this is working fine for my local system server but when the same code i deployed on windows-2000 server, i am unable to send gmail and getting internal server error.

this is the code snippet i am using --

var nodemailer = require('nodemailer');
var stringify = require('json-stringify');

// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port:587,
secure: false,
auth: {
    user: 'user@gmail.com',
    pass: 'user@123'
}
});

// setup e-mail data with unicode symbols
module.exports={

create:function(req,res){
    var params=req.allParams();
    var order_details=JSON.stringify(params);
    //console.log("order-->"+order_details);

    var mailOptions = {
    from: 'user@gmail.com', // sender address
    to: 'user1@gmail.com', // list of receivers
    subject: "g-mail message ", // Subject line
    html: order_details // html body
    }

    console.log("mailOptions"+JSON.stringify(mailOptions));
    smtpTransport.sendMail(mailOptions, function(err, result){
        console.log(result);
        if(err){
            return res.status(500).json("Error");
            //return res.json("System Error")
        }else{
            return res.status(200).json("e-mail sent to customer");
        }
        smtpTransport.close();
    });
}
}

when i am calling the api from local system server this is the response i am getting for conole.log(result)-->

{ accepted: [ 'user1@gmail.com' ],
 rejected: [],
 envelopeTime: 1094,
 messageTime: 1124,
 messageSize: 300,
 response: '250 2.0.0 OK 1539452574 189-v6sm7035924pfe.121 - gsmtp',
   envelope: 
   { from: 'user@gmail.com',
     to: [ 'user1@gmail.com' ] },
     messageId: '<d84e7c1e-7158-5c6a-851d-a783e05be5ce@gmail.com>' }

but when i am trying to call the api from my windows-2000 server, for line console.log(result)--> i am getting "undefined".

So my question is whether i am getting this response from gmail.smtp server or gmail.smtp server is unreachable from the system. I tried to ping gmail.smtp server from my windws-2000 server and there is 0% packet loss. so gmail.smtp is reachable from my server?

I also tried port 465,587,25 and secure- true, false option.

for Error log i am getting this-

{ Error: self signed certificate in certificate chain at TLSSocket.
<anonymous> (_tls_wrap.js:1105:38) at emitNone (events.js:106:13) 
at TLSSocket.emit (events.js:208:7) at TLSSocket._finishInit (_tls_wrap.js:639:8) at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:469:38) code: 'ECONNECTION', command: 'CONN' }}

Thnaks for your's valuable inputs on this issue.

andy
  • 525
  • 3
  • 6
  • 22
  • add a console log for `err` and see if there is any error. – Raj Kumar Oct 13 '18 at 18:19
  • @ Raj Kumar, for the error log I am getting this -- { Error: self signed certificate in certificate chain at TLSSocket. (_tls_wrap.js:1105:38) at emitNone (events.js:106:13) at TLSSocket.emit (events.js:208:7) at TLSSocket._finishInit (_tls_wrap.js:639:8) at TLSWrap.ssl.onhandshakedone (_tls_wrap.js:469:38) code: 'ECONNECTION', command: 'CONN' }} – andy Oct 13 '18 at 19:00
  • please check this thread: https://stackoverflow.com/questions/38431592/sending-mail-in-node-js-using-nodemailer – Raj Kumar Oct 13 '18 at 19:12
  • @Raj Kumar, Thanks this is very usefull. finally i added this line- process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0"; before nodemailer.createTransport() and its working now for me. – andy Oct 13 '18 at 19:19

1 Answers1

0
var nodemailer = require('nodemailer');
var stringify = require('json-stringify');
// add this line before nodemailer.createTransport()
process.env.NODE_TLS_REJECT_UNAUTHORIZED = "0";
// create reusable transport method (opens pool of SMTP connections)
var smtpTransport = nodemailer.createTransport({
service: 'Gmail',
host: 'smtp.gmail.com',
port:587,
secure: false,
  auth: {
  user: 'user@gmail.com',
  pass: 'user@123'
   }
});

now it will work.

andy
  • 525
  • 3
  • 6
  • 22