Search
Close this search box.
Search

Gravity Forms: Conditionally Attach PDFs to Notification Emails

Listen to this article

Attachment Gravity FormsUsually a lot of forms are used on a site. May it be for registration, login, contact, enquiry, and so on. When you think about websites for Government, Legal, or organizations, the form processing systems are automated for several processes. Considering we work on WordPress, our go to plugin to develop form automation workflows, is Gravity Forms.

The Gravity Forms plugin, is certainly a well built plugin. And we are not only talking about the end features provided to a user. Even when it comes to developing features with Gravity Forms. It’s easier because the plugin provides several hooks and filters, to fine tune the behavior of the form.

 

Gravity Forms: Email Notification Field

The Gravity forms plugin provides an option to send email notifications on form submission. This option is available for each form, and can be used to send emails, to maybe the site admin, or the user (who has filled the form).

Notification Email Gravity Forms

Conditional Logic for Email Notifications

If you notice, at the bottom of the new Notification settings, there is a ‘Conditional Logic’ checkbox. This option can be selected, if the notification has to be sent, on some condition. For example, say you have a field, for State. Depending on the State selected, you may want to send a different notification.

Conditional Logic Gravity Forms

It may also happen, that you want to send attachments along with the emails. Currently there is no setting option which allows you to do this. But Gravity Forms provides a hook, using which you can dynamically send an attachment, along with the notification email.

 

Add Attachments to Notification Emails

To add attachments to notification emails, you can use the ‘gform_notification’ hook. This filter hook is used to modify the notification object before the details are sent as an email. Apart from adding attachments, you can also use this hook, to add recipients, change the email format, modify the message, etc.

In accordance to the usual Gravity form hooks, you can either use, the generic hook, ‘gform_notification’ or if you know the form id, you can specify the hook for a particular form (‘gform_notification_5’ for form id 5).

[pre]function wdm_add_attachment( $notification, $form, $entry ) {
if($notification[“name”] == “User Email Notification”){
// get attachments as $attachments
$notification[“attachments”] = $attachments;
}
return $notification;
}
add_filter(“gform_notification”, “wdm_add_attachment”, 10, 3);[/pre]

At this point, you can even select the attachment, based on the form field value. For example, similar to selecting the notification email which has to be sent, if you wanted to decide on which attachment has to be sent, you can decide this based on the $form, ‘Country’ field value.

[space]

Generate and Attach PDFs Based on Field Values

Conditional Attachment Gravity FormsNow, consider the below stated scenario,

  1. Say a person visits your site, and fills out a form.
  2. A notification email has to be sent to him, with a PDF file attached. The PDF file selected depends on a field value specified. For example, the ‘State’. If State is California, PDF file for California state has to be sent. If it is New York, PDF file for New York state has to be sent.
  3. As mentioned, this can be done, by attaching the PDF file using a function, on ‘gform_notification’ filter hook.
  4. Consider, that before attaching the file, we need to autofill the person’s details in the PDF form. In this case, we can make use of the PDF Autofill using Gravity Forms plugin.

Providing a PDF AutoFill Functionality for Gravity Forms

By using the Gravity Forms Automation: PDF Autofill solution, we can provide this functionality as follows:

  1. Since the fields have to be filled dynamically, admin can only add PDF templates.
  2. These templates will be used to generate the PDF file.
  3. The templates will be mapped to the Gravity Form. When the Gravity form is submitted, the mapped PDF files will be generated
  4. But instead of generating all the mapped PDF files, we will add a conditional logic for each template, which will check the form fields, against expected values.
  5. If the value is matched, the associated template will be selected, and the PDF file will be generated.

In this way, we can dynamically generate and auto-fill PDF files, based on the Gravity Form field value. These PDFs can then be attached and send in notification emails.

Praveen Chauhan

Praveen Chauhan

4 Responses

  1. What is the syntax for assigning conditional variables inside the PDF autofill plugin? There is a field that states “enter conditional variable,” but there is no documentation about it.

      1. So you have to insert it yourself? Can you give an example of how it is actually done instead of the overview?

        1. Hi Cameron,

          We have a demo video for PDF Autofill. You can take at it here //wisdmlabs.com/gravity-forms-pdf-word-document-automation-solution/

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

WordPress Development

Custom WordPress Solutions, for You

LearnDash Development

Custom LearnDash Solutions, for You

WooCommerce Development

Custom WooCommerce Solutions, for You

WordPress Plugins

Scale your WordPress Business

WooCommerce Plugins

Scale your WooCommerce Business

LearnDash Plugins

Scale your LearnDash Business

Label

Title

Subtext

Label

Title

Subtext

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