You are here

protected function EmailOrderInvoice::doExecute in Ubercart 8.4

Emails an invoice.

Order token replacements may be used in the 'Sender', 'Subject' and 'Addresses' fields.

Parameters

\Drupal\uc_order\OrderInterface $order: The order object.

string $from: Sender's e-mail address.

string[] $addresses: Recipients' e-mail addresses.

string $subject: E-mail subject.

string $template: Template name to use to format the invoice.

string $view: Which view of the invoice - one of 'admin' or 'customer'.

File

uc_order/src/Plugin/RulesAction/EmailOrderInvoice.php, line 66

Class

EmailOrderInvoice
Provides a 'Email order invoice' action.

Namespace

Drupal\uc_order\Plugin\RulesAction

Code

protected function doExecute(OrderInterface $order, $from, array $addresses, $subject, $template, $view) {
  $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 = $this->token
    ->replace($settings['from'], $settings['replacements']);
  if (empty($from)) {
    $from = uc_store_email_from();
  }

  // Split up our recipient e-mail addresses so we can send a
  // separate e-mail to each.
  $recipients = [];
  foreach ($addresses as $address) {
    $address = trim($address);

    // Remove blank lines.
    if (!empty($address)) {

      // Apply token replacements to the 'recipient' e-mail address.
      $recipients[] = $this->token
        ->replace($address, $settings['replacements']);
    }
  }
  $message = [
    '#theme' => 'uc_order_invoice',
    '#order' => $order,
    '#op' => $settings['view'],
    '#template' => $settings['template'],
  ];
  $settings['message'] = \Drupal::service('renderer')
    ->renderPlain($message);

  // Use uc_order's hook_mail() to send a separate e-mail to each recipient.
  foreach ($recipients as $to) {
    $sent = $this->mailManager
      ->mail('uc_order', 'rules-action-email', $to, uc_store_mail_recipient_langcode($to), $settings, $from);
    if (!$sent['result']) {
      $this->logger
        ->get('uc_order')
        ->error('Attempt to e-mail invoice for order @order_id to @email failed.', [
        '@email' => $to,
        '@order_id' => $order
          ->id(),
      ]);
    }
  }
}