public function import()
 {
     $conn = new \Zend\Mail\Storage\Imap($this->getConfig()['support']['customer_mails']);
     $em = $this->getServiceLocator()->get('Doctrine\\ORM\\EntityManager');
     $em->getConnection()->beginTransaction();
     $messageRepo = $em->getRepository('\\Common\\Entity\\Message');
     for ($i = $conn->countMessages(); $i; --$i) {
         try {
             $email = $conn->getMessage($i);
             // check for doubles
             $messageId = isset($email->message_id) ? $email->message_id : $conn->getUniqueId($i);
             $messageId = trim($messageId, " \t\r\n<>");
             $messageId = preg_replace('/[^a-zA-Z0-9-_@]\\./', '_', $messageId);
             /*if ($messageId == '*****@*****.**') {
                                 $this->message = new Message();
                                 $this->message->setCode($messageId)
                                         ->setImportType(Message::IMPORT_TYPE_EMAIL)
                                         ->setSubject($email->subject)
                                         ->setSendedOn(new \DateTime(date('Y-m-d H:i:s', isset($email->date) ? strtotime($email->date) : time())));
                                 $body = $this->getBody($email);
                                 $this->message->setBody($body);
                                 
                                 $em->persist($this->message);
             
                                 $this->setEmails($this->parseEmails($email->from), Address::TYPE_FROM);
                                 $this->setEmails($this->parseEmails($email->to), Address::TYPE_TO);
                                 
                                 $em->persist($this->message);
                                 $em->flush();
                                 $em->getConnection()->commit();
                                 die;
                             }
                             continue;*/
             $date = new \DateTime(date('Y-m-d H:i:s', isset($email->date) ? strtotime($email->date) : time()));
             if ($messageRepo->findBy(array('code' => $messageId))) {
                 continue;
             }
             $from = $this->parseEmails($email->from);
             if ($from[0]->host == 'archonia.com') {
                 continue;
             }
             // create the message
             $this->message = new Message();
             // set the email addresses
             $this->setEmails($this->parseEmails($email->from), Address::TYPE_FROM);
             $this->setEmails($this->parseEmails($email->to), Address::TYPE_TO);
             if (isset($email->cc) && !empty($email->cc)) {
                 $this->setEmails($this->parseEmails($email->cc), Address::TYPE_CC);
             }
             if (isset($email->bcc) && !empty($email->bcc)) {
                 $this->setEmails($this->parseEmails($email->bcc), Address::TYPE_BCC);
             }
             // set message data
             $this->message->setCode($messageId)->setImportType(Message::IMPORT_TYPE_EMAIL)->setSubject($email->subject)->setSendedOn($date);
             $body = $this->getBody($email);
             $this->message->setBody($body);
             $em->persist($this->message);
             $em->flush();
             parent::rules($em);
             // parent message
             if (isset($email->References)) {
                 $messageParent = $messageRepo->findOneBy(array('code' => $email->References));
                 if ($messageParent) {
                     $this->message->setParent($messageParent);
                     $msgProfile = $messageParent->getMessageProfiles()->filter(function ($entry) {
                         if ($entry->getOwner()) {
                             return true;
                         } else {
                             return false;
                         }
                     });
                     if ($msgProfile->count() === 1) {
                         $messageProfile = new Message\Profile();
                         $messageProfile->setProfile($msgProfile->first()->getProfile())->setOwner(true)->setMessage($this->message);
                         $this->message->getMessageProfiles()->add($messageProfile);
                         $state = new Message\State();
                         $state->setMessage($this->message)->setState(Message\State::STATE_OWNED);
                         $this->message->getStates()->add($state);
                         $em->persist($this->message);
                         $em->flush();
                     }
                 }
             }
             $em->getConnection()->commit();
         } catch (\Exception $e) {
             \Common\Service\Logger::getInstance()->err('mail import error' . $this->message->getId(), array('error' => $e->getMessage(), 'file' => $e->getFile(), 'line' => $e->getLine()));
             $em->getConnection()->rollback();
             // make sure our em is still open. Due to hacky emails, inserts can go wrong and make to em closed.
             if (!$em->isOpen()) {
                 $em = $em->create($em->getConnection(), $em->getConfiguration());
             }
         }
         $em->getConnection()->beginTransaction();
     }
 }
     continue;
 }
 $subject = decodeSubject($mail->subject);
 $subject = str_replace('Fwd: ', '', $subject);
 $pid = false;
 /* try to get target folder from subject*/
 $path = false;
 //case_nr
 /* try to find user from database that corresponds to this mail.
    Ex: Kell <*****@*****.**> */
 $email = false;
 if (preg_match_all('/^[^<]*<?([^>]+)>?/i', $mail->from, $results)) {
     $email = $results[1][0];
 }
 if ($email == false) {
     $delete_ids[] = $mailbox->getUniqueId($k);
     echo "\rcannot find senders email for: {$subject} ... skipping";
     mail($mail->from, 'Error processing your email: ' . $subject, '. We didn\'t find ' . 'your email in received message. ' . $mail_requirements, 'From: ' . $core['mail_user'] . "\n\r");
     continue;
 }
 $user_id = DM\User::getIdByEmail($email);
 if (!empty($user_id)) {
     if (empty($test_user_id)) {
         $delete_ids[] = $mailbox->getUniqueId($k);
         mail($mail->from, 'Error processing your email: ' . $subject, '. We didn\'t find your' . ' email address in our users database, please update your email ' . 'address in your user profile of casebox and resend your mail. ' . 'Wrong messages are deleted automatically.' . $mail_requirements, 'From: ' . $core['mail_user'] . "\n\r");
         echo "\rcannot find corresponding user in our database '.\n                'for email {$email} from message: {$subject} ... skipping";
         continue;
     } else {
         $user_id = $test_user_id;
     }
 }