connector_factory = $connector_factory; $this->plugin_data = $plugin_data_store; $this->emails = $emails_model; $this->logs = $log_model; $this->email_endpoint = $email_endpoint; } protected function get_nonce_name() { return self::ACTION_NAME; } protected function get_test_email_markup( $as_html ) { if ( empty( $as_html ) ) { return esc_html__( 'Test Successful', 'gravitysmtp' ) . "\r\n\r\n" . esc_html__( 'Congratulations! Gravity SMTP is sending emails correctly!', 'gravitysmtp' ) . "\r\n" . esc_html__( 'Gravity SMTP is taking care of sending your emails, so now you can focus on the content of your emails and leave the technical details to us.', 'gravitysmtp' ); } $image_base_url = \Gravity_Forms\Gravity_SMTP\Gravity_SMTP::get_base_url() . '/assets/images/email-templates/'; return ' Email Template '; } public function handle() { if ( ! $this->validate() ) { wp_send_json_error( __( 'Missing required parameters.', 'gravitysmtp' ), 400 ); } $this->override_error_handling(); $self = $this; add_action( 'gravitysmtp_after_mail_created', function( $created_id ) use ( $self ) { $self->last_email_id = $created_id; }, 10, 1 ); $email = filter_input( INPUT_POST, self::PARAM_EMAIL, FILTER_SANITIZE_EMAIL ); $connector = filter_input( INPUT_POST, self::PARAM_CONNECTOR_TYPE ); $as_html = filter_input( INPUT_POST, self::PARAM_AS_HTML ); $from_email = filter_input( INPUT_POST, self::PARAM_FROM_EMAIL, FILTER_SANITIZE_EMAIL ); $connector = htmlspecialchars( $connector ); $as_html = htmlspecialchars( $as_html ) !== 'false'; $from_email = $from_email ? $from_email : get_option( 'admin_email' ); $content_type = $as_html ? 'text/html' : 'text/plain'; $headers = array( 'content-type' => 'Content-type: ' . $content_type, 'from' => 'From: ' . $from_email, ); add_filter( 'gravitysmtp_connector_for_sending', function( $current_connector, $email_args ) use ( $connector ) { return array( 'force' => true, 'connector' => $connector ); }, 8, 2 ); $success = wp_mail( array( 'email' => $email ), __( 'Test Email from Gravity SMTP', 'gravitysmtp' ), $this->get_test_email_markup( $as_html ), $headers, array() ); if ( $success === true ) { wp_send_json_success( array( 'email' => $email ) ); } $full_log = $this->get_full_log_data( $this->last_email_id ); $issues = array(); $log_copy = ''; if ( isset( $full_log['technical_information'] ) && is_array( $full_log['technical_information']['log'] ) ) { array_push( $issues, end( $full_log['technical_information']['log'] ) ); $log_copy = implode( "\r\n", $full_log['technical_information']['log'] ); } $reasons = array(); $steps = array(); if ( empty( $issues ) ) { $reasons = array( __( 'Incorrect plugin settings, such as invalid SMTP credentials or expired API key.', 'gravitysmtp' ), __( 'The SMTP server blocking the incoming connection.', 'gravitysmtp' ), __( 'Your web host rejecting the connection.', 'gravitysmtp' ), ); $steps = array( __( 'Triple check the plugin settings and ensure they are accurate, especially if you copy-pasted the values.', 'gravitysmtp' ), __( 'Contact your web hosting provider to verify if your server allows outside connections and if any firewall or security policies are in place that could interfere.', 'gravitysmtp' ), __( 'Consider using one of the other available integration types.', 'gravitysmtp' ), ); } $error_data = array( 'error_message' => __( 'There was a problem sending the test email.', 'gravitysmtp' ), 'issues' => $issues, 'full_log' => $full_log, 'log_copy' => $log_copy, 'possible_reasons' => $reasons, 'recommended_steps' => $steps, ); Debug_Logger::log_message( sprintf( __( 'Send a test error: %1$s', 'gravitysmtp' ), json_encode( $error_data ) ), 'error' ); wp_send_json_error( $error_data, 500 ); } private function override_error_handling() { ini_set( 'display_errors', 0 ); unset( $GLOBALS['wp_locale'] ); } private function get_full_log_data( $email_id ) { return $this->email_endpoint->get_log_details( $email_id ); } }