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.
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.
- 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
- Set the Dynamic Value to
- 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!
Man, if this solution works I need to pay you a beer.
Thanks so much for collating all these stuff for the community.
You’re welcome! Hope it’s helpful for you 🙂
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.
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 🙂
Aha … Boy …
With my almost zero coding skills, I will have to wait then, huh? 🙂
Thanks again for providing the plugin.
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?
If you just want this info in your email, you could do this without CF7 DTX, just using Special Mail Tags:
If you want a field in your form to be auto-populated you can use the CF7 DTX
See http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/ for more details
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
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?
You can pass any variables you want to the Contact Form via GET or POST parameters. Check out this tutorial which outlines the process http://sevenspark.com/code/how-to-create-a-dynamic-wordpress-contact-form
Thanks Chris! The dynamic code you provide looks like it is for the blog-user’s logged in email account. What code would I use to apply a select-box list of people|email ?
CF7 DTX only works with text and hidden inputs, there is no support for select boxes currently. If you are coming to the contact page via a link, I’d recommend either setting a hidden field via either a hard-coded link or a custom field on the link page. You can put whatever you want into a GET variable and then retrieve it in the form. How you put the email address into the GET var is up to you.
If you want a select list, you can always use the default CF7 method, but I don’t know any way of pre-selecting http://contactform7.com/blog/2009/11/26/selectable-recipient-with-pipes/
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: http://quincytutoring.com/members/Admin/profile/
Here’s my idea, but I don’t know what to do next…
/* Insert a PHP variable */
'key' => 0,
$value = $tutoremail;
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.
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_fieldshortcode as described under Custom Fields http://wordpress.org/extend/plugins/contact-form-7-dynamic-text-extension/
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:
Then add in the hidden field into the CF7 form:
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
$tutoremaila 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,
That worked PERFECT! Thanks for taking the time to explain that to me.
Been looking around all night for a way of getting custom fields (email recipient) to register in the sidebar. Thanks, thanks, thanks!
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.
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.
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?
Yes, just pass it as a GET variable and pick it up using the CF7_GET shortcode. Here’s an example you can modify to your own needs:
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!
Pingback: Dynamic Contact Form 7 | Darlene Grace Arcenal