On my Drupal 7 site, I have a user contact form. How can I alter the form so that an admin is notified when the form is submitted?

  • use webform for any customization – monymirza Mar 20 '13 at 9:51
  • Thank you for your response minymirza. I do not have webform installed, but entityforms. Also is there a way to assign (or attach) an entityform to each user2 profile, so it emails the owner of the profile and the admin? I asked this before here on stackexchange without luck, so I finally eneded up sticking to the sitewaide user contact form. I'm pretty stuck with this issue since the website owner wants to be notified when a user is contacted from his website... – cara Mar 20 '13 at 9:54

In a custom module

hook_form_alter() yourForm.

Add custom submit handler, it will run alongside with your defaut submit

$form['#submit'][] = 'yourSubmitFunction';

Inside your custom submit handler use drupal_mail() or any other result you need

function yourSubmitFunction($form, &$form_state) {

Note: the form_state array contains all the submited values.

| improve this answer | |
  • This is how I would do it :-) +1 – AjitS Mar 20 '13 at 11:22
  • @Pan I have implemented hook_alter_form like this: function request_info_form_alter(&$form, &$form_state, $form_id){ if($form_id == "contact_personal_form"){ $form['#submit'][] = 'request_information'; } } function request_information($form, &$form_state) { global $user, $language; $site_email = variable_get('site_mail', ''); $to = $site_email; drupal_mail('contact', 'page_mail', $to, language_default(), $values, $from); } ---and it's sending the email to the admin, but not the message, I thought the form state array contained all the submitted values...A tip?... – cara Mar 23 '13 at 15:15
  • $values should be something like $form_state['values']['element_that_contains_message'] ... – Pan Chrono Mar 23 '13 at 16:21
  • @Pan Yep! Was missing $values = $form_state['values']; $values['sender'] = $user; $values['sender']->name = $values['name']; $values['sender']->mail = $values['mail']; $values['subject'] = $form_state['values']['subject']; $values['message'] = $form_state['values']['message']; $from = $values['name']; Now works as magic! Thanks! – cara Mar 23 '13 at 23:49

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.