formspdfgravity

How do I attach a pdf file to a Gravity Forms Notification?


Gravity forms offers a way to attach files from the file uploader (See code below), but how would I change this code to simply attach my own PDF file from either a hidden field value or simply paste the pdf file within this code? I tried a few things but it didn't work. Any help would be appreciated!

 add_filter( 'gform_notification', '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
if ( $notification['name'] == 'User Notification' ) {

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

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

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

    $notification['attachments'] = $attachments;

}

return $notification;
  }

Solution

  • Based on that code, something like this should work. Replace the $url value with the URL to your PDF.

    add_filter( 'gform_notification', 'change_user_notification_attachments', 10, 3 );
    function change_user_notification_attachments( $notification, $form, $entry ) {
    
        if ( $notification['name'] == 'User Notification' ) {
            $url = 'http://yoursite.com/path/to/file.pdf';
            $notification['attachments'][] = $url;
        }
    
        return $notification;
    }