Skip to content

Contact form that goes to different recipients based on selection

If you’re not the only person involved, in managing your web presence, you might want to have email from your contact form go to different destinations depending on a selection on the form. For instance:

screenshot of contact form showing "what is this regarding" selection field

When a visitor submits the form, the email goes to a different person depending which entry they selected.

General Notes

For any contact form plugin to be able to send mail without it being blocked by increasingly suspicious spam blocking software on the receiving end, you need to set up WP Mail SMTP or a similar plugin to provide the outgoing email with a stamp of authenticity.

Also, the instant any form exists on your site and can be found via links from elsewhere on the site, busy little bots will be using it to spam you. So either set this form up on a private page initially, or set up a spam blocker now. I like hCAPTCHA, or if using Contact Form 7, you can add a quiz question that bots won’t be able to answer correctly.

In Contact Form 7 plugin

Contact Form 7 is the most popular plugin for designing contact forms. If this is the plugin you use, define the “What is this regarding” field with selections of the form “text of selection|email address” — use the vertical bar character between the two parts, and if you have multiple addressees, use “,” as a delimiter (no spaces). So the definition of the form would include something like this:

<label> What is this regarding?
   [select* recipient include_blank default:get
     "Website problem|webmaster@yourdomain.com"
     "Volunteering|mabel@gmail.com,fred@gmail.com"
     "Press inquiry|pr@yourdomain.com"
     "Other|kate-wallaby@yahoo.com"
  ]
</label>

NOTE: this would work the same if it were all on one line.

The above code creates the selection field shown at the top of this article, and establishes who is responsible for each area. I’ve chosen to name the field “recipient” (that’s in the above text also). To arrange for that selection to drive who receives the email, use the Mail tab of the contact form editing screen, and enter that field name in square brackets in the To field.

screenshot of Mail tab og contact form editor

OTHER NOTE: In some cases, the recipient may need to know which selection the sender made in this field. This can happen when, for instance, the same person is currently the recipient for multiple areas. One way to do this is to use the email address syntax that lets you specify a name as well as email address, e.g.:

"Website problem|Website problem <webmaster@yourdomain.com>"

Now you can refer to the recipient field in the email body. The part to the right of the “|” will be included, which will include the recipient’s email address but also the text you chose to add.

Default recipient specified by link

The “default:get” in the field definition markup says the form should look for a query argument in the URL to fill in a default value for the field. This is helpful as it lets you create links from elsewhere on your site that are to contact a specific recipient. The syntax for the query argument is fieldname=value, so in this example it might be:

https://mydomain.com/contact/?recipient=Website+problem

The value must match the selection text exactly and must be URL-encoded (with space changed to “+”, for instance). The “default:get” syntax can also be used to allow pre-population of other fields, e.g. subject line. The person filling in the form can still change these values — they’re only defaults.

In WPForms Lite

The WPForms plugin has a help document explaining how to allow users to select recipient in WPForms. This document doesn’t make clear that this solution requires the paid version of WPForms — not the free WPForms Lite. Also, it’s a pain to manage the mapping of values to addresses — it’s nothing like the simple list shown above. You must create a separate notification for each addressee, and if the value you test for to decide whether to use that notification doesn’t exactly match the text of the selection, no notification gets sent. It’s far too easy for someone editing the form to break this system.

There’s no way to do this in the free version that doesn’t expose the email addresses in the HTML of the contact form.

Share...

Leave a Reply

Your email address will not be published. Required fields are marked *