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 noreply@mymailserver.com 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, 'noreply@mymailserver.com', $send);

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

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 <my_email@address.com>';
      break;
  }
}
  • That should be MODULE_NAME_mail_alter( – Jonathan Jul 30 at 15:12
  • Thanks @Jonathan - I fixed it in my post. – codesmith Aug 13 at 18:08

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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