Skip to content

Commit

Permalink
dev/core#2159 Handle exceptions in Mail:send class
Browse files Browse the repository at this point in the history
This adds handling for exceptions - allowing the class to be more effectivel swapped out
- need to think a bit more about the messages though
  • Loading branch information
eileenmcnaughton committed Dec 10, 2020
1 parent 2be7783 commit b9ffa87
Show file tree
Hide file tree
Showing 2 changed files with 38 additions and 9 deletions.
12 changes: 9 additions & 3 deletions CRM/Utils/Mail.php
Original file line number Diff line number Diff line change
Expand Up @@ -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)
) {
Expand Down Expand Up @@ -94,7 +95,7 @@ public static function createMailer() {
$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 <a href=\'%1\'>Administer >> System Setting >> Outbound Email</a> to set the OutBound Email.', [1 => CRM_Utils_System::url('civicrm/admin/setting/smtp', 'reset=1')]));
Expand Down Expand Up @@ -301,8 +302,13 @@ public static function send(&$params) {
}

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
Expand Down
35 changes: 29 additions & 6 deletions tests/phpunit/CRM/Utils/MailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [
[
Expand Down Expand Up @@ -53,10 +49,37 @@ public function testFormatRFC822() {
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' => [$this, '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 function mailerError(&$mailer): void {
$mailer = PEAR::raiseError('You shall not pass');
}
}

0 comments on commit b9ffa87

Please sign in to comment.