Modifying your site’s email “From” header

By default, all email sent from your site uses the site email address configured at admin/settings/site-information as the “from” address. On most sites this is set to something generic, like “[email protected]” or “[email protected]”. However in a lot of mail clients, when the email reaches your inbox, it appears as being just from “webmaster” or “noreply”. Wouldn’t it be better if it appeared as being from “Your site” or whatever you have set your site name to be?

You can achieve this easily with just a few lines of code in a custom module:

/**
* Implements hook_mail_alter().
*/
function your_module_mail_alter(&$message)
}
?>

The above function retrieves the site name and email address that you configured on admin/settings/site-information. Then for each email sent from the site, if the “from” email address matches the site address, it modifies the From header to include the site name as well. This produces the nice user-friendly effect where emails in your inbox appear to be from “Your site” rather than just “webmaster”.

2017-03-31T06:20:18+00:00 Categories: Development, Drupal|

About the Author: