How to change sender email Id using hook_mail_alter How to change the sender email id for some emails, using hook_mail_alter. The default SMTP configured email ID is 'test@myfirstdomain.com'
The subject gets altered correctly using the hook_mail_alter, however, the email which I receive is still from the default configured email 'test@myfirstdomain.com'.
function test_mail_alter(&amp;$message) {
 switch ($message['key']) {
  case 'activation':
    
    $from = &quot;test@myseconddomain.com&quot; ;
    $message['from'] = $from;
    $message['headers']['From'] = $from;
    $message['headers']['Sender'] = $from;
    $message['headers']['Return-Path'] = $from;
    $message['subject'] = 123;
    break;
}

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

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