$body =
$this->
randomString(128
);
$message =
[ 'id' => 'drupal_mail_test',
'headers' =>
['Content-type' => 'text/html'
],
'subject' =>
$subject,
'to' => 'foobar@example.com',
'body' =>
$body,
];
// Before we send the email, \Drupal\Core\Test\AssertMailTrait::getMails()
// should return an empty array.
$captured_emails =
$this->
getMails();
$this->
assertCount(0,
$captured_emails, 'The captured emails queue is empty.'
);
// Send the email.
$mail_service->
getInstance(['module' => 'simpletest', 'key' => 'drupal_mail_test'
])->
mail($message);
// Ensure that there is one email in the captured emails array.
$captured_emails =
$this->
getMails();
$this->
assertCount(1,
$captured_emails, 'One email was captured.'
);
// Asserts that the message fields have the pattern in it.
$this->
assertMailPattern('id',
$message['id'
]);