Retry failed emails
<?php
add_action( 'groundhogg/event/failed', 'gh_retry_failed_email_events', 10, 2 );
const GH_FAILED_EMAIL_MAX_RETRIES = 3;
const GH_FAILED_EMAIL_RETRY_DELAY = 60 * 3; //3 minutes
/**
* Hooks in after an event fails and if the error code is wp_mail_failed retry the event in a few minutes
*
* @param \Groundhogg\Event $event
*
* @return void
*/
function gh_retry_failed_email_events( \Groundhogg\Event $event, WP_Error $error ) {
// not a failed email
if ( $error->get_error_code() !== 'wp_mail_failed' ) {
return;
}
// optionally target specific error messages
$msg = $error->get_error_message();
if (
! str_contains( $msg, 'some error message' )
) {
return;
}
$retries = $event->get_arg( '_retries', 0 );
if ( $retries >= GH_FAILED_EMAIL_MAX_RETRIES ) {
return;
}
$event->update( [
'status' => \Groundhogg\Event::WAITING,
'time' => time() + GH_FAILED_EMAIL_RETRY_DELAY,
] );
$event->set_args( [ '_retries' => $retries + 1 ] );
}
Was this helpful?
Let us know if this document answered your question. That’s the only way we can improve.
