Send mail to users including receiver's username
Question
I have searched settings of Contact (in Drupal core), Simple news and Mass contact modules but haven't found a way to include receiver's username in the mail body.
Any suggestions how to do this?
Solution
There isn't a setting for that, but when a user sends an email to another user, using the contact form of the other user's account, the Contact module sends a message that start with (I sent an email to myself from Drupal.org):
kiamlaluno,
kiamlaluno (http://drupal.org/user/55077) has sent you a message via your contact form (http://drupal.org/user/55077/contact) at drupal.org.
If you don't want to receive such e-mails, you can change your settings at http://drupal.org/user/55077.
The first line contain the receiver's username.
The following snippet is part of the code present in contact_mail(), the function that alter some of the messages sent from Drupal.
case 'user_mail':
case 'user_copy':
$user = $params['user'];
$account = $params['account'];
$message['subject'] .= '[' . variable_get('site_name', 'Drupal') . '] ' . $params['subject'];
$message['body'][] = "$account->name,";
$message['body'][] = t("!name (!name-url) has sent you a message via your contact form (!form-url) at !site.", array('!name' => $user->name, '!name-url' => url("user/$user->uid", array('absolute' => TRUE, 'language' => $language)), '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), '!site' => variable_get('site_name', 'Drupal')), $language->language);
$message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !url.", array('!url' => url("user/$account->uid", array('absolute' => TRUE, 'language' => $language))), $language->language);
$message['body'][] = t('Message:', NULL, $language->language);
$message['body'][] = $params['message'];
break;
Other modules can change the content for the sent emails, if they implement hook_mail_alter(). This means that other modules could remove content from the email sent via the contact form; it also means that another module could replace the username used in the first line with the first and last name provided by the receivers in their user profile.