From b3d1f857dc489a301681365a70c1e29471801bf1 Mon Sep 17 00:00:00 2001 From: Martin Bohal Date: Sat, 2 Nov 2019 12:34:03 +0100 Subject: [PATCH] Do not show 500 error if email credentials are wrong, fixes #37 (#63) --- .../controllers/MailQueuedController.php | 7 ++++-- application/services/Cron/Mail.php | 4 +++- application/services/Email.php | 23 +++++++++---------- 3 files changed, 19 insertions(+), 15 deletions(-) diff --git a/application/modules/admin/controllers/MailQueuedController.php b/application/modules/admin/controllers/MailQueuedController.php index 73ba1cb42..772877d64 100644 --- a/application/modules/admin/controllers/MailQueuedController.php +++ b/application/modules/admin/controllers/MailQueuedController.php @@ -32,8 +32,11 @@ public function indexAction() */ public function sendAllAction() { - (new Service_Email())->sendQueued(); - $this->_flashMessenger->addMessage('All emails have been successfully sent.', 'success'); + if ((new Service_Email())->sendQueued()) { + $this->_flashMessenger->addMessage('All emails have been successfully sent.', 'success'); + } else { + $this->_flashMessenger->addMessage('There was an error when sending the emails.', 'error'); + } $this->redirect($this->view->url(['action' => 'index']), ['prependBase' => false]); } } diff --git a/application/services/Cron/Mail.php b/application/services/Cron/Mail.php index 5ac316cd3..01645ab1f 100644 --- a/application/services/Cron/Mail.php +++ b/application/services/Cron/Mail.php @@ -7,6 +7,8 @@ class Service_Cron_Mail extends Service_Cron */ public function execute() { - (new Service_Email())->sendQueued(); + if (!(new Service_Email())->sendQueued()) { + throw new Exception('There was an error when sending the emails'); + } } } diff --git a/application/services/Email.php b/application/services/Email.php index 35d2bc8f4..efc954d91 100644 --- a/application/services/Email.php +++ b/application/services/Email.php @@ -15,11 +15,7 @@ public function queueForSend(Dbjr_Mail $mail) return $this; } - /** - * Sends all unsent emails - * @return Service_Email Provides fluent interface - */ - public function sendQueued() + public function sendQueued(): bool { $mailModel = new Model_Mail(); $emails = $mailModel->fetchAll( @@ -51,14 +47,17 @@ public function sendQueued() $mailer->addBcc($recipient->email, $recipient->name); } } - $mailer->send(); - - $mailModel->update( - array('time_sent' => Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss')), - array('id=?' => $email->id) - ); + try { + $mailer->send(); + $mailModel->update( + array('time_sent' => Zend_Date::now()->toString('YYYY-MM-dd HH:mm:ss')), + array('id=?' => $email->id) + ); + } catch (Zend_Mail_Protocol_Exception $e) { + return false; + } } - return $this; + return true; } }