You are here

function uc_order_action_email in Ubercart 8.4

Same name and namespace in other branches
  1. 6.2 uc_order/uc_order.ca.inc \uc_order_action_email()
  2. 7.3 uc_order/uc_order.rules.inc \uc_order_action_email()

Sends an email concerning an order.

The 'Sender', 'Recipients', 'Subject', and 'Message' fields accept order token replacements.

See also

uc_order_action_email_form()

1 string reference to 'uc_order_action_email'
uc_order_rules_action_info in uc_order/uc_order.rules.inc
Implements hook_rules_action_info().

File

uc_order/uc_order.rules.inc, line 431
Rules integration for order-related entity events, conditions, and actions.

Code

function uc_order_action_email($order, $from, $addresses, $subject, $message, $format) {
  $token_service = \Drupal::token();
  $settings = [
    'from' => $from,
    'addresses' => $addresses,
    'subject' => $subject,
    'message' => $message,
    'format' => $format,
  ];

  // Token replacements for the subject and body.
  $settings['replacements'] = [
    'uc_order' => $order,
  ];

  // Apply token replacements to the 'from' e-mail address.
  $from = $token_service
    ->replace($settings['from'], $settings['replacements']);
  if (empty($from)) {
    $from = uc_store_email_from();
  }

  // Apply token replacements to 'recipient' e-mail addresses.
  $addresses = $token_service
    ->replace($settings['addresses'], $settings['replacements']);

  // Split up our recipient e-mail addresses.
  $recipients = [];
  foreach (explode("\n", $addresses) as $address) {
    $address = trim($address);

    // Remove blank lines.
    if (!empty($address)) {
      $recipients[] = $address;
    }
  }
  foreach ($recipients as $email) {
    $sent = \Drupal::service('plugin.manager.mail')
      ->mail('uc_order', 'rules-action-email', $email, uc_store_mail_recipient_langcode($email), $settings, $from);
    if (!$sent['result']) {
      \Drupal::logger('uc_order')
        ->error('Attempt to e-mail @email concerning order @order_id failed.', [
        '@email' => $email,
        '@order_id' => $order
          ->id(),
      ]);
    }
  }
}