public function MailHandler::sendMail in Commerce Core 8.2
Sends an email to a user.
Parameters
string $to: The address the email will be sent to. Must comply with RFC 2822.
string $subject: The subject. Must not contain any newline characters.
array $body: A render array representing the message body.
array $params: Email parameters. Recognized keys:
- id: A unique identifier of the email type. Allows hook_mail_alter() implementations to identify specific emails. Defaults to "mail". Automatically prefixed with "commerce_".
- from: The address the email will be marked as being from. Defaults to the current store email.
- reply-to: The address to which the reply will be sent. No default.
- cc: The CC address or addresses (separated by a comma). No default.
- bcc: The BCC address or addresses (separated by a comma). No default.
- langcode: The email langcode. Every translatable string and entity will be rendered in this language. Defaults to the current language.
Return value
bool TRUE if the email was sent successfully, FALSE otherwise.
Overrides MailHandlerInterface::sendMail
File
- src/
MailHandler.php, line 68
Class
Namespace
Drupal\commerceCode
public function sendMail($to, $subject, array $body, array $params = []) {
if (empty($to)) {
return FALSE;
}
$default_params = [
'headers' => [
'Content-Type' => 'text/html; charset=UTF-8;',
'Content-Transfer-Encoding' => '8Bit',
],
'id' => 'mail',
// The 'from' address will be set by commerce_store_mail_alter().
'from' => '',
'reply-to' => NULL,
'subject' => $subject,
'langcode' => $this->languageManager
->getCurrentLanguage()
->getId(),
// The body will be rendered in commerce_mail(), because that's what
// MailManager expects. The correct theme and render context aren't
// setup until then.
'body' => $body,
];
if (!empty($params['cc'])) {
$default_params['headers']['Cc'] = $params['cc'];
}
if (!empty($params['bcc'])) {
$default_params['headers']['Bcc'] = $params['bcc'];
}
$params = array_replace($default_params, $params);
// Change the active language to ensure the email is properly translated.
if ($params['langcode'] != $default_params['langcode']) {
$this
->changeActiveLanguage($params['langcode']);
}
$message = $this->mailManager
->mail('commerce', $params['id'], $to, $params['langcode'], $params, $params['reply-to']);
// Revert back to the original active language.
if ($params['langcode'] != $default_params['langcode']) {
$this
->changeActiveLanguage($default_params['langcode']);
}
// Allow modules to react after an email has been sent.
$event = new PostMailSendEvent($params, $message);
$this->eventDispatcher
->dispatch(CommerceEvents::POST_MAIL_SEND, $event);
return (bool) $message['result'];
}