Technology WordPress Tips & Tricks

Dynamically Attach Files in Contact Form 7 Email

Sometimes a form submission needs to email a file along with it, automatically. Here is how to dynamically attach files in Contact Form 7 emails, so the right documents reach the right inbox without manual effort.

Ankit Shah Ankit Shah 3 min read
Dynamically Attach Files in Contact Form 7 Email

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.

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

Contact Form Registration

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.

Get a FREE Consultation

Let's build something that lasts.

Share what's on your mind — a clear brief, a half-formed idea, or just a sense that something needs to change. We'll listen first, ask the right questions, and point you toward what's actually worth building.

We take on a handful of projects each quarter,ones where we can truly make a difference.

  • Receive a human response within 24 hours
  • Get a detailed scope and quote upfront
  • We're happy to sign an NDA upon request

    Free 30-Min Strategy Call

    Your Name *

    Your Phone No *

    Work Email *

    Your Budget*

    Project Details *