2

I use MailManager to send emails to end users for notifications. I would like to override the email address shown to the end user, using a "fake" address shown and not the real mail account used to send the email. I use the following code, however, in my gmail receiving account I don't see the [email protected] but the real email account used.

$params['message'] = $message;
$params['subject'] = $title;
$key = 'send_email';
$langcode = \Drupal::currentUser()->getPreferredLangcode();
$send = true;
$result = $mailManager->mail($module, $key, $targetEmail, $langcode,
                    $params, '[email protected]', $send);

Is there a way to define a custom address to be shown to the end user? Am I missing something?

1 Answer 1

1

You can use hook_mail_alter to customize the From:

MODULE_NAME_mail_alter($key, &$message, $params) {

  switch ($key) {
    case 'my_email_key':
      $message['headers']['From'] = 'My Email Name <[email protected]>';
      break;
  }
}
2
  • That should be MODULE_NAME_mail_alter(
    – Jonathan
    Commented Jul 30, 2019 at 15:12
  • Thanks @Jonathan - I fixed it in my post.
    – codesmith
    Commented Aug 13, 2019 at 18:08

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.