From c9d8809ce10d686ac00bc732292f57058fc7f48a Mon Sep 17 00:00:00 2001 From: eileen Date: Mon, 2 Nov 2020 19:21:16 +1300 Subject: [PATCH] dev/core#2159 Handle exceptions in Mail:send class This adds handling for exceptions - allowing the class to be more effectivel swapped out - need to think a bit more about the messages though --- CRM/Utils/Mail.php | 12 +++++++--- tests/phpunit/CRM/Utils/MailTest.php | 36 +++++++++++++++++++++++----- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/CRM/Utils/Mail.php b/CRM/Utils/Mail.php index 0ac404f143..a33c03d86a 100644 --- a/CRM/Utils/Mail.php +++ b/CRM/Utils/Mail.php @@ -31,6 +31,7 @@ class CRM_Utils_Mail { public static function createMailer() { $mailingInfo = Civi::settings()->get('mailing_backend'); + /*@var Mail $mailer*/ if ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_REDIRECT_TO_DB || (defined('CIVICRM_MAILER_SPOOL') && CIVICRM_MAILER_SPOOL) ) { @@ -94,7 +95,7 @@ class CRM_Utils_Mail { $mailer = self::_createMailer('mail', []); } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_MOCK) { - $mailer = self::_createMailer('mock', []); + $mailer = self::_createMailer('mock', $mailingInfo); } elseif ($mailingInfo['outBound_option'] == CRM_Mailing_Config::OUTBOUND_OPTION_DISABLED) { CRM_Core_Error::debug_log_message(ts('Outbound mail has been disabled. Click Administer >> System Setting >> Outbound Email to set the OutBound Email.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')])); @@ -301,8 +302,13 @@ class CRM_Utils_Mail { } if (is_object($mailer)) { - $errorScope = CRM_Core_TemporaryErrorScope::ignoreException(); - $result = $mailer->send($to, $headers, $message); + try { + $result = $mailer->send($to, $headers, $message); + } + catch (Exception $e) { + CRM_Core_Session::setStatus($e->getMessage(), ts('Mailing Error'), 'error'); + return FALSE; + } if (is_a($result, 'PEAR_Error')) { $message = self::errorMessage($mailer, $result); // append error message in case multiple calls are being made to diff --git a/tests/phpunit/CRM/Utils/MailTest.php b/tests/phpunit/CRM/Utils/MailTest.php index f87b9f8392..a70cfc5436 100644 --- a/tests/phpunit/CRM/Utils/MailTest.php +++ b/tests/phpunit/CRM/Utils/MailTest.php @@ -6,15 +6,11 @@ */ class CRM_Utils_MailTest extends CiviUnitTestCase { - public function setUp() { - parent::setUp(); - } - /** * Test case for add( ) * test with empty params. */ - public function testFormatRFC822() { + public function testFormatRFC822(): void { $values = [ [ @@ -53,10 +49,38 @@ class CRM_Utils_MailTest extends CiviUnitTestCase { foreach ($values as $value) { $result = CRM_Utils_Mail::formatRFC822Email($value['name'], $value['email'], - CRM_Utils_Array::value('useQuote', $value, FALSE) + $value['useQuote'] ?? FALSE ); $this->assertEquals($result, $value['result'], 'Expected encoding does not match'); } } + /** + * Test exception handling in mail function. + */ + public function testMailException(): void { + $params = [ + 'toEmail' => 'a@example.com', + 'from' => 'b@example.com', + ]; + Civi::settings()->set('mailing_backend', [ + 'outBound_option' => CRM_Mailing_Config::OUTBOUND_OPTION_MOCK, + 'preSendCallback' => ['CRM_Utils_MailTest', 'mailerError'], + ]); + + $this->assertFalse(CRM_Utils_Mail::send($params)); + $this->assertEquals('You shall not pass', CRM_Core_Session::singleton()->getStatus()[0]['text']); + } + + /** + * Mimic exception in mailer class. + * + * @throws \PEAR_Exception + * + * @param Mail $mailer + */ + public static function mailerError(&$mailer): void { + $mailer = PEAR::raiseError('You shall not pass'); + } + } -- 2.25.1