title: nodemailer google smtp connection closed error
body: <p>I am trying to send email using nodemailer and nodemailer-smtp-transport in express js. i am creating a custom module to send mails to the user. When ever i am executing the script it is throwing 2 specific errors.</p>
  
  <p><strong>Error 1</strong></p>
  
  <pre><code>D:\Projects\project_name\modules\mailer.js:45
                          throw error;
                          ^
  
  Error: Connection closed
  
  at SMTPConnection.&lt;anonymous&gt; (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\lib\smtp-transport.js:113:29)
  at SMTPConnection.g (events.js:260:16)
  at emitNone (events.js:67:13)
  at SMTPConnection.emit (events.js:166:7)
  at SMTPConnection._destroy (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\lib\smtp-connection.js:558:10)
  at SMTPConnection._onEnd (D:\Projects\WISSENX\node_modules\nodemailer-smtp-transport\node_modules\smtp-connection\lib\smtp-connection.js:538:10)
  at Socket.g (events.js:260:16)
  at emitNone (events.js:72:20)
  at Socket.emit (events.js:166:7)
  at endReadableNT (_stream_readable.js:921:12)
  at nextTickCallbackWith2Args (node.js:442:9)
  at process._tickCallback (node.js:356:17)
  </code></pre>
  
  <p><strong>Error 2</strong></p>
  
  <pre><code>{
  [Error: getaddrinfo ENOTFOUND smtp.gmail.com smtp.gmail.com:465]
  
  code: 'ECONNECTION',
  
  errno: 'ENOTFOUND',
  
  syscall: 'getaddrinfo',
  
  hostname: 'smtp.gmail.com',
  
  host: 'smtp.gmail.com',
  
  port: '465',
  
  command: 'CONN' }
  </code></pre>
  
  <p>My Complete code is</p>
  
  <pre><code>var nodemailer = require('nodemailer');
  var smtpTransport = require("nodemailer-smtp-transport")
  
  var constants = require('./../modules/constants');
  
  
  /*var smtpTransport = nodemailer.createTransport(constants.MAIL_HOST,{
   service : "Gmail",
       auth: {
          user: constants.MAIL_FROM_AUTH,
          pass: constants.MAIL_PASSWORD
      }
  });*/
  
  
  var smtpTransport = nodemailer.createTransport(smtpTransport({
      host : constants.MAIL_HOST,
      secureConnection : constants.MAIL_SECURE,
      port: constants.MAIL_PORT,
      auth : {
          user : constants.MAIL_FROM_AUTH,
          pass : constants.MAIL_PASSWORD
      },
      ignoreTLS: true
  }));
  
  
  
  var sendMail = function(req, res, template, to, subject, callback) {
      var mailOptions = {
          from: constants.MAIL_FROM, // sender address
          to: to, // list of receivers
          subject: subject, // Subject line
          text: "random text", // plaintext body
          html: template // html body
      }
  console.log(mailOptions);
  console.log(smtpTransport);
  
      // send mail with defined transport object
      smtpTransport.sendMail(mailOptions, function(error, response){
  console.log("Message sent: " + response);
  console.log(error);
          if(error) {
              throw error;
              console.log(error);
          }
          else {
              console.log("Message sent: " + response.message);
          }
  
          smtpTransport.close(); // shut down the connection pool, no more messages
  
      });
  }
  exports.sendMail = sendMail;
  </code></pre>