We have several forms set up with webhooks that we would like to have a notification be sent to an admin if the webhook fails upon submission. I found a previous ticket from 2023 where a Gravity Forms rep provided this function to use:
add_action( ‘gform_webhooks_post_request’, function ( $response, $feed, $entry, $form ) {
if ( is_wp_error( $response ) ) {
$to = "emailhere"; // Change this to the admin email address
$subject = "Webhook failed at {$entry\['source_url'\]}";
$body = "Webhook for entry #{$entry\['id'\]} form #{$form\['id'\]} failed.";
// $body .= "Error message: " . $response->get_error_message();
// Use wp_mail to send the alert
wp_mail( $to, $subject, $body );
}
}, 10, 4 );
I obviously took out my email where it says “emailhere” in the function, but the rest is copy/pasted from the previous ticket.
We are not getting any notifications of webhook failures after adding this to our functions.php file. Please help me find out why this is not working.
I’m not seeing anything obviously wrong with the code but I did find the latest version of that code from Gravity Forms here:
I’d try with that, only updating the email and see if that works for you.
Also, I feel compelled to mention our API Alchemist add-on for Gravity Forms if you’re looking for an even easier way to trigger webhooks and see all the juicy behind-the-scenes details of each request.
I updated the code to the one you pointed out is in the Gravity Forms documentation, but we’re still not getting any email notifications when the webhooks fail. I’ve looked in all of my email folders just in case it might have been filtered somewhere other than the inbox. I’ve checked our SMTP settings and everything is good there (and we’re getting all of the other email notifications from the website without issues). I also checked our Email Log to see if an email was sent and just wasn’t delivered for whatever reason, and I’m not seeing that any emails are being sent to notify me of the webhook failure.
If someone from Gravity Forms could help, I’d appreciate it!
If you want to talk directly to the Gravity Forms team, opening a ticket is the best way to make that happen:
The next thing I’d check is to make sure the code is running at all. For this, we can tweak the original snippet to log a message rather than send an email.
With this in place, you can go enable logging for the Webhooks add-on and then trigger a webhook error. Look for “CHECKING FOR ERROR” in the log. If you find that, the code is running. Then look for “ERROR FOUND”.
If you find that, the code is running and your wp_mail() call is being triggered — there’s likely a mail issue. If you don’t find that, there may not be an error. If you find neither message, it’s possible no request is being sent and we’ll need to pivot our troubleshooting.
After working with another developer, we found that the code Gravity Forms provides in the documentation wasn’t registering that there was an error because it was only looking for whether it returned a response, not whether the response was that the webhook was successful or not (ie, not whether it was returning a 200 response vs 404, 502, etc.). So we changed up the code, and finally got this to work: