-
Jonathan Hedstrom
Lead Developer
Drupal's core contact module allows users of a site to contact one another via email. Unfortunately, it also reveals the sender's email address. Normally, this is seen as fine behavior because the only person's email address that is at stake is the one taking the action. However, for a recent site, this violated the site's stated COPPA compliance, so we needed to alter the From and Reply-To headers sent with the email. As it turns out, this is quite an easy thing to do thanks to hook_mail_alter().
/** * Implementation of hook_mail_alter(). */ function somemodulename_mail_alter(&$message) { if ($message['id'] == 'contact_user_mail') { // Set 'From' address to a no-reply rather than leaking student's email address. $mail = 'NO-REPLY@example.com'; $message['from'] = $mail; foreach (array('Reply-To', 'From') as $header) { $message['headers'][$header] = $mail; } } }
variable_get
Instead of hard coding $mail, use
$mail = variable_get("site_mail", ini_get("sendmail_from"));
Thanks toemaz, Yes, that
Thanks toemaz,
Yes, that would be better. I kept the code above simple to convey the point. Ideally, this would be configurable, either on a per-site basis, or even better, on a per-user basis (a user could choose to reveal their own email. or use a no-reply placeholder).
You might also want to add a
You might also want to add a link to the sender's contact page in the body of the email so that the recipient can somehow reply.
Changing the contact page itself
This works absolutely perfectly! Thank you for the post. I'm trying to find where I would edit the actual contact page in Drupal to reflect the changes made through the hook now. On the contact page it still shows "From: username ". Do you know where I could change this so that it shows "From: username " so that I don't have to explain that while it shows their personal email here it won't show to the receiving user?
You should be able to change
You should be able to change this section of the contact form via an implementation of hook_form_alter.
CC
Does this affect the "Send yourself a copy" checkbox? I'm getting the following error when that box is checked and a user tries to send an email: The following From address failed: (the user's email address) The following From address failed: (the user's email address again)
Any way that this could be causing that?
How to use the user's email field instead?
this is a good way to modify the contact form, but how do we use the user's email address from the contact form itself? not all users submitting the contact form are registered users of the site, so it is difficult to get email addresses of anonymous users who use the contact form. (too many "use"'s, sorry)
-gnat
BCC Email
Hi,
I think this is great, thanks for such great information. Is there a way of sending a bcc from the user contact form? thanks I would really appreciate your help.
Add your comment