Devin Walker is a San Diego based entrepreneur, developer and designer.

Gravity Forms Email Attachments for White Papers, Files, etc.

Posted on Sep 19, 2012 in Coding, WordPress | 5 Comments

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:

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:

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.

5 Comments

  1. 256studio
    May 16, 2013

    Can this be used for image attachments with different names? I have a form that people upload head-shots, but they are all JPG.

  2. Janey Lee
    October 4, 2013

    Question about this line

    $attachments = $uploadDir[‘path’].’/my-attachment-file.pdf’;

    Would that be http://www.mydomain.com/wp-content/uploads/2013/10/my-attachment-file.pdf

    or http://www.mydomain.com/wp-content/uploads/my-attachment-file.pdf

    Basically, I am curious to see if it knows what folder under uploads that the attachment is in.

  3. Madeleine Burke
    December 10, 2013

    This no longer seems to work. Is this hook compatible with the latest gravity forms still?

    • Web Propellers
      June 5, 2015

      Hello Maddie,

      I have the same problem too. Have you already figured this out? If so, can I request some walk throughs or any clues?

      Thanks and have a great day 🙂

  4. Pablo Salinas
    October 7, 2016

    Hi. What if I want a different file for each form?