Пример #1
0
 public function fetchFromMailServer(Entity $emailAccount)
 {
     if ($emailAccount->get('status') != 'Active') {
         throw new Error();
     }
     $importer = new \Fox\Core\Mail\Importer($this->getEntityManager(), $this->getFileManager(), $this->getConfig());
     $maxSize = $this->getConfig()->get('emailMessageMaxSize');
     $teamId = $emailAccount->get('teamId');
     $userId = $this->getUser()->id;
     if ($emailAccount->get('assignToUserId')) {
         $userId = $emailAccount->get('assignToUserId');
     }
     $teamIds = array();
     if (!empty($teamId)) {
         $teamIds[] = $teamId;
     }
     $filterCollection = $this->getEntityManager()->getRepository('EmailFilter')->where(['OR' => [['parentType' => $emailAccount->getEntityType(), 'parentId' => $emailAccount->id], ['parentId' => null]]])->find();
     $fetchData = json_decode($emailAccount->get('fetchData'), true);
     if (empty($fetchData)) {
         $fetchData = array();
     }
     if (!array_key_exists('lastUID', $fetchData)) {
         $fetchData['lastUID'] = array();
     }
     if (!array_key_exists('lastUID', $fetchData)) {
         $fetchData['lastDate'] = array();
     }
     $imapParams = array('host' => $emailAccount->get('host'), 'port' => $emailAccount->get('port'), 'user' => $emailAccount->get('username'), 'password' => $this->getCrypt()->decrypt($emailAccount->get('password')));
     if ($emailAccount->get('ssl')) {
         $imapParams['ssl'] = 'SSL';
     }
     $storage = new \Fox\Core\Mail\Mail\Storage\Imap($imapParams);
     $monitoredFolders = $emailAccount->get('monitoredFolders');
     if (empty($monitoredFolders)) {
         $monitoredFolders = 'INBOX';
     }
     $portionLimit = $this->getConfig()->get('inboundEmailMaxPortionSize', self::PORTION_LIMIT);
     $monitoredFoldersArr = explode(',', $monitoredFolders);
     foreach ($monitoredFoldersArr as $folder) {
         $folder = mb_convert_encoding(trim($folder), 'UTF7-IMAP', 'UTF-8');
         try {
             $storage->selectFolder($folder);
         } catch (\Exception $e) {
             $GLOBALS['log']->error('InboundEmail ' . $emailAccount->id . ' (Select Folder) [' . $e->getCode() . '] ' . $e->getMessage());
             continue;
         }
         $lastUID = 0;
         $lastDate = 0;
         if (!empty($fetchData['lastUID'][$folder])) {
             $lastUID = $fetchData['lastUID'][$folder];
         }
         if (!empty($fetchData['lastDate'][$folder])) {
             $lastDate = $fetchData['lastDate'][$folder];
         }
         $ids = $storage->getIdsFromUID($lastUID);
         if (count($ids) == 1 && !empty($lastUID)) {
             if ($storage->getUniqueId($ids[0]) == $lastUID) {
                 continue;
             }
         }
         $k = 0;
         foreach ($ids as $i => $id) {
             $toSkip = false;
             if ($k == count($ids) - 1) {
                 $lastUID = $storage->getUniqueId($id);
             }
             if ($maxSize) {
                 if ($storage->getSize($id) > $maxSize * 1024 * 1024) {
                     $k++;
                     continue;
                 }
             }
             $message = null;
             $email = null;
             try {
                 $message = $storage->getMessage($id);
                 if ($message && isset($message->from)) {
                     $fromString = $message->from;
                     if (preg_match('/MAILER-DAEMON|POSTMASTER/i', $fromString)) {
                         $toSkip = true;
                         try {
                             $this->processBouncedMessage($message);
                         } catch (\Exception $e) {
                             $GLOBALS['log']->error('InboundEmail ' . $emailAccount->id . ' (Process Bounced Message: [' . $e->getCode() . '] ' . $e->getMessage());
                         }
                     }
                 }
                 if (!$toSkip) {
                     try {
                         $email = $importer->importMessage($message, $userId, $teamIds, $filterCollection);
                     } catch (\Exception $e) {
                         $GLOBALS['log']->error('InboundEmail ' . $emailAccount->id . ' (Import Message): [' . $e->getCode() . '] ' . $e->getMessage());
                     }
                 }
             } catch (\Exception $e) {
                 $GLOBALS['log']->error('InboundEmail ' . $emailAccount->id . ' (Get Message): [' . $e->getCode() . '] ' . $e->getMessage());
             }
             if (!empty($email)) {
                 if (!$emailAccount->get('createCase')) {
                     $this->noteAboutEmail($email);
                 }
                 if ($emailAccount->get('createCase')) {
                     $this->createCase($emailAccount, $email);
                 } else {
                     if ($emailAccount->get('reply')) {
                         $user = $this->getEntityManager()->getEntity('User', $userId);
                         $this->autoReply($emailAccount, $email, $user);
                     }
                 }
             }
             if ($k == count($ids) - 1) {
                 if ($message && isset($message->date)) {
                     $dt = new \DateTime($message->date);
                     if ($dt) {
                         $dateSent = $dt->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s');
                         $lastDate = $dateSent;
                     }
                 }
             }
             if ($k == $portionLimit - 1) {
                 $lastUID = $storage->getUniqueId($id);
                 break;
             }
             $k++;
         }
         $fetchData['lastUID'][$folder] = $lastUID;
         $fetchData['lastDate'][$folder] = $lastDate;
         $emailAccount->set('fetchData', json_encode($fetchData));
         $this->getEntityManager()->saveEntity($emailAccount, array('silent' => true));
     }
     return true;
 }
Пример #2
0
 public function fetchFromMailServer(Entity $emailAccount)
 {
     if ($emailAccount->get('status') != 'Active') {
         throw new Error();
     }
     $importer = new \Fox\Core\Mail\Importer($this->getEntityManager(), $this->getFileManager(), $this->getConfig());
     $maxSize = $this->getConfig()->get('emailMessageMaxSize');
     $user = $this->getEntityManager()->getEntity('User', $emailAccount->get('assignedUserId'));
     if (!$user) {
         throw new Error();
     }
     $userId = $user->id;
     $teamId = $user->get('defaultTeamId');
     $teamIds = array();
     if (!empty($teamId)) {
         $teamIds[] = $teamId;
     }
     $filterCollection = $this->getEntityManager()->getRepository('EmailFilter')->where(['OR' => [['parentType' => $emailAccount->getEntityType(), 'parentId' => $emailAccount->id], ['parentId' => null]]])->find();
     $fetchData = json_decode($emailAccount->get('fetchData'), true);
     if (empty($fetchData)) {
         $fetchData = array();
     }
     if (!array_key_exists('lastUID', $fetchData)) {
         $fetchData['lastUID'] = array();
     }
     if (!array_key_exists('lastUID', $fetchData)) {
         $fetchData['lastDate'] = array();
     }
     $storage = $this->getStorage($emailAccount);
     $monitoredFolders = $emailAccount->get('monitoredFolders');
     if (empty($monitoredFolders)) {
         throw new Error();
     }
     $portionLimit = $this->getConfig()->get('personalEmailMaxPortionSize', self::PORTION_LIMIT);
     $monitoredFoldersArr = explode(',', $monitoredFolders);
     foreach ($monitoredFoldersArr as $folder) {
         $folder = mb_convert_encoding(trim($folder), 'UTF7-IMAP', 'UTF-8');
         try {
             $storage->selectFolder($folder);
         } catch (\Exception $e) {
             $GLOBALS['log']->error('EmailAccount ' . $emailAccount->id . ' (Select Folder) [' . $e->getCode() . '] ' . $e->getMessage());
             continue;
         }
         $lastUID = 0;
         $lastDate = 0;
         if (!empty($fetchData['lastUID'][$folder])) {
             $lastUID = $fetchData['lastUID'][$folder];
         }
         if (!empty($fetchData['lastDate'][$folder])) {
             $lastDate = $fetchData['lastDate'][$folder];
         }
         if (!empty($lastUID)) {
             $ids = $storage->getIdsFromUID($lastUID);
         } else {
             $dt = new \DateTime($emailAccount->get('fetchSince'));
             if ($dt) {
                 $ids = $storage->getIdsFromDate($dt->format('d-M-Y'));
             } else {
                 return false;
             }
         }
         if (count($ids) == 1 && !empty($lastUID)) {
             if ($storage->getUniqueId($ids[0]) == $lastUID) {
                 continue;
             }
         }
         $k = 0;
         foreach ($ids as $i => $id) {
             if ($k == count($ids) - 1) {
                 $lastUID = $storage->getUniqueId($id);
             }
             if ($maxSize) {
                 if ($storage->getSize($id) > $maxSize * 1024 * 1024) {
                     $k++;
                     continue;
                 }
             }
             $message = null;
             $email = null;
             try {
                 $message = $storage->getMessage($id);
                 if ($message && $emailAccount->get('keepFetchedEmailsUnread')) {
                     $flags = $message->getFlags();
                 }
                 try {
                     $email = $importer->importMessage($message, $userId, $teamIds, $filterCollection);
                 } catch (\Exception $e) {
                     $GLOBALS['log']->error('EmailAccount ' . $emailAccount->id . ' (Import Message): [' . $e->getCode() . '] ' . $e->getMessage());
                 }
                 if ($emailAccount->get('keepFetchedEmailsUnread')) {
                     if (is_array($flags) && empty($flags[Storage::FLAG_SEEN])) {
                         $storage->setFlags($id, $flags);
                     }
                 }
             } catch (\Exception $e) {
                 $GLOBALS['log']->error('EmailAccount ' . $emailAccount->id . ' (Get Message): [' . $e->getCode() . '] ' . $e->getMessage());
             }
             if (!empty($email)) {
                 $this->noteAboutEmail($email);
             }
             if ($k == count($ids) - 1) {
                 $lastUID = $storage->getUniqueId($id);
                 if ($message && isset($message->date)) {
                     $dt = new \DateTime($message->date);
                     if ($dt) {
                         $dateSent = $dt->setTimezone(new \DateTimeZone('UTC'))->format('Y-m-d H:i:s');
                         $lastDate = $dateSent;
                     }
                 }
             }
             if ($k == $portionLimit - 1) {
                 $lastUID = $storage->getUniqueId($id);
                 break;
             }
             $k++;
         }
         $fetchData['lastUID'][$folder] = $lastUID;
         $fetchData['lastDate'][$folder] = $lastDate;
         $emailAccount->set('fetchData', json_encode($fetchData));
         $this->getEntityManager()->saveEntity($emailAccount, array('silent' => true));
     }
     return true;
 }