Last active
October 7, 2021 20:14
-
-
Save yanknudtskov/4e51117fc06698322ab457a944a67aa8 to your computer and use it in GitHub Desktop.
When PHPs upload limit is reached, the mailserver will reject the message using a HTTP Error 413 Request entity too large. This code for gravity forms fixes this by setting a fileupload limit, checking each forms attachements. If the limit is reached, the attachments are removed from the e-mail (they are still on the entry in Gravity Form) and t…
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// define( 'ONE_MB', 1024 ); // 1 * 1024 | |
// define( 'TWENTYFIVE_MB', 26214400 ); // 25 * 1024 | |
// define( 'TWENTYFOUR_MB', 25165824 ); // 24 * 1025 | |
define( 'LARGE_NOTIFICATIONS_ATTACHMENT_LIMIT_IN_BYTES', 25165824 ); // 24 * 1025 = 24MB | |
add_filter( 'gform_notification', 'yanco_filter_large_notification_attachments', 10, 3 ); | |
function yanco_filter_large_notification_attachments( $notification, $form, $entry ) { | |
$fileupload_fields = GFCommon::get_fields_by_type( $form, array( 'fileupload' ) ); | |
$upload_path = GFFormsModel::get_upload_path( $entry[ 'form_id' ] ); | |
$upload_url = GFFormsModel::get_upload_url( $entry[ 'form_id' ] ); | |
if ( ! is_array( $fileupload_fields ) ) { | |
return $notification; | |
} else { | |
$total_attachment_filesize = 0; | |
$file_url_array = array(); | |
$notification['attachments'] = rgar( $notification, 'attachments', array() ); | |
foreach( $fileupload_fields as $field ) { | |
$url = rgar( $entry, $field->id ); | |
if ( empty( $url ) ) { | |
continue; | |
} elseif ( $field->multipleFiles ) { | |
$uploaded_files = json_decode( stripslashes( $url ), true ); | |
foreach ( $uploaded_files as $uploaded_file ) { | |
$filename = str_replace( $upload_url, $upload_path, $uploaded_file ); | |
$total_attachment_filesize += filesize( $filename ); // Add to the total attachment size | |
$file_url_array[] = $uploaded_file; // Add to the list of file URLs | |
} | |
} else { | |
$filename = str_replace( $upload_url, $upload_path, $entry[ $field->id ] ); | |
$total_attachment_filesize += filesize( $filename ); // Add to the total attachment size | |
$file_url_array[] = $url; // Add to the list of file URLs | |
} | |
} | |
// Test if the attachments are > 25MB | |
if( $total_attachment_filesize >= LARGE_NOTIFICATIONS_ATTACHMENT_LIMIT_IN_BYTES ) { | |
$notification['message'] .= '<h2>' . __( 'Attachments exceeded the fileupload limit', 'yanco_child_theme' ) . '</h2>'; | |
$notification['message'] .= '<p>' . __( 'The attachments have been removed from this email, but you can download them using the links below.', 'yanco_child_theme' ) . '</p>'; | |
if( !empty( $file_url_array ) ) { | |
$notification['message'] .= '<h3>Link to attached files</h3>'; | |
$notification['message'] .= '<ul>'; | |
foreach( $file_url_array as $file_url ) { | |
$notification['message'] .= '<li><a href="' . $file_url . '">' . $file_url . '</a></li>'; | |
} | |
$notification['message'] .= '</ul>'; | |
} | |
unset( $notification['attachments'] ); | |
} | |
} | |
return $notification; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment