Contact Form 7 is by far the most popular contact form plugin in WordPress. The contact form allows you to create a simple enough form using HTML tags. The forms you create can be offered as subscription forms, contact forms, registration forms, etc. Using the mail options available, you can create an auto-responder email, which is sent every time a user contacts you via the Contact Form 7 plugin. You can even add attachments in the mail you send.
In this article we will learn how to create a registration form using contact form 7 and dynamically change the files attached in the contact email sent, depending on user inputs.
[space]
Create a Registration Form using Contact Form 7 plugin
The contact form can very easily be used as a registration form for courses or events you offer. To use the same contact form for different courses or events, you will need a field, like a drop down list, from which a user can select a particular course or event he/she wants to register for. It makes sense, that the auto-reply email sent upon user registration need not be the same for every course or event available.
For example, say you have a registration form for language courses. The registration form will contain fields to accept user details like name and email address, and the course name they are interested in. The course will be a drop down list, from which they have to make a selection.
When user registers for a Spanish course, you want to send him a different attachment.
[space]
Attach a File depending on User Input
When a user registers to a course, basically the contact form 7 is submitted. Before the mail is sent, there is a hook available, wpcf7_before_send_mail, which you can use to add the corresponding attachment. Using the Contact Form 7 object, you can get the selected value in the dropdown menu.
Based on the value of the dropdown menu, we can select the file which has to be sent. This file should be present in your uploads folder. We can get the file using the get_template_directory_uri() and uploads folder path. By specifying an identifier (course-details) for the file attachments, we can use it to add our files to be attached.
add_action( 'wpcf7_before_send_mail', 'my_dynamic_attachments' ); function my_dynamic_attachments($cf7) { //check if it is the registration form if ($cf7->id==123) { // get the dropdown menu value and the corresponding file $filename = get_course_details_filepath($cf7->form['menu-language']); $cf7->uploaded_files = array('course-details'=>$filename); } }
[space]
Thus, using an available hook you can dynamically attach and send files in the contact form email. This hook also allows you to make changes in the message body or add recipients.
10 Responses
This tutorial is exactly what I am trying to accomplish. Is there a way I could get some more information? Thanks!
Hi Nikki,
Glad the post helped. What kind of information do you need?
Thanks for this post!
Here’s alternative approach:
add_filter(‘wpcf7_mail_components’, function($components, $form) {
if ($form->id == 123)
{
$filename = get_course_details_filepath($form->form[‘menu-language’]);
$components[‘attachments’] = array($filename);
}
return $components;
}, 10, 2);
If you dig into Contact Form 7 code, you’ll see that ‘attachments’ element of components array passes directly into wp_mail function.
Hey Roman,
Thanks for the alternate approach! I will try it out. 🙂
This would be perfect for my needs but I can’t get it to work. I don’t think I have the file directory right. Where do you use get_template_directory_uri() ? How do the attachments need to be named? Right now I have it in my uploads folder (not in any subfolders) and the form can’t seem to find the attachment.
Hi Greg,
If you have the file in your uploads folder, you need to use the wp_upload_dir() function to get the file. Say you have added this code in your custom plugin. Then, you could upload the files as part of the plugin and then use the path e.g. plugin_dir_url( __FILE__ )./Spanish.pdf to retrieve the file path.
Can anybody please explain what exactly the meaning of the followings:
get_course_details_filepath
[‘menu-language’]
‘course-details’
I’m also stuck on how to complete this.
My form has checkboxes. I want to be able to attach a document(s) depending on the checkbox(es) selected.
Can anyone shed some more light on this please?
Thanks in advance.
I’ve searched the web relentlessly and have been fighting with this for a while. Does this method still work with CF7, or is there an updated version? Thanks!
I would like to know if this code still works with new versions of CF7. I tried to add this code but the attachment does not appear in the message. thank you so much