Unverified Commit 5614f828 authored by alexpott's avatar alexpott
Browse files

Issue #2349725 by AdamPS, alexpott, hexblot, jonathanshaw, Berdir, larowlan,...

Issue #2349725 by AdamPS, alexpott, hexblot, jonathanshaw, Berdir, larowlan, andypost: Core Mail class prints non-overridable errors on the page
parent 8425a091
......@@ -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
......@@ -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);
}
}
}
}
......
......@@ -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
......
......@@ -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;
......@@ -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.
*
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment