One question I get a lot is how to dynamically change the email recipient in Contact Form 7. It’s actually very easy, and with Contact Form 7 – Dynamic Text Extension (free download), you get a lot of flexibility.
The Basics
First, let’s start off with the basic mechanism: Create a tag that will contain the email address, and set that tag’s name as the email recipient. The simplest dynamic email address can be set with a basic text input. The contact form 7 tag would look like this:
[ text* recipient-email ]
Then, all we do is place the name tag in the Mail To: field, as such:
[recipient-email]
We end up with this:
Okay, that was easy. But this isn’t a very good idea. We don’t want just anyone to be able to change who the form is being sent to and then send out emails from our site.
Setting the Email Address Dynamically
Using CF7 DTX, we can dynamically set the email address for the form. The email address might be stored in a POST variable, for example, and we can retrieve it using the CF7_POST
shortcode. Here we’ll walk through an example of setting the email address via a custom field (post meta data). This means the email address for the form will be determined by the post, so we can have a single CF7 contact form that is displayed on every post, each potentially aimed at a different recipient.
- Install Contact Form 7 and Contact Form 7 – Dynamic Text Extension
- Navigate to Contact > Edit
- Create or Edit a contact form
- Click Generate Tag and select Dynamic Text Field
- Set the Name to
recipient-email
- Set the Dynamic Value to
CF7_get_custom_field key='user_email'
- Click Make this field Uneditable – we don’t want users to change the email address!
At this point your Tag Generator should look like this:
- Copy the tag into the form. It’ll look like this:
[dynamictext recipient-email uneditable "CF7_get_custom_field key='user_email'"]
- Copy the name tag
[recipient-email]
into the To: input. - Save the form
The form is now set up to do what we want. The rest involves writing your posts.
- Create a post.
- Copy the CF7 Contact Form shortcode into the post.
[ contact-form-7 id="963" title="Contact form 1" ]
- Set the custom field user_email to any email address.
- Your contact form will be sent to that email.
- Repeat the same process with the same form shortcode on a separate post with a new email address and the email will go to that address
Hiding the Email Address
That’s great, but we don’t really need to display the email to the user. In fact, we’d really rather not. Instead of a text field, we really just want a hidden input field. Luckily, that’s easy to do – just substitute a Dynamic Hidden Field for the Dynamic Text Field instead! The code simply changes like this:
[dynamichidden recipient-email "CF7_get_custom_field key='user_email'"]
Great! But one last thing: spam bots are going to grab this email address and spam the hell out of you. Damn. Let’s at least obfuscate the email a little to make it harder for them. It’s built in to the CF7_get_custom_field
shortcode, so luckily it’s really easy. Just do this:
[dynamichidden recipient-email "CF7_get_custom_field key='user_email' obfuscate='on'"]
Awesome!! Now we’re good to go!