1

On the personal contact form, when someone cc themselves, the username of the user they are emailing is exposed. Many users actually type their full name as username, so for them to remain anonymous, I don't want username to be exposed to others.

1

You can customize the email with hook_mail_alter(&$message).

1

The email that is send in those cases is set by contact_mail(), which is hook_mail() implementation done from the Contact module, and that contains the following code:

  switch ($key) {
    case 'page_mail':
    case 'page_copy':
      $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
      $message['body'][] = t("!sender-name (!sender-url) sent a message using the contact form at !form-url.", $variables, array('langcode' => $language->language));
      $message['body'][] = $params['message'];
      break;

    case 'page_autoreply':
      $message['subject'] .= t('[!category] !subject', $variables, array('langcode' => $language->language));
      $message['body'][] = $params['category']['reply'];
      break;

    case 'user_mail':
    case 'user_copy':
      $variables += array(
        '!recipient-name' => format_username($params['recipient']), 
        '!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
      );
      $message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->language));
      $message['body'][] = t('Hello !recipient-name,', $variables, array('langcode' => $language->language));
      $message['body'][] = t("!sender-name (!sender-url) has sent you a message via your contact form (!form-url) at !site-name.", $variables, array('langcode' => $language->language));
      $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !recipient-edit-url.", $variables, array('langcode' => $language->language));
      $message['body'][] = t('Message:', array(), array('langcode' => $language->language));
      $message['body'][] = $params['message'];
      break;
  }

As visible in contact_personal_form_submit(), the message identifier for the email sent when somebody contacts another user is "user_mail."

Implementing hook_mail_alter() using something similar to the following code, you should achieve the task you want.

function mymodule_mail_alter(&$message) {
  switch ($message['key']) {
    case 'user_mail':
    case 'user_copy':
      $language = $message['language'];
      $message['body'] = array();
      $params = $message['params'];
      $variables = array(
        '!site-name' => variable_get('site_name', 'Drupal'), 
        '!subject' => $params['subject'], 
        '!recipient-name' => format_username($params['recipient']), 
        '!recipient-edit-url' => url('user/' . $params['recipient']->uid . '/edit', array('absolute' => TRUE, 'language' => $language)),
        '!form-url' => url($_GET['q'], array('absolute' => TRUE, 'language' => $language)), 
        '!sender-name' => format_username($params['sender']), 
        '!sender-url' => $params['sender']->uid ? url('user/' . $params['sender']->uid, array('absolute' => TRUE, 'language' => $language)) : $params['sender']->mail,
        '!recipient-name' => format_username($params['recipient']), 
      );

      $message['subject'] .= t('[!site-name] !subject', $variables, array('langcode' => $language->language));
      $message['body'][] = t('Hello !recipient-name,', $variables, array('langcode' => $language->language));
      $message['body'][] = t("A user (!sender-url) has sent you a message via your contact form (!form-url) at !site-name.", $variables, array('langcode' => $language->language));
      $message['body'][] = t("If you don't want to receive such e-mails, you can change your settings at !recipient-edit-url.", $variables, array('langcode' => $language->language));
      $message['body'][] = t('Message:', array(), array('langcode' => $language->language));
      $message['body'][] = $params['message'];
      break;
  }
}

I altered the message with key equal to user_reply, which is the message returned to the user who sends the message. In this way the CC returned to the sender is the same message sent to the recipient.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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