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

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!

25 thoughts on “How to Dynamically Set the Recipient (To) Email Address in Contact Form 7

  1. Hi,
    Thanks for the plugin.
    I was trying to see if I can have a global varaiable (room_discount) as a default value for a field on the Contact Form.

    [dynamictext room-disc uneditable “CF7_get_custom_field key=room_discount'”]

    What am I doing wrong?

    Thanks in advance.


    • Hi Madhu,

      First off you’re missing the opening single quote before ‘room_discount’

      Second, that CF7_get_custom_field is for post custom fields, so if you’re really trying to get a “global” variable (unrelated to the current post), you’ll need to write a new shortcode.

      Hope that helps 🙂


  2. Aha … Boy …
    With my almost zero coding skills, I will have to wait then, huh? 🙂
    Thanks again for providing the plugin.

  3. I am using CF7 to collect new project information.
    Is there a way to automatically populate the Name and Email fields with the posters name and email?

  4. thanks for usefull info want to know about how to display multiple contact form as like registration form one by one after clicking on the option countinue in one pageor post
    reply soon

  5. Thank you for this tutorial, and for the plugin. How can we make a dynamic short code to choose from a list of email addresses? For example, I have 10 departments. Each dept. has about 3 email addresses. Normally, I’d make a select-box to allow the user to choose which department. But, I was hoping I could click a link, open the form, pass a variable to the form which chooses the correct department, based on the link. Is this possible?

  6. Chris,
    First, this is an amazing plug-in. I’m trying to use it in a Buddypress installation and I could use a little coaching if you have a second.

    We have members, and I want to be able to grab the email address from custom field we’ve created in BuddyPress. I can get the info that I need in the .php page, but I can’t figure out how to pass that on to the DTX plugin. I even tried writing my own little shortcode, but I obviously am not doing something right.

    The page is:

    Here’s my idea, but I don’t know what to do next…

    /* Insert a PHP variable */
    function cf7_php($atts){
    'key' => 0,
    ), $atts));

    $value = $tutoremail;
    return $value;
    add_shortcode('CF7_PHP', 'cf7_php');

    That $tutoremail is a variable that I can create on the php page of the members.

    I’m sure I’m just missing something simple.


    • Hi Lance,

      Glad you like the plugin. It’d be useful if you can post the code that you use to retrieve the email address.

      Note that if the email address is a custom field of the page/post, you can use the CF7_get_custom_field shortcode as described under Custom Fields

      But let’s assume you have a function with the signature get_tutor_email() that returns email address for the tutor on the current page (the $tutoremail variable). All you’d need to do is add it as a shortcode like this:

      add_shortcode('CF7_get_tutor_email', 'get_tutor_email');

      Then add in the hidden field into the CF7 form:

      [dynamichidden recipient-email "CF7_get_tutor_email" ]

      And use the [recipient-email] tag as the CF7 form’s To: field.

      If you want to use the shortcode you wrote, you’d need to make $tutoremail a global variable, initialized before the shortcode is called, so that it can be accessed in your shortcode. Best not to create too many global variables, but it’d work.

      Hope that helps,


  7. Been looking around all night for a way of getting custom fields (email recipient) to register in the sidebar. Thanks, thanks, thanks!

  8. Is it possible that the custom field shortcode isn’t getting values from custom post types?

    • The shortcode is really just a wrapper for the get_post_meta function, which as far as I know works for all post types. So I don’t think that should be an issue.

  9. my website allows guest users to make posts. they fill out their email address which gets sent to a custom field. i am then using contact form 7 with the dynamic text editor to reply to that user’s email address by pulling the data from the custom field using this code
    [dynamichidden recipient-email "CF7_get_custom_field key='Author Email' obfuscate='on'"]

    everything works great, however when using developer tools like firebug, you can still view the person’s email address that you are sending to and this is a major problem. please help. any ideas???

    • Yup, because of the way CF7 works, the best it can do is obfuscate the email slightly from spambots (by encoding the text), but it’s still available to humans.

      You COULD use pipes, but you’d need to modify the CF7 core to interpret the dynamic text shortcodes appropriately. I’ll try to write up an article on this when I get some time.

  10. I’m trying to add a contact form to the Business Directory Plugin. the email address is stored in the db and I can display it on the screen, but am unsure how to pass it to the Contact 7 form using the extension. I want a link in the Business Directory to open a new window with the Contact 7 form in it, can I pass the email on the url and have the Contact 7 form grab it from the path?


      • Chris, thanks for the extension plugin!
        I am having the same problem with Eric. I have a author page which they can send an email to the author, and it will send a copy to the admin. I can display the author email but I can’t put it in the CF7, also can you show a sample of shortcode? And how to get the $user_email and pass it to CF7? Thanks in advance!

  11. Pingback: Dynamic Contact Form 7 | Darlene Grace Arcenal

Comments are closed.