I'm writing a custom module to format the email I receive from a user who fills out a contact form. I've created two contact forms and I want a separate email format for each form.


I'm using this code as the basis of my module

But, I'm having trouble targeting a particular form. This is the code I'm trying to use, but it doesn't seem to be working:

    // further restrict mail_alter to machine name of a particular contact form
    if ($message['params']['contact_form']->id() == 
    'contact_message_contact_technical_support_form') {
    ...code to format message... (from linked article)
elseif ($message['params']['contact_form']->id() == 
'contact_message_feedback_form_form') {
    ...code to format message... (from linked article)


Would someone please tell me how to properly id my forms so I can target them?

  • The example that you posted is the form ID, that's most definitely wrong. Should be just contact_technical_support, you can always just display/log the ID and then use that.
    – Berdir
    Commented Mar 12, 2017 at 22:17
  • 1
    For clarity, can you post the exact code that you have. As well as the machine name of your form. As well as a screenshot of var_dump($message); Commented Mar 13, 2017 at 2:03
  • By using separate functions to sent mail from each contact form using hook_form_alter()
    – developer
    Commented Mar 13, 2017 at 10:11

1 Answer 1


Thanks for your help everyone. The comments were helpful. I ended up using the var_dump($message) as suggested and then whittled that down to what I needed. As one of you suggested the form name wasn't as long as I originally thought (and it WAS NOT the ID for the form). I think one ended up being 'feedback' and the other was, as you suggested 'contact_technical_support' or something like that.

The other issue I was having was that I was ignorantly naming my module with the word 'alter' in it which was overriding, or being overridden, by the alter hook it was using, I think.

At any rate, short answer is, my code basically works, but use var_dump($message) to find the correct ID of the forms and don't use 'alter' in the module name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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