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

Ex: My admin e-mail is "hello@gmail.com". While sending e-mail to user, I want to change it as "Myname <(hello@gmail.com)>"

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

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

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.

| improve this answer | |
  • hook_mail_alter - Its not firing in module as well as in template.php any idea why its not happening? – kalidasan Aug 21 '15 at 4:10
  • Have you written hook_mail and called drupal_mail function. I think either is missing in your code. – Ramdas Gaikar Aug 21 '15 at 18:18
  • hook_mail() is not necessary, if the OP wants to alter emails sent from other modules. – kiamlaluno May 3 '16 at 12:47

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