In this post, I will explain the file uploading and attachment feature of Contact Form 7. With this feature, you can allow your users to upload their files via your form, and then an email with attachments of the files is sent to you.
To set up, two steps are needed: 1) Add file uploading fields in your form, 2) Set up your mail settings to attach the uploaded files. The two steps will be explained in the rest of this post.
Adding file uploading fields in your form
Like for other types of form fields, Contact Form 7 provides form tags for file uploading fields (
<input type="file"> in HTML):
file* is a required field and requires the user to upload a file.
||Acceptable file types. List the file extensions after
||Limit the max file size acceptable. You can use
[file your-file filetypes:pdf|txt limit:2mb]
Contact Form 7 applies default restrictions for file type and file size when you do not set the
limit: (file size) options explicitly. Default acceptable file types (extensions) are: jpg, jpeg, png, gif, pdf, doc, docx, ppt, pptx, odt, avi, ogg, m4a, mov, mp3, mp4, mpg, wav, and wmv. Default acceptable file size is 1 MB (1048576 bytes).
Setting up file attachments with a mail
To attach the uploaded files to the mail, put mail tags corresponding to form tags for file uploading fields into ‘File attachments’ field as shown below.
In this example, the form tag for the file uploading field is:
[file your-file filetypes:pdf]
Therefore, the corresponding mail tag to this is:
Note that what you’re required to put in the ‘File attachments’ field is
[file your-file filetypes:pdf].
If you have multiple files uploaded and want to attach them into an email, simply line them up in the ‘File attachments’ field like this:
Local file attachment
Contact Form 7 3.5 and higher support local file attachment. You can put local file paths in the ‘File attachments’ field and those files will be attached to the email as well as uploaded files.
Put a file path per line. When the path is not an absolute path, it will be treated as a relative path to wp-content directory.
[your-file][your-another-file] /home/you/dir/dir/dir/fantasticpicture.jpg uploads/2013/08/08/boringguide.pdf
How your uploaded files are managed
After a user uploads a file through your contact form, Contact Form 7 checks to see if: 1.) Any PHP errors have occurred; 2.) the file type and file size are valid; and then, if the check turns out okay, Contact Form 7 moves the uploaded file to a temporary folder. At this point, Contact Form 7 attaches the file to the mail and sends it. After these procedures, Contact Form 7 then removes the file from the temporary folder.
The location of the temporary folder depends upon your settings in WordPress. Open the Settings > Miscellaneous (Settings > Media if you use WordPress 3.0 or higher) menu, and check the setting for Store uploads in this folder field. The default is
wp-content/uploads. Normally, Contact Form 7 creates a
wpcf7_uploads folder in this location and uses it as the temporary folder for any uploaded files.
This folder is created automatically, but sometimes it can fail. The most possible reason for this is that the parent folder doesn’t have sufficient writing permissions. In such cases, you can change the permissions or create a folder manually.
You can also change the path of the temporary folder by setting the
WPCF7_UPLOADS_TMP_DIR constant in your wp-config.php like this:
define( 'WPCF7_UPLOADS_TMP_DIR', '/your/file/path' );
WPCF7_UPLOADS_TMP_DIR is defined, this directory is used as the temporary folder instead.
Make certain your temporary folder for uploaded files exists and is writable. Otherwise, all file uploading will fail.