From 08a6f7b8d085a613146cbc4429d58e3c40722156 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Sat, 12 Jan 2019 04:49:25 -0300 Subject: [PATCH] Add email polling deletion --- server/controllers/system/email-polling.php | 29 ++++++++++++++------- 1 file changed, 19 insertions(+), 10 deletions(-) diff --git a/server/controllers/system/email-polling.php b/server/controllers/system/email-polling.php index d5da6d66..e91f5c9d 100755 --- a/server/controllers/system/email-polling.php +++ b/server/controllers/system/email-polling.php @@ -4,6 +4,8 @@ class EmailPolling extends Controller { const PATH = '/email-polling'; const METHOD = 'POST'; + private $mailbox; + public function validations() { return [ 'permission' => 'any', @@ -20,6 +22,13 @@ class EmailPolling extends Controller { if(Controller::isUserSystemEnabled()) throw new RequestException(ERRORS::USER_SYSTEM); + $this->mailbox = new \PhpImap\Mailbox( + Setting::getSetting('imap-host')->getValue(), + Setting::getSetting('imap-user')->getValue(), + Setting::getSetting('imap-pass')->getValue(), + __DIR__ + ); + $errors = []; $emails = $this->getLastEmails(); @@ -85,19 +94,13 @@ class EmailPolling extends Controller { } public function getLastEmails() { - $mailbox = new \PhpImap\Mailbox( - Setting::getSetting('imap-host')->getValue(), - Setting::getSetting('imap-user')->getValue(), - Setting::getSetting('imap-pass')->getValue(), - __DIR__ - ); - - $mailsIds = $mailbox->searchMailbox('ALL'); + $mailsIds = $this->mailbox->searchMailbox('ALL'); $emails = []; sort($mailsIds); + foreach($mailsIds as $mailId) { - $mail = $mailbox->getMail($mailId); - $mailHeader = $mailbox->getMailHeader($mailId); + $mail = $this->mailbox->getMail($mailId); + $mailHeader = $this->mailbox->getMailHeader($mailId); $emails[] = new Email([ 'fromAddress' => $mailHeader->fromAddress, 'fromName' => $mailHeader->fromName, @@ -111,6 +114,12 @@ class EmailPolling extends Controller { } public function eraseAllEmails() { + $mailsIds = $this->mailbox->searchMailbox('ALL'); + foreach($mailsIds as $mailId) { + $this->mailbox->deleteMail($mailId); + } + + $this->mailbox->expungeDeletedMails(); } }