Hook_mail_alter

My theme name is octave. I want to use “hook_mail_alter” to attach the invoice as a pdf with mail.
I am using “octave_mail_alter” like below in the octave.module file:-
function octave_mail_alter(array &$message) {
\Drupal::logger(‘octave’)->debug(‘Hook mail alter invoked. inserted’);
}

Why this function is not being hit?

I previously had been doing something like this.

function MYMODULE_mail_alter(&$message) {
  if($message['key] == 'EMAIL_KEY'){
    if($message['params']['user']->SOME_FIELD == 'SOME_VALUE'){
      // prevent sending of the Email.
      $message['send'] = FALSE;
    }
  }
}

Now I am doing this.

use Drupal\symfony_mailer\EmailInterface;

/**
 * Acts on an email message after rendering.
 *
 * The email is now ready to send and any headers can be altered.
 *
 * @param \Drupal\symfony_mailer\EmailInterface $email
 *   The email.
 */
function MYMODULE_mailer_post_render(EmailInterface $email) {
  if($email->getSubType() == 'EMAIL_KEY'){
    if($email->getParam('user')->get('SOME_FIELD')->value == 'SOME_VALUE'){
      // prevent sending of the Email.
      $email->setTransportDsn('null://null');
    }
  }
}
2 Likes