You are here

protected function SendOrderEmail::doExecute in Ubercart 8.4

Same name in this branch
  1. 8.4 uc_order/src/Plugin/RulesAction/SendOrderEmail.php \Drupal\uc_order\Plugin\RulesAction\SendOrderEmail::doExecute()
  2. 8.4 uc_file/src/Plugin/RulesAction/SendOrderEmail.php \Drupal\uc_file\Plugin\RulesAction\SendOrderEmail::doExecute()
  3. 8.4 uc_role/src/Plugin/RulesAction/SendOrderEmail.php \Drupal\uc_role\Plugin\RulesAction\SendOrderEmail::doExecute()

Sends an email concerning an order.

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

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 $message: E-mail body.

string $format: Format filter machine name.

File

uc_role/src/Plugin/RulesAction/SendOrderEmail.php, line 65

Class

SendOrderEmail
Provides a 'Send order email' action.

Namespace

Drupal\uc_role\Plugin\RulesAction

Code

protected function doExecute(OrderInterface $order, $from, array $addresses, $subject, $message, $format) {
  $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 = $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']);
    }
  }

  // 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_role')
        ->error('Attempt to e-mail @email concerning order @order_id failed.', [
        '@email' => $to,
        '@order_id' => $order
          ->id(),
      ]);
    }
  }
}