title: Can Nodemailer be a standalone SMTP server?
body: <p>i was reading about that Nodemailer is <a href="http://adilapapaya.com/docs/nodemailer/" rel="nofollow noreferrer">using SMTP or sendmail or Amazon SES</a> and i also discovered more <a href="https://stackoverflow.com/a/29287563/1705922">smtp servers</a> so i was assuming that aside of my nodemailer application i have to make one of those SMTP servers running and make my app connect to it but someone told me that Nodemailer can be itself an autonomous (or maybe a standlaone) SMTP server "Nodemailer is autonomous to send mails (it is not necessary to go through a server / SMTP service)".</p>
  
  <p>So i was reading again its documentation and i found that the Nodemailer transport object uses the nodemailer-smtp-transport module as default</p>
  
  <blockquote>
    <p>var transporter = nodemailer.createTransport(smtpTransport(options))
    (1)</p>
  </blockquote>
  
  <p>or (by using smtpTransport as default)</p>
  
  <blockquote>
  <pre><code>var transporter = nodemailer.createTransport(options) (2)
  </code></pre>
  </blockquote>
  
  <p>(which i suppose it means that (1) is equivalent to (2))</p>
  
  <p>So i was thiking that maybe it's the nodemailer-smtp-transport which makes Nodemailer to be a standalone smtp server and then i tried this code for defining the Nodemailer transporter and to make it sending the email with sendMail: (let's call this <strong>snippet1</strong>) </p>
  
  <pre><code>var transporter = nodemailer.createTransport(smtpTransport({
      host: 'localhost',
      port: 465,
      auth: {
          user: 'username',
          pass: 'pass'
      }
  }));
  
  // NB! No need to recreate the transporter object. You can use 
  // the same transporter object for all e-mails 
  
  // setup e-mail data with unicode symbols 
  var mailOptions = {
      from: 'Ahmed Feki ✔ &lt;fekiajob@gmail.com&gt;', // sender address 
      to: 'fekiajob@yahoo.fr, mido.feki@gmail.com', // list of receivers 
      subject: 'Hello ✔', // Subject line 
      text: 'Hello world ✔', // plaintext body 
      html: '&lt;b&gt;Hello world from nodemailer test ✔&lt;/b&gt;' // html body 
  };
  
  
  // send mail with defined transport object 
  transporter.sendMail(mailOptions, function(error, info){
      if(error){
          return console.log('####  sendMail error : ' + error);
      }
      console.log('####  Message sent: ' + info.response);
  
  });
  </code></pre>
  
  <p>and when i was running the app i got this error :</p>
  
  <blockquote>
    sendMail error : Error: connect ECONNREFUSED 127.0.0.1:25
  </blockquote>
  
  <p>So i decided to use the smtp-server in the same application using this code :</p>
  
  <pre><code>var SMTPServer = require('smtp-server').SMTPServer;
  
  var server = new SMTPServer({
      onConnect: function(session, callback){
          if(session.remoteAddress === '127.0.0.1'){
              return callback(new Error('No connections from localhost allowed'));
          }
          return callback(); // Accept the connection
      },
      onError:function(err){
          console.log('Error onError %s', err.message);
      }
  });
  
  /*
          onError wasn't mentioned on the documentation i just wrote it myself 
          as to replace the error on listener :
  
            server.on('error', function(err){
               console.log('Error %s', err.message);
            }); 
          and i don't know if it would work or not.. i actually kept both of them      and just modified the log for i can know from where it would come.
  */
  
  server.listen(465, onConnectCb);
  </code></pre>
  
  <p>and inside the onConnectCb definition (witten in the above last line) i wrote all the <strong>snippet1</strong>'s code and results where to much better as now i got the following log :</p>
  
  <pre><code>[2015-11-02 15:33:11] INFO: Connection from ::ffff:127.0.0.1]
  [2015-11-02 15:33:11] DEBUG: S: 220 Fekis-PC ESMTP
  [2015-11-02 15:33:11] DEBUG: C: EHLO [127.0.0.1]
  [2015-11-02 15:33:11] DEBUG: S: 250-OK: Nice to meet you [::ffff:127.0.0.1]
  [2015-11-02 15:33:11] DEBUG: [mhJFwLqR43aX] C: STARTTLS
  [2015-11-02 15:33:11] DEBUG: [mhJFwLqR43aX] S: 220 Ready to start TLS
  [2015-11-02 15:33:11] INFO: [mhJFwLqR43aX] Connection upgraded to TLS
  ####  sendMail error : Error: self signed certificate
  [2015-11-02 15:33:11] INFO: Connection closed to [::ffff:127.0.0.1]
  </code></pre>
  
  <p>which is too much better even the email wasn't sent as there is still an error on it </p>
  
  <blockquote>
    <p>'self signed certificate'</p>
  </blockquote>
  
  <p>which i suppose it happens because i still didn't set the key and the cert as options in the SMTPServer object (as i still don't know how to get them).</p>
  
  <pre><code>var server = new SMTPServer({
      /*secure: true,
      key: fs.readFileSync('private.key'),
      cert: fs.readFileSync('server.crt')*/
  });
  </code></pre>
  
  <p>So i asked this question to get a confirmation that Nodemailer  can't be a standalone SMTP server.</p>