Commit 2d727c6f authored by alexpott's avatar alexpott

Issue #2358991 by JeroenT, ianthomas_uk, rpayanm, jamesdixon,...

Issue #2358991 by JeroenT, ianthomas_uk, rpayanm, jamesdixon, er.pushpinderrana, Ashok Negi: Remove usage of drupal_mail()
parent a198ce63
......@@ -17,7 +17,7 @@
interface MailInterface {
/**
* Formats a message composed by drupal_mail() prior sending.
* Formats a message prior to sending.
*
* Allows to preprocess, format, and postprocess a mail message before it is
* passed to the sending system. By default, all messages may contain HTML and
......@@ -32,11 +32,13 @@ interface MailInterface {
*
* @return array
* The formatted $message.
*
* @see \Drupal\Core\Mail\MailManagerInterface
*/
public function format(array $message);
/**
* Sends a message composed by drupal_mail().
* Sends a message composed by \Drupal\Core\Mail\MailManagerInterface->mail().
*
* @param array $message
* Message array with at least the following elements:
......
......@@ -117,8 +117,8 @@ public function __construct(\Traversable $namespaces, CacheBackendInterface $cac
*
* @param array $options
* An array with the following key/value pairs:
* - module: (string) The module name which was used by drupal_mail() to
* invoke hook_mail().
* - module: (string) The module name which was used by
* \Drupal\Core\Mail\MailManagerInterface->mail() to invoke hook_mail().
* - key: (string) A key to identify the email sent. The final message ID
* is a string represented as {$module}_{$key}.
*
......
......@@ -48,7 +48,8 @@ interface MailManagerInterface extends PluginManagerInterface {
* function example_notify($accounts) {
* foreach ($accounts as $account) {
* $params['account'] = $account;
* // example_mail() will be called based on the first \Drupal::service('plugin.manager.mail')->mail() parameter.
* // example_mail() will be called based on the first
* // MailManagerInterface->mail() parameter.
* \Drupal::service('plugin.manager.mail')->mail('example', 'notice', $account->mail, user_preferred_langcode($account), $params);
* }
* }
......@@ -72,8 +73,8 @@ interface MailManagerInterface extends PluginManagerInterface {
* }
* @endcode
*
* Another example, which uses \Drupal::service('plugin.manager.mail')->mail()
* to format a message for sending later:
* Another example, which uses MailManagerInterface->mail() to format a
* message for sending later:
*
* @code
* $params = array('current_conditions' => $data);
......@@ -108,9 +109,9 @@ interface MailManagerInterface extends PluginManagerInterface {
* @param string|null $reply
* Optional email address to be used to answer.
* @param bool $send
* If TRUE, \Drupal::service('plugin.manager.mail')->mail() will call
* \Drupal::service('plugin.manager.mail')->mail() to deliver the message,
* and store the result in $message['result']. Modules implementing
* If TRUE, call an implementation of
* \Drupal\Core\Mail\MailInterface->mail() to deliver the message, and
* store the result in $message['result']. Modules implementing
* hook_mail_alter() may cancel sending by setting $message['send'] to
* FALSE.
*
......
......@@ -53,7 +53,7 @@ public function format(array $message) {
* TRUE if the mail was successfully accepted, otherwise FALSE.
*
* @see http://php.net/manual/en/function.mail.php
* @see drupal_mail()
* @see \Drupal\Core\Mail\MailManagerInterface::mail()
*/
public function mail(array $message) {
// If 'Return-Path' isn't already set in php.ini, we pass it separately
......
......@@ -10,6 +10,7 @@
use Drupal\Core\Action\ConfigurableActionBase;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Plugin\ContainerFactoryPluginInterface;
use Drupal\Core\Utility\Token;
use Psr\Log\LoggerInterface;
......@@ -47,6 +48,13 @@ class EmailAction extends ConfigurableActionBase implements ContainerFactoryPlug
*/
protected $logger;
/**
* The mail manager
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* Constructs a EmailAction object.
*
......@@ -62,13 +70,16 @@ class EmailAction extends ConfigurableActionBase implements ContainerFactoryPlug
* The entity manager.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\Mail\MailManagerInterface
* The mail manager.
*/
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, EntityManagerInterface $entity_manager, LoggerInterface $logger) {
public function __construct(array $configuration, $plugin_id, $plugin_definition, Token $token, EntityManagerInterface $entity_manager, LoggerInterface $logger, MailManagerInterface $mail_manager) {
parent::__construct($configuration, $plugin_id, $plugin_definition);
$this->token = $token;
$this->storage = $entity_manager->getStorage('user');
$this->logger = $logger;
$this->mailManager = $mail_manager;
}
/**
......@@ -78,7 +89,8 @@ public static function create(ContainerInterface $container, array $configuratio
return new static($configuration, $plugin_id, $plugin_definition,
$container->get('token'),
$container->get('entity.manager'),
$container->get('logger.factory')->get('action')
$container->get('logger.factory')->get('action'),
$container->get('plugin.manager.mail')
);
}
......@@ -105,7 +117,7 @@ public function execute($entity = NULL) {
}
$params = array('context' => $this->configuration);
if (drupal_mail('system', 'action_send_email', $recipient, $langcode, $params)) {
if ($this->mailManager->mail('system', 'action_send_email', $recipient, $langcode, $params)) {
$this->logger->notice('Sent email to %recipient', array('%recipient' => $recipient));
}
else {
......
......@@ -60,7 +60,7 @@ public function testCancelMessage() {
\Drupal::state()->set('system.test_mail_collector', array());
// Send a test message that simpletest_mail_alter should cancel.
drupal_mail('simpletest', 'cancel_test', 'cancel@example.com', $language_interface->getId());
\Drupal::service('plugin.manager.mail')->mail('simpletest', 'cancel_test', 'cancel@example.com', $language_interface->getId());
// Retrieve sent message.
$captured_emails = \Drupal::state()->get('system.test_mail_collector');
$sent_message = end($captured_emails);
......@@ -82,7 +82,7 @@ public function testFromAndReplyToHeader() {
// Send an email with a reply-to address specified.
$from_email = 'Drupal <simpletest@example.com>';
$reply_email = 'someone_else@example.com';
drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $reply_email);
\Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $reply_email);
// Test that the reply-to email is just the email and not the site name
// and default sender email.
$captured_emails = \Drupal::state()->get('system.test_mail_collector');
......@@ -92,7 +92,7 @@ public function testFromAndReplyToHeader() {
$this->assertFalse(isset($sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.');
// Send an email and check that the From-header contains the site name.
drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language);
\Drupal::service('plugin.manager.mail')->mail('simpletest', 'from_test', 'from_test@example.com', $language);
$captured_emails = \Drupal::state()->get('system.test_mail_collector');
$sent_message = end($captured_emails);
$this->assertEqual($from_email, $sent_message['headers']['From'], 'Message is sent from the site email account.');
......
......@@ -297,23 +297,23 @@ function hook_contextual_links_plugins_alter(array &$contextual_links) {
}
/**
* Alter an email message created with the drupal_mail() function.
* Alter an email message created with MailManagerInterface->mail().
*
* hook_mail_alter() allows modification of email messages created and sent
* with drupal_mail(). Usage examples include adding and/or changing message
* text, message fields, and message headers.
* with MailManagerInterface->mail(). Usage examples include adding and/or
* changing message text, message fields, and message headers.
*
* Email messages sent using functions other than drupal_mail() will not
* invoke hook_mail_alter(). For example, a contributed module directly
* calling the \Drupal::service('plugin.manager.mail')->mail() or PHP mail()
* function will not invoke this hook. All core modules use drupal_mail() for
* messaging, it is best practice but not mandatory in contributed modules.
* Email messages sent using functions other than MailManagerInterface->mail()
* will not invoke hook_mail_alter(). For example, a contributed module directly
* calling the MailInterface->mail() or PHP mail() function will not invoke
* this hook. All core modules use MailManagerInterface->mail() for messaging,
* it is best practice but not mandatory in contributed modules.
*
* @param $message
* An array containing the message data. Keys in this array include:
* - 'id':
* The drupal_mail() id of the message. Look at module source code or
* drupal_mail() for possible id values.
* The MailManagerInterface->mail() id of the message. Look at module source
* code or MailManagerInterface->mail() for possible id values.
* - 'to':
* The address or addresses the message will be sent to. The
* formatting of this string must comply with RFC 2822.
......@@ -331,15 +331,16 @@ function hook_contextual_links_plugins_alter(array &$contextual_links) {
* Associative array containing mail headers, such as From, Sender,
* MIME-Version, Content-Type, etc.
* - 'params':
* An array of optional parameters supplied by the caller of drupal_mail()
* that is used to build the message before hook_mail_alter() is invoked.
* An array of optional parameters supplied by the caller of
* MailManagerInterface->mail() that is used to build the message before
* hook_mail_alter() is invoked.
* - 'language':
* The language object used to build the message before hook_mail_alter()
* is invoked.
* - 'send':
* Set to FALSE to abort sending this email message.
*
* @see drupal_mail()
* @see \Drupal\Core\Mail\MailManagerInterface::mail()
*/
function hook_mail_alter(&$message) {
if ($message['id'] == 'modulename_messagekey') {
......@@ -376,33 +377,38 @@ function hook_system_breadcrumb_alter(array &$breadcrumb, \Drupal\Core\Routing\R
}
/**
* Prepare a message based on parameters; called from drupal_mail().
* Prepares a message based on parameters;
*
* Note that hook_mail(), unlike hook_mail_alter(), is only called on the
* $module argument to drupal_mail(), not all modules.
* This hook is called from MailManagerInterface->mail(). Note that hook_mail(),
* unlike hook_mail_alter(), is only called on the $module argument to
* MailManagerInterface->mail(), not all modules.
*
* @param $key
* An identifier of the mail.
* @param $message
* An array to be filled in. Elements in this array include:
* - id: An ID to identify the mail sent. Look at module source code
* or drupal_mail() for possible id values.
* - id: An ID to identify the mail sent. Look at module source code or
* MailManagerInterface->mail() for possible id values.
* - to: The address or addresses the message will be sent to. The
* formatting of this string must comply with RFC 2822.
* - subject: Subject of the email to be sent. This must not contain any
* newline characters, or the mail may not be sent properly. drupal_mail()
* sets this to an empty string when the hook is invoked.
* newline characters, or the mail may not be sent properly.
* MailManagerInterface->mail() sets this to an empty
* string when the hook is invoked.
* - body: An array of lines containing the message to be sent. Drupal will
* format the correct line endings for you. drupal_mail() sets this to an
* empty array when the hook is invoked.
* format the correct line endings for you. MailManagerInterface->mail()
* sets this to an empty array when the hook is invoked.
* - from: The address the message will be marked as being from, which is
* set by drupal_mail() to either a custom address or the site-wide
* default email address when the hook is invoked.
* set by MailManagerInterface->mail() to either a custom address or the
* site-wide default email address when the hook is invoked.
* - headers: Associative array containing mail headers, such as From,
* Sender, MIME-Version, Content-Type, etc. drupal_mail() pre-fills
* several headers in this array.
* Sender, MIME-Version, Content-Type, etc.
* MailManagerInterface->mail() pre-fills several headers in this array.
* @param $params
* An array of parameters supplied by the caller of drupal_mail().
* An array of parameters supplied by the caller of
* MailManagerInterface->mail().
*
* @see \Drupal\Core\Mail\MailManagerInterface->mail()
*/
function hook_mail($key, &$message, $params) {
$account = $params['account'];
......
......@@ -62,7 +62,7 @@ function _update_cron_notify() {
else {
$target_langcode = $default_langcode;
}
$message = drupal_mail('update', 'status_notify', $target, $target_langcode, $params);
$message = \Drupal::service('plugin.manager.mail')->mail('update', 'status_notify', $target, $target_langcode, $params);
// Track when the last mail was successfully sent to avoid sending
// too many emails.
if ($message['result']) {
......
......@@ -446,7 +446,7 @@ function update_fetch_data_finished($success, $results) {
* keys, and the status reason constant (UPDATE_NOT_SECURE, etc) for the
* values.
*
* @see drupal_mail()
* @see \Drupal\Core\Mail\MailManagerInterface::mail()
* @see _update_cron_notify()
* @see _update_message_text()
*/
......
......@@ -1257,7 +1257,7 @@ function user_role_revoke_permissions($rid, array $permissions = array()) {
* operation happens on the given user account.
*
* @see user_mail_tokens()
* @see drupal_mail()
* @see \Drupal\Core\Mail\MailManagerInterface::mail()
*
* @param $op
* The operation being performed on the account. Possible values:
......@@ -1300,11 +1300,11 @@ function _user_mail_notify($op, $account, $langcode = NULL) {
if (empty($site_mail)) {
$site_mail = ini_get('sendmail_from');
}
$mail = drupal_mail('user', $op, $account->getEmail(), $langcode, $params, $site_mail);
$mail = \Drupal::service('plugin.manager.mail')->mail('user', $op, $account->getEmail(), $langcode, $params, $site_mail);
if ($op == 'register_pending_approval') {
// If a user registered requiring admin approval, notify the admin, too.
// We use the site default language for this.
drupal_mail('user', 'register_pending_approval_admin', $site_mail, \Drupal::languageManager()->getDefaultLanguage()->getId(), $params);
\Drupal::service('plugin.manager.mail')->mail('user', 'register_pending_approval_admin', $site_mail, \Drupal::languageManager()->getDefaultLanguage()->getId(), $params);
}
}
return empty($mail) ? NULL : $mail['result'];
......
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