0

I'm currently building a php scripts which fetches the email from a server using imap functions and stores the details in the database.

My problem is I dont know how to identify new mails from old mails that already exists. and how to get reply mails sent to the mail

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
Astro
  • 13
  • 1
  • 1
  • 4
  • possible duplicate of [Getting unique email id with IMAP in PHP](http://stackoverflow.com/questions/14894624/getting-unique-email-id-with-imap-in-php) – Matiss Jul 16 '15 at 08:00

1 Answers1

0

use the UID message to determine the last message, you have to store the last UID in the table

  $uidsArray = imap_sort($imapConnection, SORTARRIVAL, 1, SE_UID);
  if ($uidsArray) {
      // read UID last message, XEmailUID - table(mailbox, lastuid mailbox)
      $lastUIDObject = new XEmailUID();
      $lastUIDObject->setImap($mailbox->getId().'/'.$mailboxRef);
      if (!$lastUIDObject->select()) {
           $lastUIDObject->insert();
      }

      $uidMax = 0;

      foreach ($uidsArray as $uid) {
        if ($uid < $lastUIDObject->getUid()) {
           continue;
        }

        if ($uid >= $uidMax) {
            $uidMax = $uid;
        }

       // your function
       $this->_readIMAPMessage(
          $imapConnection,
          $uid,
          $mailboxRef
       );
      }

      if ($uidMax > 0) {
         $lastUIDObject->setUid($uidMax);
         $lastUIDObject->update();
      }
   }
D G
  • 13
  • 7