I'm altering different emails from my hook_mail_alter(&$message) function. But for some reason altering the email sent by commerce invoice module is not working. I'm trying to do it like:

    case 'invoice_confirmation':
      $message['subject'] = 'Some other title';

I found the code that sends that email in invoice module:

  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);


and there I saw that id should be 'invoice_confirmation', but like my hook function is never reached when this email is sent?! How to alter this email title?

1 Answer 1


Solved the issue - id is actually 'commerce_invoice_confirmation'.

I was trying to print out $message['id'] and stop executing of code, but that never happened? So I assumed that my code was never called. Not sure why. So I used watchdog logger to log id value and found it out.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.