Modifying your site's email "From" header
on
October 10, 2009
Modifying your site's email "From" header
By default, all email sent from your site uses the site email address configured at
The above function retrieves the site name and email address that you configured on
admin/settings/site-information as the "from" address. On most sites this is set to something generic, like "webmaster@example.com" or "noreply@example.com". 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:
<?php
/**
* Implements hook_mail_alter().
*/
function your_module_mail_alter(&$message) {
$site_name = variable_get('site_name', '');
$site_mail = variable_get('site_mail', '');
if (!empty($site_name) && $site_mail == $message['from']) {
$message['from'] = '"'. mime_header_encode($site_name) .'" <'. $site_mail .'>';
$message['headers']['From'] = '"'. mime_header_encode($site_name) .'" <'. $site_mail .'>';
}
}
?>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".
Bevan Rudge November 21, 2009
I thought it was possible to do this by putting "Site name" <noreply@example.com> into that field. Doesn't that work?
Dale (not verified) October 13, 2009
One caveat to be aware of. hook_mail_alter() will only modify messages sent using drupal_mail(). If a module bypasses drupal_mail() the alter will never be called. For example, perhaps a developer didn't want to write a hook_mail() function and calls drupal_mail_send() directly.
This should only be a potential issue with mail sent from contrib modules.
Gregory Heller October 12, 2009
This would make a great little module, however it would be better if the issue were fixed in Drupal Core so that you could add an "name" to the site email address as was possible in previous versions of Drupal. There is an issue for it in D7.
Bart Braem (not verified) October 11, 2009
This could be a nice module!







Oh that's right. I remember trying that now. You could probably set it to such a string in the $conf array in settings.php though – right?