Drupal: How to add another recipient in personal user contact form

Drupal's core contact module comes with the feature that allows users to have their own personal contact forms.
We were asked to provide a way so that User 1 would receive a copy of any message exchanged between users via the forms.

Our first thought was to use Rules, but it seemed that creating an event for contact form submission was the long way to go.

The solution was quite simple: Using hook_mail_alter.

So in a custom module, we included this function:

<?php
// $Id: ccadmin.module,v 1.1.2.4.2.12.2.6.2.2 2011/06/03 03:07:53 Konordo $

function ccadmin_mail_alter(&$message) {
$admin = user_load(1);
if (
$message['id'] == 'contact_user_mail'
 
|| $message['id'] == 'anonymous_contact_user_mail') {
   
$message['to'] = $message['to'].' ,'.$admin->mail;
  }
}
?>

What this does, is to override the drupal_mail function called by core's contact module. In our case, the Anonymous Contact Form module was used as well, so we are overriding this one as well.

After loading User 1, we are overriding the $message['to'] variable, by adding the User 1's email. The form is valid: email1, email2

And that's it! User 1 will be copied in all mail messages. :)

Please note that we have the flexibility of adding a different email, or even more than one. We just to follow the formatting of $to parameter of drupal_mail.
Some examples (as seen in drupal_mail page) are:

user@example.com
user@example.com, anotheruser@example.com
User < user@example.com >
User < user@example.com >, Another User < anotheruser@example.com >
Tags: 
Drupal 6, Drupal Quick Tips

Recent Comments

Forward by Dries Buytaert, Founder and Project Lead, Drupal; CTO Acquia.

Order now from:

Managing Expectations Podcast with DrupalEasy

Author Interview http://tinyurl.com/8a8nxno

We provide the most Shiny Drupal Bits & Pixels

Since Drupal 4.x