You are here

function uc_order_mail in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 uc_order/uc_order.module \uc_order_mail()
  2. 7.3 uc_order/uc_order.module \uc_order_mail()

Implements hook_mail().

File

uc_order/uc_order.module, line 144
Handles all things concerning Ubercart orders.

Code

function uc_order_mail($key, &$message, $params) {
  $langcode = isset($message['language']) ? $message['language']->language : NULL;

  // Build the appropriate message parameters based on the e-mail key.
  switch ($key) {

    // Setup an e-mailed invoice.
    case 'invoice':
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
      $message['subject'] = t('Your Order Invoice', [], [
        'langcode' => $langcode,
      ]);
      $message['from'] = uc_store_email_from();
      $body = [
        '#theme' => 'uc_order_invoice',
        '#order' => $params['order'],
        '#op' => 'admin-mail',
      ];
      $message['body'][] = \Drupal::service('renderer')
        ->render($body);
      break;

    // Setup a custom e-mail defined by an action on a predicate.
    case 'rules-action-email':

      // Assemble an email message from the Rules settings.
      $message['headers']['Content-Type'] = 'text/html; charset=UTF-8; format=flowed';
      $message['from'] = $params['from'];

      // Perform token replacement on the subject and body.
      $token_service = \Drupal::token();
      $subject = $token_service
        ->replace($params['subject'], $params['replacements'], $langcode ? [
        'language' => $message['language'],
      ] : []);
      $body = $token_service
        ->replace($params['message'], $params['replacements'], $langcode ? [
        'language' => $message['language'],
      ] : []);

      // Strip newline characters from e-mail subjects.
      // @todo Maybe drupal_mail_send() should do this?
      $message['subject'] = str_replace([
        "\r\n",
        "\r",
        "\n",
      ], ' ', $subject);

      // Apply an input format to the message body if specified.
      if (isset($params['format'])) {
        $message['body'][] = check_markup($body, $params['format'], $langcode);
      }
      else {
        $message['body'][] = $body;
      }
      break;
  }
}