# Modifying Notification to Add Attachments based on Checkbox

**URL:** <https://community.gravityforms.com/t/modifying-notification-to-add-attachments-based-on-checkbox/15021>\
**Category:** Get Help\
**Tags:** notifications, gform\_notification, attachments, attach-pdf, conditional-attachments\
**Created:** [April 26, 2023, 1:53am UTC](https://community.gravityforms.com/t/modifying-notification-to-add-attachments-based-on-checkbox/15021 "2023-04-26T01:53:37Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![user62682e347e9c9081](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user62682e347e9c9081/32/5446_2.png) [@user62682e347e9c9081](https://community.gravityforms.com/u/user62682e347e9c9081)\
**Post date:** [April 26, 2023, 1:53am UTC](https://community.gravityforms.com/t/modifying-notification-to-add-attachments-based-on-checkbox/15021/1 "2023-04-26T01:53:37Z")

</div>

I’d like to know if it’s possible to attach multiple files based on items that are checked off in a checkbox field similar to how it is shown for 1 file in the documentation:

> **[gform\_notification](https://docs.gravityforms.com/gform_notification/#h-10-attach-specific-file)**
>
> Modify a notification object before it is converted into an email and sent.

I have 5 PDF files that I would like to be added to my notification based on the choices selected. I’ve tried multiple variations of adding to the attachments array, but nothing works.

(Example: 1 & 3 selected = PDFs 1 & 3 are attached • 2 & 4 selected = PDFs 2 & 4 are attached)

Any help would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![user62682e347e9c9081](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/user62682e347e9c9081/32/5446_2.png) [@user62682e347e9c9081](https://community.gravityforms.com/u/user62682e347e9c9081)\
**Post date:** [April 26, 2023, 4:36pm UTC](https://community.gravityforms.com/t/modifying-notification-to-add-attachments-based-on-checkbox/15021/2 "2023-04-26T16:36:22Z")

</div>

I was able to figure it out. Hopefully, this well help someone else who might run into any issues. I’ve replaced my data with placeholders.

* * *

```auto
add_filter( 'gform_notification_1', 'add_attachments_to_notification', 10, 3 ); // REPLACE 1 WITH YOUR FORM ID
function add_attachments_to_notification( $notification, $form, $entry ) {
    
    GFCommon::log_debug( __METHOD__. '(): Start.' ); // LOG MESSAGE - START
    
	if( $notification['name'] == 'Test Notification' ) { // REPLACE Test Notification WITH THE NAME OF YOUR NOTIFICATION
        
        GFCommon::log_debug( __METHOD__. '(): Notification name is Test Notification.' ); // LOG MESSAGE - NOTIFICATION NAME

        
        $checkbox_field_id = 76; // REPLACE 76 WITH THE ID OF YOUR CHECKBOX FIELD
		
        // GET THE VALUES OF THE SELECTED CHECKBOXES

        // REPLACE 5 WITH THE NUMBER OF OPTIONS IN YOUR CHECKBOX FIELD
		$checkbox_values = array();
		for ( $i = 1; $i <= 5; $i++ ) {
			$checkbox_values[] = rgar( $entry, $checkbox_field_id . '.' . $i );
		}
		
        // LOG - VERIFY CHECKBOX VALUES

		if ( empty( $checkbox_values ) ) {
			GFCommon::log_debug( __METHOD__. '(): No checkboxes selected.' );
		} else {
			GFCommon::log_debug( __METHOD__. '(): Checkbox values: ' . print_r( $checkbox_values, true ) );
		}

        
        // GET THE UPLOAD ROOT FOR WORDPRESS
        $upload = wp_upload_dir();
        $upload_path = $upload['basedir'];
        
        // DEFINE AN ARRAY OF ATTACHMENT FILE PATHS
        $attachments = array(
            'Value1' => $upload_path . '/2023/04/file-1.pdf', 
            'Value2' => $upload_path . '/2023/04/file-2.pdf', 
            'Value3' => $upload_path . '/2023/04/file-3.pdf', 
            'Value4' => $upload_path . '/2023/04/file-4.pdf', 
            'Value5' => $upload_path . '/2023/04/file-5.pdf'
        );
        
        GFCommon::log_debug( __METHOD__. '(): Attachments: ' . print_r( $attachments, true ) );
        
        // LOOP THROUGH THE ATTACHMENT ARRAY AND ADD THE ATTACHMENTS TO THE NOTIFICATION
        foreach ( $attachments as $value => $path ) {
            
            GFCommon::log_debug( __METHOD__. '(): Processing attachment: ' . $value );
            
            if ( in_array( $value, $checkbox_values ) ) {
                if ( file_exists( $path ) ) {
                    $notification['attachments'] = rgar( $notification, 'attachments', array() );
                    $notification['attachments'][] = $path;
                } else {
                    GFCommon::log_debug( __METHOD__. '(): Not attaching; file does not exist.' );
                }
            }
        }
    }
    
    GFCommon::log_debug( __METHOD__. '(): End.' );
    
    // RETURN ALTERED NOTIFICATION OBJECT
    return $notification;
}

```

---

<div class="post-metadata">

**Author:** ![chrishajer](https://sea2.discourse-cdn.com/flex020/user_avatar/community.gravityforms.com/chrishajer/32/88_2.png) [@chrishajer](https://community.gravityforms.com/u/chrishajer)\
**Post date:** [April 26, 2023, 8:49pm UTC](https://community.gravityforms.com/t/modifying-notification-to-add-attachments-based-on-checkbox/15021/3 "2023-04-26T20:49:32Z")

</div>

Closed on April 26, 2023, 8:49pm UTC


