You are here

public function TestMailManager::doMail in Drupal 8

Same name and namespace in other branches
  1. 9 core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php \Drupal\Tests\Core\Mail\TestMailManager::doMail()

Composes and optionally sends an email message.

Parameters

string $module: A module name to invoke hook_mail() on. The {$module}_mail() hook will be called to complete the $message structure which will already contain common defaults.

string $key: A key to identify the email sent. The final message ID for email altering will be {$module}_{$key}.

string $to: The email address or addresses where the message will be sent to. The formatting of this string will be validated with the PHP email validation filter. Some examples are:

string $langcode: Language code to use to compose the email.

array $params: (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.

string|null $reply: Optional email address to be used to answer.

bool $send: 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.

Return value

array The $message array structure containing all details of the message. If already sent ($send = TRUE), then the 'result' element will contain the success indicator of the email, failure being already written to the watchdog. (Success means nothing more than the message being accepted at php-level, which still doesn't guarantee it to be delivered.)

Overrides MailManager::doMail

See also

\Drupal\Core\Mail\MailManagerInterface::mail()

File

core/tests/Drupal/Tests/Core/Mail/MailManagerTest.php, line 188
Contains \Drupal\Tests\Core\Mail\MailManagerTest.

Class

TestMailManager
Provides a testing version of MailManager with an empty constructor.

Namespace

Drupal\Tests\Core\Mail

Code

public function doMail($module, $key, $to, $langcode, $params = [], $reply = NULL, $send = TRUE) {

  // Build a simplified message array and return it.
  $message = [
    'id' => $module . '_' . $key,
    'module' => $module,
    'key' => $key,
    'to' => $to,
    'from' => 'from@example.org',
    'reply-to' => $reply,
    'langcode' => $langcode,
    'params' => $params,
    'send' => TRUE,
    'subject' => '',
    'body' => [],
  ];
  return $message;
}