'headers' =>
['Content-type' => 'text/html'
],
'subject' =>
$subject,
'to' => 'foobar@example.com',
'body' =>
$body,
];
// Before we send the email, drupalGetMails should return an empty array.
$captured_emails =
$this->
drupalGetMails();
$this->
assertCount(0,
$captured_emails, 'The captured emails queue is empty.'
);
// Send the email.
\Drupal::
service('plugin.manager.mail'
)->
getInstance(['module' => 'test', 'key' => 'drupal_mail_test'
])->
mail($message);
// Ensure that there is one email in the captured emails array.
$captured_emails =
$this->
drupalGetMails();
$this->
assertCount(1,
$captured_emails, 'One email was captured.'
);
// Assert that the email was sent by iterating over the message properties
// and ensuring that they are captured intact.
foreach ($message as $field =>
$value) { $this->
assertMail($field,
$value,
new FormattableMarkup('The email was sent and the value for property @field is intact.',
['@field' =>
$field]));
}