title: Node JS - nodemailer & imap-simple - Identify threads
body: <p>(I feel sorry for my poor english, but i'll do my best !) :)</p>
  
  <p>I'm trying to setup a local mailbox for a personnal project, and i'm trying to use imap-simple and nodemailer to do that.</p>
  
  <p>I want to be able to identify a thread, when i send an e-mail.</p>
  
  <p>Here's what i exactly want to do :</p>
  
  <p>In my application, i'll be able to send an e-mail to a specific person (let's admit foo@bar.com)</p>
  
  <p>When the mail is sent, a callback function will store mail content and subject in DB (for example, in a CRM app, i will store the sent mail which will be related to a specific record in my database).</p>
  
  <p>The complex part is just after that :</p>
  
  <p>When the person replies to this e-mail, i want using IMAP to identify that this person is answering to my previous mail, and then store it in DB too, also linked to the same record i used on my first e-mail.</p>
  
  <p>I actually have this in a sandbox folder (For IMAP) :</p>
  
  <pre><code>var imaps = require('imap-simple');
  var nodemailer = require('nodemailer');
  
  var config = {
      imap: {
          user: 'catchall@xxxxxxxx.fr',
          password: 'xxxxxxxx',
          host: 'imap.gmail.com',
          port: 993,
          tls: true,
          authTimeout: 3000
      }
  };
  
  imaps.connect(config).then(function (connection) {
  
      return connection.openBox('INBOX').then(function () {
          var searchCriteria = [
              'UNSEEN'
          ];
  
          var fetchOptions = {
              bodies: ['HEADER', 'TEXT'],
              markSeen: false,
          };
          console.log('Passing there');
          return connection.search(searchCriteria, fetchOptions).then(function (results) {
              var subjects = results.map(function (res) {
                  return res.parts.filter(function (part) {
                      return part.which === 'HEADER';
                  })[0].body.subject[0];
              });
              console.log('BASE');
              console.log(results[0]);
              console.log('FIRST');
              console.log(results[0].parts[0]);
              console.log('SECOND');
              console.log(results[0].parts[1]);
          });
      });
  });
  </code></pre>
  
  <p>And here is the SMTP part :</p>
  
  <pre><code>var transporter = nodemailer.createTransport({
      service: 'gmail',
      auth: {
             user: 'catchall@xxxxxxxx.fr',
             pass: 'xxxxxxxx'
         }
  });
  
  const mailOptions = {
      from: 'catchall@xxxxxxxx.fr', // sender address
      to: 'xxxxxxxx@gmail.com', // list of receivers
      subject: 'Subject of your email', // Subject line
      html: '&lt;p&gt;Here is my test !&lt;/p&gt;',// plain text body
      headers: {
          'collection': 'Pipelines',
          'doc-ID': 'mydocid'
      }
  };
  
  transporter.verify(function(error, success) {
      if (error) {
        console.log(error);
      } else {
        console.log("Server is ready to take our messages");
      }
  });
  
  transporter.sendMail(mailOptions, function (err, info) {
      if(err)
        console.log(err)
      else
        console.log(info);
  });
  </code></pre>
  
  <p>Here is a screen of what i want to be able to :</p>
  
  <p><a href="https://i.stack.imgur.com/8wQ8m.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/8wQ8m.png" alt="Example"></a></p>
  
  <p>On <strong>IMAP</strong>, i'm getting exactly the information i want in console : </p>
  
  <pre><code>BASE
  { attributes:
     { date: 2019-12-05T16:53:07.000Z,
       flags: [],
       uid: 94,
       modseq: '27800',
       'x-gm-labels': [ '\\Important' ],
       'x-gm-msgid': '1652099423356172171',
       'x-gm-thrid': '1652099362185438260' },
  </code></pre>
  
  <p><strong>x-gm-thrid</strong> allow me to identify a thread. Perhaps, i can't find this information in the nodemailer callback function :</p>
  
  <pre><code>Server is ready to take our messages
  { accepted: [ 'xxxxxxxxxxxxxxxxx@gmail.com' ],
    rejected: [],
    envelopeTime: 400,
    messageTime: 819,
    messageSize: 346,
    response: '250 2.0.0 OK  1575566223 m3sm12955793wrs.53 - gsmtp',
    envelope:
     { from: 'catchall@xxxxxxxxxx.fr',
       to: [ 'xxxxxxxxxxxx@gmail.com' ] },
    messageId: '&lt;f93b3970-e17a-84b5-d0d1-ebb4fd6efe46@xxxxxxxxxx.fr&gt;' }
  </code></pre>
  
  <p>Does anyone have an idea of how i could proceed ? </p>
  
  <p><strong>Thanks a lot btw !</strong> </p>
  
  <p>Happy coding :)</p>