Loading core/lib/Drupal/Core/Mail/MailManager.php +7 −2 Original line number Diff line number Diff line Loading @@ -202,7 +202,9 @@ public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, * @param string $langcode * Language code to use to compose the email. * @param array $params * (optional) Parameters to build the email. * (optional) Parameters to build the email. Use the key '_error_message' * to provide translatable markup to display as a message if an error * occurs, or set this to false to disable error display. * @param string|null $reply * Optional email address to be used to answer. * @param bool $send Loading Loading @@ -311,7 +313,10 @@ public function doMail($module, $key, $to, $langcode, $params = [], $reply = NUL '%to' => $message['to'], '%reply' => $message['reply-to'] ? $message['reply-to'] : $this->t('not set'), ]); $this->messenger()->addError($this->t('Unable to send email. Contact the site administrator if the problem persists.')); $error_message = $params['_error_message'] ?? $this->t('Unable to send email. Contact the site administrator if the problem persists.'); if ($error_message) { $this->messenger()->addError($error_message); } } } } Loading core/lib/Drupal/Core/Mail/MailManagerInterface.php +3 −1 Original line number Diff line number Diff line Loading @@ -102,7 +102,9 @@ interface MailManagerInterface extends PluginManagerInterface { * @param string $langcode * Language code to use to compose the email. * @param array $params * (optional) Parameters to build the email. * (optional) Parameters to build the email. Use the key '_error_message' * to provide translatable markup to display as a message if an error * occurs, or set this to false to disable error display. * @param string|null $reply * Optional email address to be used to answer. * @param bool $send Loading core/modules/system/tests/src/Functional/Mail/MailTest.php +19 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Mail\Plugin\Mail\TestMailCollector; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Render\Markup; use Drupal\Core\Url; use Drupal\file\Entity\File; Loading Loading @@ -51,6 +52,24 @@ public function testPluggableFramework() { $this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.'); } /** * Assert that the pluggable mail system is functional. */ public function testErrorMessageDisplay() { // Switch mail backends. $this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save(); // Test with errors displayed to users. \Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en'); $messages = \Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR); $this->assertEquals('Unable to send email. Contact the site administrator if the problem persists.', $messages[0]); \Drupal::messenger()->deleteAll(); // Test without errors displayed to users. \Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en', ['_error_message' => '']); $this->assertEmpty(\Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR)); } /** * Test that message sending may be canceled. * Loading Loading
core/lib/Drupal/Core/Mail/MailManager.php +7 −2 Original line number Diff line number Diff line Loading @@ -202,7 +202,9 @@ public function mail($module, $key, $to, $langcode, $params = [], $reply = NULL, * @param string $langcode * Language code to use to compose the email. * @param array $params * (optional) Parameters to build the email. * (optional) Parameters to build the email. Use the key '_error_message' * to provide translatable markup to display as a message if an error * occurs, or set this to false to disable error display. * @param string|null $reply * Optional email address to be used to answer. * @param bool $send Loading Loading @@ -311,7 +313,10 @@ public function doMail($module, $key, $to, $langcode, $params = [], $reply = NUL '%to' => $message['to'], '%reply' => $message['reply-to'] ? $message['reply-to'] : $this->t('not set'), ]); $this->messenger()->addError($this->t('Unable to send email. Contact the site administrator if the problem persists.')); $error_message = $params['_error_message'] ?? $this->t('Unable to send email. Contact the site administrator if the problem persists.'); if ($error_message) { $this->messenger()->addError($error_message); } } } } Loading
core/lib/Drupal/Core/Mail/MailManagerInterface.php +3 −1 Original line number Diff line number Diff line Loading @@ -102,7 +102,9 @@ interface MailManagerInterface extends PluginManagerInterface { * @param string $langcode * Language code to use to compose the email. * @param array $params * (optional) Parameters to build the email. * (optional) Parameters to build the email. Use the key '_error_message' * to provide translatable markup to display as a message if an error * occurs, or set this to false to disable error display. * @param string|null $reply * Optional email address to be used to answer. * @param bool $send Loading
core/modules/system/tests/src/Functional/Mail/MailTest.php +19 −0 Original line number Diff line number Diff line Loading @@ -6,6 +6,7 @@ use Drupal\Component\Utility\Unicode; use Drupal\Core\Mail\MailFormatHelper; use Drupal\Core\Mail\Plugin\Mail\TestMailCollector; use Drupal\Core\Messenger\MessengerInterface; use Drupal\Core\Render\Markup; use Drupal\Core\Url; use Drupal\file\Entity\File; Loading Loading @@ -51,6 +52,24 @@ public function testPluggableFramework() { $this->assertTrue($mail_backend instanceof TestMailCollector, 'Additional mail interfaces can be added.'); } /** * Assert that the pluggable mail system is functional. */ public function testErrorMessageDisplay() { // Switch mail backends. $this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save(); // Test with errors displayed to users. \Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en'); $messages = \Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR); $this->assertEquals('Unable to send email. Contact the site administrator if the problem persists.', $messages[0]); \Drupal::messenger()->deleteAll(); // Test without errors displayed to users. \Drupal::service('plugin.manager.mail')->mail('default', 'default', 'test@example.com', 'en', ['_error_message' => '']); $this->assertEmpty(\Drupal::messenger()->messagesByType(MessengerInterface::TYPE_ERROR)); } /** * Test that message sending may be canceled. * Loading