You are here

public function InvoiceConfirmationMail::send in Commerce Invoice 8.2

Sends the invoice confirmation email.

Parameters

\Drupal\commerce_invoice\Entity\InvoiceInterface $invoice: The invoice.

string $to: The address the email will be sent to. Must comply with RFC 2822. Defaults to the invoice 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 InvoiceConfirmationMailInterface::send

File

src/Mail/InvoiceConfirmationMail.php, line 66

Class

InvoiceConfirmationMail

Namespace

Drupal\commerce_invoice\Mail

Code

public function send(InvoiceInterface $invoice, $to = NULL, $bcc = NULL) {
  $to = isset($to) ? $to : $invoice
    ->getEmail();
  if (!$to) {

    // The email should not be empty.
    return FALSE;
  }
  $subject = $this
    ->t('Invoice #@number', [
    '@number' => $invoice
      ->getInvoiceNumber(),
  ]);
  $body = [
    '#theme' => 'commerce_invoice_confirmation',
    '#invoice_entity' => $invoice,
    '#totals' => $this->invoiceTotalSummary
      ->buildTotals($invoice),
  ];
  if ($billing_profile = $invoice
    ->getBillingProfile()) {
    $body['#billing_information'] = $this->profileViewBuilder
      ->view($billing_profile);
  }
  $params = [
    'id' => 'invoice_confirmation',
    'from' => $invoice
      ->getStore()
      ->getEmail(),
    'bcc' => $bcc,
    'invoice' => $invoice,
  ];
  $customer = $invoice
    ->getCustomer();
  if ($customer
    ->isAuthenticated()) {
    $params['langcode'] = $customer
      ->getPreferredLangcode();
  }
  $file = $this->invoiceFileManager
    ->getInvoiceFile($invoice);
  $attachment = [
    'filepath' => $file
      ->getFileUri(),
    'filename' => $file
      ->getFilename(),
    'filemime' => $file
      ->getMimeType(),
  ];
  $params['attachments'][] = $attachment;
  return $this->mailHandler
    ->sendMail($to, $subject, $body, $params);
}