You are here

function uc_order_action_email_invoice in Ubercart 8.4

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

Emails an invoice.

The 'Sender', 'Subject' and 'Addresses' fields take order token replacements.

1 call to uc_order_action_email_invoice()
uc_cart_uc_checkout_complete in uc_cart/uc_cart.module
Implements hook_uc_checkout_complete().
1 string reference to 'uc_order_action_email_invoice'
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 494
Rules integration for order-related entity events, conditions, and actions.

Code

function uc_order_action_email_invoice($order, $from, $addresses, $subject, $template, $view) {
  $token_service = \Drupal::token();
  $settings = [
    'from' => $from,
    'addresses' => $addresses,
    'subject' => $subject,
    'template' => $template,
    'view' => $view,
  ];

  // Token replacements for the from, 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;
    }
  }
  $message = [
    '#theme' => 'uc_order_invoice',
    '#order' => $order,
    '#op' => $settings['view'],
    '#template' => $settings['template'],
  ];
  $settings['message'] = \Drupal::service('renderer')
    ->renderPlain($message);
  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 invoice for order @order_id to @email failed.', [
        '@email' => $email,
        '@order_id' => $order
          ->id(),
      ]);
    }
  }
}