PLEASE NOTE: These forums are no longer utilized and are provided as an archive for informational purposes only. All support issues will be handled via email using our support ticket system. For more detailed information on this change, please see this blog post.

PDF attachment for user notification not working

  1. I have successfully set up a PDF attachment to a form user notification in the past on another server, but using the same code (and changing only the form ID and PDF file name), I cannot get the PDF to be attached. I am using this function added to my theme:

    // Allows Gravity Forms to send a PDF attachment in email
    add_filter("gform_user_notification_attachments_2", "add_attachment", 10, 3);
    function add_attachment($attachments, $lead, $form){
    
        //getting wordpress upload folder
        $upload = wp_upload_dir();
        $upload_path = $upload["basedir"];
    
        $attachments = array();
        //add a file from the uploads folder
        $attachments[] = $upload_path . "/choosing-a-motorcycle-tour.pdf";
    
        return $attachments;
    }

    Am I doing something wrong, or have things change in 1.7?

    -- Lee

    Posted 6 years ago on Thursday May 2, 2013 | Permalink
  2. David Peralty
    Administrator

    Yes, this hook has been deprecated. Please use our new hook - http://www.gravityhelp.com/documentation/page/Gform_notification

    We include details on how to transition your code. All my best!

    Posted 6 years ago on Thursday May 2, 2013 | Permalink
  3. Thanks David. So in the example on the page you cited, do you still use the _2 after attachments in the first line to denote form ID, or do you replace the "id" phrase with the actual form ID number (shown in line 16 of the example at the end of the cited page)?

    Here's what I used, and it did not work:

    // Allows Gravity Forms to send a PDF attachment in email
    add_filter('gform_notification', 'change_user_notification_attachments_2', 10, 3);
    function change_user_notification_attachments( $notification, $form, $entry ) {
    
     //There is no concept of user notifications anymore, so we will need to target notifications based on other criteria, such as name or subject
        if($notification["name"] == "User Notification"){
    
            $fileupload_fields = GFCommon::get_fields_by_type($form, array("fileupload"));
    
            if(!is_array($fileupload_fields))
                return $attachments;
    
            $attachments = array();
            $upload_root = RGFormsModel::get_upload_root();
            foreach($fileupload_fields as $field){
                $url = $entry[$field["id"]];
                $attachment = preg_replace('|^(.*?)/|', $upload_root, $url);
                if($attachment){
                    $attachments[] = $attachment;
                }
            }
    
            $notification["choosing-a-motorcycle-tour.pdf"] = $attachments;
    
        }
    
        return $notification;
    }

    Thanks for your helpl

    Posted 6 years ago on Friday May 3, 2013 | Permalink
  4. David Peralty
    Administrator

    You put change_user_notification_attachments_2 it should be change_user_notification_attachments

    And to specific the form it should be: gform_notification_2

    Hope that helps.

    Posted 6 years ago on Friday May 3, 2013 | Permalink
  5. Still not working. Here's what I have. What could be hanging this up?

    -- Lee

    //Adds PDF attachment to User Notification
    add_filter('gform_notification_2', 'change_user_notification_attachments', 10, 3);
    function change_user_notification_attachments( $notification, $form, $entry ) {
    
        //There is no concept of user notifications anymore, so we will need to target notifications based on other criteria, such as name or subject
        if($notification["name"] == "User Notification"){
    
            $fileupload_fields = GFCommon::get_fields_by_type($form, array("fileupload"));
    
            if(!is_array($fileupload_fields))
                return $attachments;
    
            $attachments = array();
            $upload_root = RGFormsModel::get_upload_root();
            foreach($fileupload_fields as $field){
                $url = $entry[$field["id"]];
                $attachment = preg_replace('|^(.*?)/|', $upload_root, $url);
                if($attachment){
                    $attachments[] = $attachment;
                }
            }
    
            $notification["choosing-a-motorcycle-tour.pdf"] = $attachments;
    
        }
    
        return $notification;
    }
    Posted 6 years ago on Friday May 3, 2013 | Permalink
  6. David Peralty
    Administrator

    Is the name of the notification you are trying to edit named "User Notification"?

    Posted 6 years ago on Friday May 3, 2013 | Permalink
  7. Yes, the name of the notification is "User Notification." The problem I'm having is knowing where to place the name of the file. Do I have that correct in the above example? Everything else is the same as the example you provided except I made the base directory the root of the uploads directory.

    -- Lee

    Posted 6 years ago on Friday May 3, 2013 | Permalink
  8. Hi, Lee,

    Below is an example for attaching a simple text file that resides in the uploads directory using the new hook. Take a look and let us know if you have questions.

    add_filter('gform_notification_2', 'add_attachment_pdf', 10, 3); //target form id 2, change to your form id
    function add_attachment_pdf( $notification, $form, $entry ) {
        //There is no concept of user notifications anymore, so we will need to target notifications based on other criteria,
        //such as name or subject
        if($notification["name"] == "User Notification"){
    		//get upload root for WordPress
    		$upload = wp_upload_dir();
    		$upload_path = $upload["basedir"];
    
    		//add file, use full path , example -- $attachment = "C:\\xampp\\htdocs\\wpdev\\wp-content\\uploads\\test.txt"
    		$attachment = $upload_path . "/test.txt";
            $notification["attachments"] = $attachment;
        }
    	//return altered notification object
        return $notification;
    }
    Posted 6 years ago on Monday May 6, 2013 | Permalink
  9. Hi Dana

    Like Lee, I want to attach a PDF to the notification email but, unlike Lee (I think), I've never done any PHP coding and it scares me to death!

    From your previous answer...

    i) which PHP file do you make these edits to? I saw another post mentioning "common.php" but wanted to confirm this

    ii) can I add my customised version of this code in one block at the bottom of the existing file, or does some of it need to go at the top of the file and some at the bottom?

    Sorry to ask basic PHP questions but I was kind of hoping GF would provide this kind of facility with a nice button!!

    Steve

    Posted 6 years ago on Wednesday May 22, 2013 | Permalink
  10. David Peralty
    Administrator

    You add custom PHP like this to your theme's functions.php file and its placement in that file doesn't matter.

    Posted 6 years ago on Wednesday May 22, 2013 | Permalink
  11. I'm trying to add images uploaded to a form as attachments to the notification email. I tried using the code on the Gform_Notification documentation page and I tried the code below from when 1.7 was in beta. Any help would be appreciated. The form is on this page:

    http://www.scannellproductions.com/employee-tools/promo-sheet/

    add_filter('gform_notification', 'change_admin_notification_attachments', 10, 3);
    function change_admin_notification_attachments( $notification, $form, $entry ) {
    if($notification["name"] == "Admin Notification"){

    $fileupload_fields = GFCommon::get_fields_by_type($form, array("fileupload"));

    if(!is_array($fileupload_fields))
    return $attachments;

    $attachments = array();
    $upload_root = RGFormsModel::get_upload_root();
    foreach($fileupload_fields as $field){
    $url = $lead[$field["id"]];
    $attachment = preg_replace('|^(.*?)/gravity_forms/|', $upload_root, $url);
    if($attachment){
    $attachments[] = $attachment;
    }
    }
    $notification["attachments"] = $attachments;
    }
    return $notification;
    }

    Posted 6 years ago on Monday July 1, 2013 | Permalink
  12. Richard Vav
    Administrator

    If you still require assistance with this please open a new support ticket or a priority support ticket if you are a developer license holder. Thank you.

    Posted 6 years ago on Friday July 26, 2013 | Permalink

This topic has been resolved and has been closed to new replies.