Ejemplo n.º 1
0
 /**
  * send mail
  *
  * array or object of type you set while initialize service
  * @param Object|array $mail
  *
  * @throws \Exception $ex
  *
  * @return Mail
  */
 public function sendMail($mail)
 {
     if (!is_array($mail)) {
         try {
             $arrayMail = $mail->toArray();
         } catch (\Exception $ex) {
             throw new \Exception('impossible to convert ' . get_class($mail) . ' to mail array');
         }
     } else {
         $arrayMail = $mail;
     }
     $this->openTransport();
     $header = $arrayMail['header'];
     $sendingMail = $this->convertor->convertToSendFormat($arrayMail);
     //        prn($sendingMail->toString());
     try {
         $this->transport->send($sendingMail);
     } catch (\Exception $ex) {
         //create checking exception to output normal view, that describes problem to user
         throw new \Exception('Mail format exception. Asc administrator to fix the problem');
         //            throw $ex;
     }
     $headers = $sendingMail->getHeaders()->toArray();
     if (isset($headers['Message-ID'])) {
         $header['message-id'] = $headers['Message-ID'];
     }
     if (is_array($mail)) {
         $mail['header'] = $header;
     } else {
         $mail->header = $header;
     }
     $this->closeTransport();
     return $mail;
 }
 /**
  * carves mail data by tags and returns
  * tagged array
  *
  * @param MailPart $mail
  *
  * return mail object of type you wish
  *
  * @throws \Exception
  * @return Array
  */
 public function carveData(MailPart $mail)
 {
     //        prn($mail->dataGetters);
     //        prn($mail);
     //        prn($mail->dataUniters);
     //        exit;
     $resArray = [];
     if (isset($mail)) {
         $resArray = $mail->getData();
         $configurators = [];
         if (is_array($resArray)) {
             $configurators = MailConvert::getConfigurators();
         }
         foreach ($resArray as $tag => $part) {
             //                prn($tag,$resArray[$tag]);
             try {
                 $resArray[$tag] = $configurators[$tag]->configure($resArray[$tag]);
             } catch (\Exception $ex) {
                 $exceptionMessage = isset($configurators[$tag]) ? 'Internal error in ' . get_class($configurators[$tag]) . ' configurator for mail tag ' . $tag : 'Configurator for tag ' . $tag . ' doesn\'t set';
                 throw new \Exception($exceptionMessage);
             }
         }
     }
     //        prn($resArray);
     //        exit;
     return $resArray;
 }
Ejemplo n.º 3
0
 /**
  * @param array $exceptProtocolUids
  *
  * @return array
  * @throws \Exception
  */
 public function fetchAll($exceptProtocolUids = [])
 {
     $this->findRootFolder();
     $exceptProtocolUids = $this->prepareExceptUids($exceptProtocolUids);
     $uids = $this->transport->getUniqueId();
     prn('root folder', $this->rootFolder->getGlobalName());
     prn('uids before', count($uids));
     $uids = array_diff($uids, $exceptProtocolUids);
     $this->lastSyncSuccessful = true;
     $settingID = $this->setting['id'];
     //        $uids = ['3AB4A466-FC5E-11E3-89A8-00215AD99F24'];
     $resUids = [];
     $count = 0;
     prn('total count', count($uids));
     foreach ($uids as $uid) {
         //            prn( 'outside' );
         $storeMail = $this->storage->find(['protocol_ids.' . $settingID => $this->getFullUid($uid)])->current();
         $rawMail = null;
         if (!isset($storeMail) || !$storeMail->is_converted) {
             prn('new mail');
             $storeMail = $this->getModelServiceVerify()->get($this->storeModel);
             //                prn( 'inside', $storeMail );
             $storeMail->protocol_ids = [$settingID => $this->getFullUid($uid)];
             //                prn( $storeMail );
             try {
                 $rawMail = $this->transport->getMessage($this->transport->getNumberByUniqueId($uid));
             } catch (\Exception $ex) {
                 $storeMail->error = ['sync_error' => $ex->getMessage()];
                 $this->lastSyncSuccessful = false;
             }
             $sameMail = $this->storage->find(['message_id' => $rawMail->message_id])->current();
             if (isset($sameMail)) {
                 $storeMail = $sameMail;
                 $protocolIds = $storeMail->protocol_ids;
                 $protocolIds[$settingID] = $this->getFullUid($uid);
                 $storeMail->protocol_ids = $protocolIds;
             }
         }
         if (!$storeMail->is_converted && isset($rawMail)) {
             $convertedMail = [];
             $content = $rawMail->getContent();
             $headers = $rawMail->getHeaders()->toString();
             if (mb_check_encoding($content, 'UTF-8')) {
                 $storeMail->raw_content = $content;
             }
             if (mb_check_encoding($headers, 'UTF-8')) {
                 $storeMail->raw_headers = $headers;
             }
             try {
                 $convertedMail = $this->convertor->convertMailToInternalFormat($rawMail);
                 prn(count($convertedMail['text']));
                 $convertedMail['link'] = [];
                 $storeMail->is_converted = false;
             } catch (\Exception $ex) {
                 $error = $storeMail->error;
                 $error['convert_error'] = $ex->getMessage();
                 $storeMail->error = $error;
             }
             $storeMail->converted_mail = $convertedMail;
             $storeMail->message_id = $rawMail->message_id;
             $size = $this->checkVariableSize($storeMail);
             if ($size > 16777216) {
                 $storeMail->raw_content = 'too big, size = ' . $size . ' bytes';
             }
             prn('size', $size);
             //                prn($storeMail);
         } else {
             $convertedMail = $storeMail->converted_mail;
             $convertedMail['link'] = [];
             $storeMail->converted_mail = $convertedMail;
         }
         $storeMail->raw_content = iconv("utf-8", "utf-8//ignore", $storeMail->raw_content);
         $cm = $storeMail->converted_mail;
         $cm['text'] = iconv("utf-8", "utf-8//ignore", $cm['text']);
         $storeMail->converted_mail = $cm;
         prn(++$count, 'here', $storeMail->message_id);
         $this->storage->save($storeMail);
         $resUids[] = $this->getFullUid($uid);
     }
     return $resUids;
 }