Using Gravity Forms ‘gform_mailgun_send_email_failed’ PHP filter

The gform_mailgun_send_email_failed action hook is triggered when the MailGun API reports it is unable to send the email.

Usage

add_action('gform_mailgun_send_email_failed', 'my_function', 10, 6);

Parameters

  • $error_message (string): The error message returned by the API.
  • $mailgun_email (array): The Mailgun email arguments.
  • $email (array): The original email details.
  • $message_format (string): The message format, HTML or text.
  • $notification (array): An array of properties which make up a notification object. See Notifications Object for possible properties.
  • $entry (array): The entry object.

More information

See Gravity Forms Docs: gform_mailgun_send_email_failed

Examples

Log error message

Log the error message when Mailgun fails to send an email.

function log_mailgun_error($error_message, $mailgun_email, $email, $message_format, $notification, $entry) {
    error_log("Mailgun email send failed: " . $error_message);
}
add_action('gform_mailgun_send_email_failed', 'log_mailgun_error', 10, 6);

Send a notification to the admin

Send a notification to the admin when Mailgun fails to send an email.

function notify_admin_on_failure($error_message, $mailgun_email, $email, $message_format, $notification, $entry) {
    $admin_email = get_option('admin_email');
    wp_mail($admin_email, 'Mailgun Email Send Failed', 'Error message: ' . $error_message);
}
add_action('gform_mailgun_send_email_failed', 'notify_admin_on_failure', 10, 6);

Save the error message to the entry’s notes

Save the error message as a note in the entry when Mailgun fails to send an email.

function save_error_to_notes($error_message, $mailgun_email, $email, $message_format, $notification, $entry) {
    $note = sprintf('Mailgun email send failed: %s', $error_message);
    GFFormsModel::add_note($entry['id'], $entry['form_id'], 0, $note);
}
add_action('gform_mailgun_send_email_failed', 'save_error_to_notes', 10, 6);

Retry sending the email

Retry sending the email when Mailgun fails to send it.

function retry_email_send($error_message, $mailgun_email, $email, $message_format, $notification, $entry) {
    sleep(10); // Wait for 10 seconds
    GFCommon::send_email($email, $entry, $form, $notification);
}
add_action('gform_mailgun_send_email_failed', 'retry_email_send', 10, 6);

Change the email service

Switch to an alternative email service when Mailgun fails to send an email.

function switch_email_service($error_message, $mailgun_email, $email, $message_format, $notification, $entry) {
    // Use another email service, e.g., SendGrid
    // Replace the following line with your alternative email service function
    alternative_email_service_send($email, $entry, $form, $notification);
}
add_action('gform_mailgun_send_email_failed', 'switch_email_service', 10, 6);