0

While sending registration success e-mail in Drupal system, I want to update "from" element.

Ex: My admin e-mail is "[email protected]". While sending e-mail to user, I want to change it as "Myname <([email protected])>"

Can we do it through hook_mail_alter?

I have tried below code. But din't work as expected.

function mymodule_mail_alter(&$message) {
 print_r($message); // I din't get any response here
}

Want to append Name before email ID.

Edit: hook_mail_alter its not triggering neither module nor template.php. Module is enabled. I have cleared cache too.

Existing comment : https://api.drupal.org/comment/59119#comment-59119

3
  • I think you need to make sure that hook_mail_alter is firing so you could print_r($message); die;
    – sam2de
    Commented Aug 20, 2015 at 14:13
  • hook_mail_alter its not firing neither module nor template.php :(
    – Kalidasan
    Commented Aug 21, 2015 at 4:07
  • after adding your hook you have to clear cache. also make sure that module is enabled.
    – sam2de
    Commented Aug 21, 2015 at 4:10

1 Answer 1

1

We can easily change the From with hook_mail_alter using 'from' key from $message.

function mymodule_mail_alter(&$message) { 
  $message['from'] = 'Your Name ('. $message['from'] . ')'; 
}

Also, Please check hook_mail function https://api.drupal.org/api/drupal/modules%21system%21system.api.php/function/hook_mail/7 which needs to be written for your module and then you need to call drupal_mail function.

hook_mail_alter is just for modifying the variables passed to drupal_mail.

3
  • hook_mail_alter - Its not firing in module as well as in template.php any idea why its not happening?
    – Kalidasan
    Commented Aug 21, 2015 at 4:10
  • Have you written hook_mail and called drupal_mail function. I think either is missing in your code. Commented Aug 21, 2015 at 18:18
  • hook_mail() is not necessary, if the OP wants to alter emails sent from other modules.
    – avpaderno
    Commented May 3, 2016 at 12:47

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