1

How to change the sender email id for some emails, using hook_mail_alter. The default SMTP configured email ID is '[email protected]'

The subject gets altered correctly using the hook_mail_alter, however, the email which I receive is still from the default configured email '[email protected]'.

function test_mail_alter(&$message) {
 switch ($message['key']) {
  case 'activation':
    
    $from = "[email protected]" ;
    $message['from'] = $from;
    $message['headers']['From'] = $from;
    $message['headers']['Sender'] = $from;
    $message['headers']['Return-Path'] = $from;
    $message['subject'] = 123;
    break;
}

}

1 Answer 1

-4

According to the documentation:

/**
 * Alter an email message created with MailManagerInterface->mail().
 *
 * Hook hook_mail_alter() allows modification of email messages created and sent
 * with MailManagerInterface->mail(). Usage examples include adding and/or
 * changing message text, message fields, and message headers.
 *
 * Email messages sent using functions other than MailManagerInterface->mail()
 * will not invoke hook_mail_alter(). For example, a contributed module directly
 * calling the MailInterface->mail() or PHP mail() function will not invoke
 * this hook. All core modules use MailManagerInterface->mail() for messaging,
 * it is best practice but not mandatory in contributed modules.
...

Can we check if the mail is sent MailManagerInterface->mail() like this and not any other way.

1
  • The subject gets altered and that confirms the hook is invoked Mar 1, 2021 at 2:03

Your Answer

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

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