Snabow
Snabow

Reputation: 331

Modify sender address in mail plugin

I'm using the notify_entity module for Drupal 8 and want to change the mail address used for the from value.
I'm trying to do it using hook_mail_alt r but it doesn't work, Drupal still send mail with the default administrator mail address... Am I doing something wrong? Or there is another way to do this? Thanks.

/**
 * Implements hook_mail_alter()
 */
function notify_entity_mail_alter(&$message){
  $from = "[email protected]";
  $message['from'] = $from;
}

Upvotes: 0

Views: 923

Answers (1)

GiorgosK
GiorgosK

Reputation: 8299

They should be changed like this

  $message['headers']['Return-Path'] = '[email protected]';
  $message['headers']['Sender'] = '[email protected]';
  $message['headers']['From'] = 'Site name';
  $message['headers']['Reply-to'] = '[email protected]';  

Also note it seems you are modifying (hacking) the notify_entity module right ? You should not do that! If you update it or someone else updates this drupal installation in the future you might end up losing the changes and not realize it ...

You should create your own module and implement the hook_mail_alter and name your function MYMODULE_mail_alter()

Just today I had to do this and stumbled upon your question here it is a very simple module that does exactly what you want https://github.com/GiorgosK/mail_alter_headers. NOTE: you have to modify the .module file with your own details or comment out // the ones you don't want to modify.

Upvotes: 1

Related Questions