/**
  * 新メール取得
  *
  * @return array 取得メール
  * @throws RuntimeException
  */
 public function get()
 {
     // Serverのインスタンス時にホスト名が必要
     $this->server = is_null($this->server) ? new Server($this->host, $this->port) : $this->server;
     try {
         // IMAPへ接続
         $connection = $this->server->authenticate($this->user, $this->password);
         $mailbox = $connection->getMailbox('INBOX');
         // 未読だけを取り込むためフラッグを指定
         $this->search->addCondition($this->unseen);
         // メッセージの取得
         $messages = $mailbox->getMessages($this->search);
     } catch (\Exception $e) {
         Log::error('メール取得失敗:' . $e->getMessage());
         throw new RuntimeException($e->getMessage(), $e->getCode(), $e->getPrevious());
     }
     // メッセージオブジェクトの配列をそのまま返すのは
     // 結合が強いため通常の配列へ変換
     $messageArray = [];
     foreach ($messages as $message) {
         // ヘッダーからの情報
         $msg['subject'] = $message->getSubject();
         $msg['from'] = $message->getFrom()->getFullAddress();
         $msg['date'] = $message->getDate();
         // メッセージのBodyを取得すると、自動的に既読になります
         $msg['body'] = $message->getBodyText();
         $messageArray[] = $msg;
     }
     return $messageArray;
 }
Example #2
0
 public function testSearch()
 {
     $search = new SearchExpression();
     $condition = new To('*****@*****.**');
     $search->addCondition($condition);
     $messages = $this->mailbox->getMessages($search);
     $this->assertEquals('Message 1', $messages->current()->getSubject());
 }
 /**
  * Show the entire Mail.
  *
  * @param $number
  *
  * @return Response
  */
 public function show($number)
 {
     $message = $this->SearchMessageinMailboxes($number);
     if ($message) {
         $search = new SearchExpression();
         $search->addCondition(new Unseen('UNSEEN'));
         $inbox = $this->connection->getMailbox('INBOX');
         $unseen = $inbox->getMessages($search);
         return view('emails.show')->with('mail', $message)->with('cuenta', $this->cuenta)->with('inboxunread', count($unseen));
     }
     Flash::warning('Mail no Encontrado');
     return redirect('emails/inbox');
 }
Example #4
0
 public function testSearchNoResults()
 {
     $search = new SearchExpression();
     $search->addCondition(new To('*****@*****.**'));
     $this->assertCount(0, $this->mailbox->getMessages($search));
 }
    public function index()
    {
        $server = new Server('outlook.office365.com');
        // $connection is instance of \Ddeboer\Imap\Connection
        $connection = $server->authenticate('*****@*****.**', 'Data1992');
        $mailbox = $connection->getMailbox('INBOX/Home Depot');
        $search = new SearchExpression();
        $search->addCondition(new Unseen());
        $messages = $mailbox->getMessages($search);
        foreach ($messages as $message) {
            $strip = str_replace(' ', '', strip_tags($message->getBodyHtml()));
            $value = strpos($strip, 'TOTAL$');
            //Check if its a return or a purchase
            if ($value == false) {
                $thing = 'TOTAL-$';
                $num = 7;
            } else {
                $thing = 'TOTAL$';
                $num = 6;
            }
            //TOTAL $__
            $first = strpos($strip, $thing);
            $space = strpos($strip, 'X', $first) - $first - $num;
            // $value = substr($strip, $first+5, 1);
            //Check if its a return or a purchase
            if ($value == false) {
                $amount = '-' . substr($strip, $first + $num, $space);
            } else {
                $amount = substr($strip, $first + $num, $space);
            }
            //FIND PO/JOPBNAME...attach expense to project..if not porject is 0 and user is promped to select a project.
            $po = strpos($strip, 'JOBNAME:');
            if (is_bool($po)) {
                $project = '0';
            } else {
                $spc = strpos($strip, 'PRO', $po) - $po - 14;
                $pro = substr($strip, $po + 8, $spc);
                $project_id = Project::where('id', $pro)->first();
            }
            //check if PROJECT NAME/ PO/JOBNAME from receipt exists in database, if not, project is 0
            if (is_null($project_id)) {
                $project = '0';
            } else {
                $project = $project_id->id;
            }
            Expense::insert(['amount_paid' => $amount, 'employee_id' => '5', 'paid_on' => date('Y-m-d'), 'Project_id' => $project, 'created_at' => date("Y-m-d H:i:s"), 'updated_at' => date("Y-m-d H:i:s"), 'reimbursment' => 2, 'office' => 2]);
            $attachments = $message->getAttachments();
            foreach ($attachments as $attachment) {
                $file = $attachment->getDecodedContent();
                $name = time() . 'expense.pdf';
                // $file->move('docs/expenses', $name);
                file_put_contents('docs/expenses/' . $name, $attachment->getDecodedContent());
                // $file = $request->file('file');
                $url = Expense::latest()->first();
                $url->receipt_url = $name;
                $url->save();
            }
            unset($project);
            unset($project_id);
            ?>
				<br>
				<?php 
        }
    }
Example #6
0
 public function collectNews($page = 1, $limit = 20, &$errors)
 {
     $objects = array();
     $categories = $this->em->getRepository('SymbbCoreNewsBundle:Category')->findAll();
     /**
      * @var $categories Category[]
      */
     foreach ($categories as $category) {
         $sources = $category->getSources();
         foreach ($sources as $source) {
             $date = new \DateTime();
             if ($source->getLastCall()) {
                 $date->setTimestamp($source->getLastCall());
             } else {
                 $date->modify("- 2 weeks");
             }
             $source->setLastCall(new \DateTime());
             if ($source instanceof Category\Source\Email) {
                 // $connection is instance of \Ddeboer\Imap\Connection
                 try {
                     $connection = $this->getEmailConnection($source);
                     $category = $source->getCategory();
                     $mailboxes = $connection->getMailboxes();
                     foreach ($mailboxes as $mailbox) {
                         if (strpos($mailbox->getName(), "INBOX") === 0) {
                             $search = new SearchExpression();
                             $search->addCondition(new After($date));
                             $search->addCondition(new Undeleted());
                             $messages = $mailbox->getMessages($search);
                             foreach ($messages as $message) {
                                 // imap filter will only check the day not the time...
                                 if ($message->getDate() <= $date) {
                                     continue;
                                 }
                                 $text = $message->getBodyHtml();
                                 if (empty($text)) {
                                     $text = $message->getBodyText();
                                 }
                                 $text = Utils::purifyHtml($text);
                                 $objects[] = array('email_id' => $message->getId(), 'title' => $message->getSubject(), 'text' => $text, 'realSource' => $message->getFrom(), 'date' => $message->getDate(), 'type' => "email", 'category' => $category, 'source' => $source);
                             }
                         }
                     }
                 } catch (\Exception $exp) {
                     $errors[] = array('title' => "error while connection to email server", "text" => $exp->getMessage(), "date" => new \DateTime());
                 }
             } else {
                 if ($source instanceof Category\Source\Feed) {
                     $reader = $this->feedReader;
                     $feed = $reader->getFeedContent($source->getUrl(), $date);
                     $items = $feed->getItems();
                     foreach ($items as $item) {
                         $objects[] = array('feed_id' => null, 'title' => (string) $item->getTitle(), 'text' => (string) $item->getDescription(), 'date' => $item->getUpdated(), 'realSource' => (string) $item->getLink(), 'type' => "feed", 'category' => $category, 'source' => $source);
                     }
                 }
             }
             $this->em->persist($source);
         }
     }
     foreach ($objects as $object) {
         $entry = new Category\Entry();
         $entry->setCategory($object["category"]);
         $entry->setSource($object["source"]);
         $entry->setType($object["type"]);
         $entry->setTitle($object["title"]);
         $entry->setText($object["text"]);
         $entry->setDate($object["date"]);
         $this->em->persist($entry);
     }
     $this->em->flush();
     $qb = $this->em->getRepository('SymbbCoreNewsBundle:Category\\Entry')->createQueryBuilder('e');
     $qb->select("e");
     $qb->where("e.topic IS NULL");
     $qb->addOrderBy("e.date", "desc");
     $query = $qb->getQuery();
     $objects = $this->createPagination($query, $page, $limit);
     return $objects;
 }