How to Dynamically Set the Recipient (To) Email Address in Contact Form 7

Allowing the recipient to be chosen dynamically at page load with Contact Form 7 Dynamic Text Extension adds a lot of flexibility to an already awesome plugin.

Written by

Chris Mavricos

Published on

BlogCode · How To · WordPress Plugins · WordPress Tutorials

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:


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.

  1. Install Contact Form 7 and Contact Form 7 – Dynamic Text Extension
  2. Navigate to Contact > Edit
  3. Create or Edit a contact form
  4. Click Generate Tag and select Dynamic Text Field
  5. Set the Name to recipient-email
  6. Set the Dynamic Value to CF7_get_custom_field key='user_email'
  7. 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:

  1. Copy the tag into the form. It’ll look like this: [dynamictext recipient-email uneditable "CF7_get_custom_field key='user_email'"]
  2. Copy the name tag [recipient-email] into the To: input.
  3. Save the form

The form is now set up to do what we want. The rest involves writing your posts.

  1. Create a post.
  2. Copy the CF7 Contact Form shortcode into the post. [ contact-form-7 id="963" title="Contact form 1" ]
  3. Set the custom field user_email to any email address.
  4. Your contact form will be sent to that email.
  5. 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!

Chris Mavricos

Hi, I'm Chris. I'm a web developer and founder of SevenSpark located in Boulder, CO. I've been developing websites since 2004, and have extensive experience in WordPress theme, plugin, and custom development over the last 15+ years.