Skip to content

Commit

Permalink
Tweak - Catch mail send error (#1316)
Browse files Browse the repository at this point in the history
* Added - EVF Email Failed notice

* Fix - PHPCS issue

* Chnagelog updated
  • Loading branch information
deepench authored Sep 5, 2024
1 parent e52d267 commit 5b33fae
Show file tree
Hide file tree
Showing 7 changed files with 137 additions and 1 deletion.
4 changes: 4 additions & 0 deletions changelog.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
== Changelog ==

= 3.0.3 - xx-xx-2024
* Tweak - Hidden field editable compatibility.
* Tweak - Catch mail send error.

= 3.0.2 - 07-08-2024
* Feature - Enable minimum time for form submission.
* Feature - Delete option in Header logo in PDF Submission.
Expand Down
19 changes: 19 additions & 0 deletions includes/admin/class-evf-admin-notices.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class EVF_Admin_Notices {
'survey' => 'survey_notice',
'allow_usage' => 'allow_usage_notice',
'php_deprecation' => 'php_deprecation_notice',
'email_failed' => 'email_failed_notice',
);

/**
Expand Down Expand Up @@ -84,6 +85,7 @@ public static function reset_admin_notices() {
self::add_notice( 'survey' );
self::add_notice( 'allow_usage' );
self::add_notice( 'php_deprecation' );
self::add_notice( 'email_failed' );
}

/**
Expand Down Expand Up @@ -341,6 +343,23 @@ public static function php_deprecation_notice() {
}
}

/**
* Email Failed Notice
*/
public static function email_failed_notice() {
$failed_data = get_transient( 'everest_forms_mail_send_failed_count' );
$failed_count = isset( $failed_data['failed_count'] ) ? $failed_data['failed_count'] : 0;
$error_message = isset( $failed_data['error_message'] ) ? $failed_data['error_message'] : '';
$show_notice = $failed_count > 5 ? true : false;
if ( $show_notice ) {
$is_dismissed = get_option( 'everest_forms_email_send_notice_dismiss', false );
}
if ( $show_notice && ! $is_dismissed ) {
include 'views/html-notice-email-failed-notice.php';
}
}


/**
* Remove non-EverestForms notices from EverestForms pages.
*
Expand Down
44 changes: 44 additions & 0 deletions includes/admin/views/html-notice-email-failed-notice.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
<?php
/**
* Admin View: Notice - Email Failed Notice.
*
* @package Everest Forms
*/

if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly.
}
?>

<div class="notice notice-warning is-dismissible" id="everest-forms-email-failed-notice">
<p>
<strong><?php esc_html_e( 'Everest Forms Email Send Error', 'everest-forms' ); ?></strong><br/>
<?php esc_html_e( 'The last email sent from the Everest Forms Plugin was not delivered to the user.', 'everest-forms' ); ?>
</p>
<p style="border-left: 2px solid #72aee6; background: #F0FFFF; padding: 10px;">\
/* translators: %s: Error Message*/
<?php echo esc_html( sprintf( __( '%s', 'everest-forms' ), $error_message ) ); ?>
</p>
<br/>
<p>
<a href="https://docs.wpeverest.com/everest-forms/docs/emails-are-not-being-delivered/" target="_blank">
<?php esc_html_e( 'Learn More', 'everest-forms' ); ?>
</a>
</p>
</div>

<script>
jQuery(function($) {
$(document).ready(function() {
var noticeContainer = $('#everest-forms-email-failed-notice');
noticeContainer.find('.notice-dismiss').on('click', function(e) {
e.preventDefault();

$.post(ajaxurl, {
action: 'everest_forms_email_failed_notice_dismiss',
_wpnonce: '<?php echo esc_js( wp_create_nonce( 'email_failed_nonce' ) ); ?>'
});
});
});
});
</script>
15 changes: 15 additions & 0 deletions includes/class-evf-ajax.php
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ public static function add_ajax_events() {
'survey_dismiss' => false,
'allow_usage_dismiss' => false,
'php_notice_dismiss' => false,
'email_failed_notice_dismiss' => false,
'enabled_form' => false,
'import_form_action' => false,
'template_licence_check' => false,
Expand Down Expand Up @@ -830,6 +831,20 @@ public static function php_notice_dismiss() {
wp_die();
}


/**
* Triggered when clicking the email failed notice.
*/
public static function email_failed_notice_dismiss() {
check_ajax_referer( 'email_failed_nonce', '_wpnonce' );

if ( ! current_user_can( 'manage_everest_forms' ) ) {
wp_die( -1 );
}
update_option( 'everest_forms_email_send_notice_dismiss', true );
wp_die();
}

/**
* Triggered when clicking the form toggle.
*/
Expand Down
13 changes: 13 additions & 0 deletions includes/class-evf-emails.php
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,19 @@ public function send( $to, $subject, $message, $attachments = '', $connection_id
// Let's do this.
$sent = wp_mail( $to, $subject, $message, $this->get_headers(), $this->attachments );

if ( ! $sent ) {
$error_message = apply_filters( 'everest_forms_email_send_failed_message', '' );
$failed_data = get_transient( 'everest_forms_mail_send_failed_count' );
$failed_count = $failed_data && isset( $failed_data['failed_count'] ) ? $failed_data['failed_count'] : 0;
++$failed_count;
set_transient(
'everest_forms_mail_send_failed_count',
array(
'failed_count' => $failed_count,
'error_message' => $error_message,
)
);
}
// Hooks after the email is sent.
do_action( 'everest_forms_email_send_after', $this );

Expand Down
37 changes: 37 additions & 0 deletions includes/evf-core-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -5604,3 +5604,40 @@ function evf_get_next_key_array( $arr, $key ) {

return isset( $next_key ) ? $next_key : '' ;
}


add_action( 'wp_mail_failed', 'evf_email_send_failed_handler', 1 );

if ( ! function_exists( 'evf_email_send_failed_handler' ) ) {

/**
* Handle errors fetch mechanism when mail send failed.
*
* @param object $error_instance WP_Error message instance.
*/
function evf_email_send_failed_handler( $error_instance ) {
$error_message = '';
$decoded_message = json_decode( $error_instance->get_error_message() );

if ( json_last_error() === JSON_ERROR_NONE && ! empty( $decoded_message ) ) {
/* translators: %s: Status Log URL */
$error_message = wp_kses_post( sprintf( __( 'Please check the `evf_mail_errors` log under <a target="_blank" href="%s"> Logs </a> section.', 'everest-forms' ), admin_url( 'admin.php?page=evf-tools&tab=logs' ) ) );
} else {
$error_message = $error_instance->get_error_message();
}

evf_get_logger()->info(
$error_message,
array( 'source' => 'evf_mail_errors' )
);

if ( ! empty( $error_message ) ) {
add_filter(
'everest_forms_email_send_failed_message',
function ( $msg ) use ( $error_message ) {
return $error_message;
}
);
}
}
}
6 changes: 5 additions & 1 deletion readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ Tags: contact form, custom form, form builder, forms, survey
Requires at least: 5.2
Tested up to: 6.6.1
Requires PHP: 7.2
Stable tag: 3.0.2
Stable tag: 3.0.3
License: GPLv3
License URI: http://www.gnu.org/licenses/gpl-3.0.html

Expand Down Expand Up @@ -308,6 +308,10 @@ Yes you can! Join in on our [GitHub repository](https://github.com/wpeverest/eve

== Changelog ==

= 3.0.3 - xx-xx-2024
* Tweak - Hidden field editable compatibility.
* Tweak - Catch mail send error.

= 3.0.2 - 07-08-2024
* Feature - Enable minimum time for form submission.
* Feature - Delete option in Header logo in PDF Submission.
Expand Down

0 comments on commit 5b33fae

Please sign in to comment.