I’m often building corporate websites that offer users the opportunity to complete a form and receive a white paper attachment PDF file at their specified email address. Gravity Forms is my current tool of choice to handle my WordPress forms so it was important that this functionality be capable within this plugin. At the time of this post, there is no option within the gforms GUI to add an attachment, but it is possible with some simple code. Here’s how can send attachments to users who complete our forms.

Gform User Notification Attachments

Since we are concerned about sending a PDF to the form submission email, we’ll need to hook into the user attachment email using a hook included since version 1.6.3 named gform_user_notification_attachments. Here’s a quote about the hook from Gravity Forms:

We have introduced a hook to enable attaching files to email notifications. We have also included example code with the documentation for this hook which will make it easy for you to automatically attach all file uploads for a form submission as file attachments on the email notification.

Here’s the documentation for gform_user_notification_attachments which is what we need because we are not concerned with admin emails at this point. Ok, enough background… let’s dive in and get our attachments sent.

Setup Form and Upload Attachment File

In order to get started we need to make sure we have the form setup and attachment file ready. Here’s our initial tasks list:

  1. Create a Gravity Form with at least two fields: Name and Email Address (you can also whatever form fields you’d like)
  2. Place Gravity Form on page or post to test
  3. Upload your desired attachment file to your WordPress’ uploads directory (can use Media Uploader)
  4. CHMOD the file to 640 (unless you want users to be able to directly view it)

Once you have the above items completed it’s time to add our bit of code to connect the attachment to the form(s).

The Gravity Forms Attachment Filter

The example function provided by Gravity forms in the link above is a bit too much for what we need. We simply want to send the user a PDF file, not attach all file upload fields and send those as is done within their provided function.

Here’s the simple function I wrote that is run when the user submits the Newsletter form with the ID of 5:

 * Newsletter Attachments

add_filter("gform_user_notification_attachments_5", "add_attachment", 10, 3);
function add_attachment($attachments){
    $uploadDir =  wp_upload_dir();
    $attachments =  $uploadDir['path'].'/my-attachment-file.pdf';
    return $attachments;

Code Usage Notes:

  • Add the code to your theme’s functions.php or equivalent
  • Update the file name “my-attachment-file.pdf” to your file
  • To have the function applied when all forms are submitted simply remove the “_5” on the hook
  • To target another form replace the “_5” with your appropriate form ID

Note: Attachment emails take a bit longer to send/receive. Speeds will vary depending on your server and attachment file size.

Code Explained

What is happening with our function is that first we are using assigning a variable to the output of the wp_upload_dir() function get the direct path to the attachment file. Next, we create the complete attachment variable using our previously declared upload path variable. Remember, this is where you will specify actual name of the file. In the final line we simply return the file path string and let Gforms take care of the rest.

Multiple Attachments

If you have more than one attachment that you would like to send we’ll simply return an array of attachment variables. The function should go something like this:

 * Newsletter Attachments

add_filter("gform_user_notification_attachments_5", "add_attachment", 10, 3);
function add_attachment($attachments, $form){
    $uploadDir =  wp_upload_dir();   
    $attachment1 =  $uploadDir['path'].'/attachment-1.pdf';
    $attachment2 =  $uploadDir['path'].'/attachment-2.pdf';
    $attachment3 =  $uploadDir['path'].'/attachment-3.pdf';
    $attachments = array($attachment1, $attachment2, $attachment3);
    return $attachments;

Happy Emailer

Nothing too fancy here… I simply added several more attachment items and bundled them into an array that I’m returning with the function. Please note, I haven’t tested this but I’m fairly confident it should work as expected. Please comment below if you are having trouble.

Working Example of Attachments Sending

You can view an example of where I’ve implemented a working white paper download by viewing the form on the following page.

In Closing…

I hope you enjoy this article and hopefully once you get this working your users, bosses, clients, whatevers will be happily enjoying their attachment content! Use the comments below if you have any questions.

Similar Posts