How to receive an email notification even there is an error validation?

Hi all,

I wanted to receive an email notification even the submission was failed or contains an error validation. The email notifications should contain all the data entered in the input fields. I used “gform_after_submission” hook to meet the requirements. However, I don’t know why it is not working. Please see my code below. Thank you in advance!

==========

add_action( ‘gform_after_submission’, ‘custom_send_email_notification’, 10, 2 );
function custom_send_email_notification( $entry, $form ) {
// Specify the form ID you want to include
$target_form_id = 123; // Replace 123 with your actual form ID

// Check if the submitted form matches the target form
if ( $form['id'] == $target_form_id ) {
			
    // You can customize the email recipients, subject, etc. here
    $to = 'recipient@example.com';
    $subject = 'Form Submission';

    // Build email message with all input fields
    $message = '';
    foreach ( $form['fields'] as $field ) {
        $value = rgar( $entry, $field->id );
        $label = $field->label;
        $message .= "$label: $value\n";
    }

    // Send email
    wp_mail( $to, $subject, $message );
}

}

The gform_after_submission hook only runs when the form has successfully passed validation. If you want to do something when the form is invalid, you could use the gform_validation filter with a late priority, e.g.

add_filter( 'gform_validation', function ( $validation_result ) {
	if ( ! rgar( $validation_result, 'is_valid' ) ) {
		// do something when the form is invalid
	}

	return $validation_result;
}, 100 );

Oh~ Thanks a lot!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.