You are here

public function ShipmentConfirmationMail::send in Commerce Shipping 8.2

Sends the shipment confirmation email.

Parameters

\Drupal\commerce_shipping\Entity\ShipmentInterface $shipment: The shipment.

string $to: The address the email will be sent to. Must comply with RFC 2822. Defaults to the order email.

string $bcc: The BCC address or addresses (separated by a comma).

Return value

bool TRUE if the email was sent successfully, FALSE otherwise.

Overrides ShipmentConfirmationMailInterface::send

File

src/Mail/ShipmentConfirmationMail.php, line 44

Class

ShipmentConfirmationMail

Namespace

Drupal\commerce_shipping\Mail

Code

public function send(ShipmentInterface $shipment, $to = NULL, $bcc = NULL) {

  /** @var \Drupal\commerce_order\Entity\OrderInterface $order */
  $order = $shipment
    ->getOrder();
  $to = isset($to) ? $to : $order
    ->getEmail();
  if (!$to) {

    // The email should not be empty.
    return FALSE;
  }
  $subject = $this
    ->formatPlural($shipment
    ->get('items')
    ->count(), 'An item for order #@number shipped!', 'Items for your order #@number shipped!', [
    '@number' => $order
      ->getOrderNumber(),
  ]);
  $profile_view_builder = $this->entityTypeManager
    ->getViewBuilder('profile');
  $shipment_view_builder = $this->entityTypeManager
    ->getViewBuilder('commerce_shipment');
  $body = [
    '#theme' => 'commerce_shipment_confirmation',
    '#order_entity' => $order,
    '#shipment_entity' => $shipment,
    '#shipping_profile' => $profile_view_builder
      ->view($shipment
      ->getShippingProfile()),
    '#tracking_code' => $shipment_view_builder
      ->viewField($shipment
      ->get('tracking_code'), 'default'),
  ];
  $params = [
    'id' => 'shipment_confirmation',
    'from' => $order
      ->getStore()
      ->getEmail(),
    'bcc' => $bcc,
    'order' => $order,
    'shipment' => $shipment,
  ];
  $customer = $order
    ->getCustomer();
  if ($customer
    ->isAuthenticated()) {
    $params['langcode'] = $customer
      ->getPreferredLangcode();
  }
  return $this->mailHandler
    ->sendMail($to, $subject, $body, $params);
}