By bojanz on
Change record status:
Published (View all published change records)
Project:
Introduced in branch:
8.x-2.x
Introduced in version:
8.x-2.12
Description:
Commerce now has a MailHandler service (commerce.mail_handler) that should be used for sending HTML emails.
Allows a render array (with an associated #theme) to be used as the message body.
Ensures the correct translation language of both strings and entities.
Note: Since Drupal core doesn't support HTML emails out of the box, Commerce assumes that Swiftmailer (or an appropriate alternative) is used.
Code example:
$subject = $this->t('Order #@number confirmed', ['@number' => $order->getOrderNumber()]);
$body = [
'#theme' => 'commerce_order_receipt',
'#order_entity' => $order,
];
// All parameters are optional.
$params = [
// Used for hook_mail_alter(). Prepended with "commerce_".
'id' => 'order_receipt',
// Defaults to the current store email.
'from' => $order->getStore()->getEmail(),
'bcc' => $order_type->getReceiptBcc(),
// Defaults to the default language.
'langcode' => $order->getCustomer()->getPreferredLangcode(),
];
$this->mailHandler->sendMail($order->getEmail(), $subject, $body, $params);
Developers are encouraged to create a service per email under the Mail namespace. For example Drupal\commerce_order\Mail\OrderReceiptMail.
Impacts:
Module developers
Comments
$mail_handler = \Drupal: