Dynamically Attach Files in Contact Form 7 Email

    Ankit Shah
Listen to this article

Attachment Contact Form 7Contact 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.


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.

Contact Form Registration

When user registers for a Spanish course, you want to send him a different attachment.


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);


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.

Ankit Shah

Ankit Shah

10 Responses

  1. This tutorial is exactly what I am trying to accomplish. Is there a way I could get some more information? Thanks!

  2. 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.

  3. 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.

    1. 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.

  4. Can anybody please explain what exactly the meaning of the followings:

  5. 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.

  6. 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!

  7. 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

Leave a Reply

Your email address will not be published. Required fields are marked *

Get The Latest Updates

Subscribe to our Newsletter

A key to unlock the world of open-source. We promise not to spam your inbox.

Suggested Reads

Join our 55,000+ Subscribers

    The Wisdm Digest delivers all the latest news, and resources from the world of open-source businesses to your inbox.

    Suggested Reads