InvoiceConfirmationMail.php in Commerce Invoice 8.2
File
src/Mail/InvoiceConfirmationMail.php
View source
<?php
namespace Drupal\commerce_invoice\Mail;
use Drupal\commerce\MailHandlerInterface;
use Drupal\commerce_invoice\Entity\InvoiceInterface;
use Drupal\commerce_invoice\InvoiceFileManagerInterface;
use Drupal\commerce_invoice\InvoiceTotalSummaryInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
class InvoiceConfirmationMail implements InvoiceConfirmationMailInterface {
use StringTranslationTrait;
protected $mailHandler;
protected $invoiceTotalSummary;
protected $profileViewBuilder;
protected $invoiceFileManager;
public function __construct(EntityTypeManagerInterface $entity_type_manager, MailHandlerInterface $mail_handler, InvoiceTotalSummaryInterface $invoice_total_summary, InvoiceFileManagerInterface $invoice_file_manager) {
$this->mailHandler = $mail_handler;
$this->invoiceTotalSummary = $invoice_total_summary;
$this->profileViewBuilder = $entity_type_manager
->getViewBuilder('profile');
$this->invoiceFileManager = $invoice_file_manager;
}
public function send(InvoiceInterface $invoice, $to = NULL, $bcc = NULL) {
$to = isset($to) ? $to : $invoice
->getEmail();
if (!$to) {
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);
}
}