PluginProbe
EmailKit – Email Customizer for WooCommerce & WP / 1.6.9
EmailKit – Email Customizer for WooCommerce & WP v1.6.9
1.6.9 1.6.8 1.6.7 trunk 1.0.0 1.2.0 1.4.0 1.4.5 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.5.9 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.6.6
emailkit / includes / Admin / Api / TestEmail.php

TestEmail.php in EmailKit – Email Customizer for WooCommerce & WP 1.6.9, at includes/Admin/Api/TestEmail.php

101 lines 4.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace EmailKit\Admin\Api;
4
5 defined( 'ABSPATH' ) || exit;
6
7 class TestEmail {
8
9 public $prefix = '';
10 public $param = '';
11 public $request = null;
12 public function __construct() {
13
14 add_action('rest_api_init', function () {
15 register_rest_route('emailkit/v1', 'send-test-email', array(
16 'methods' => \WP_REST_Server::ALLMETHODS,
17 'callback' => [$this, 'sendEmail'],
18 'permission_callback' => '__return_true',
19 ));
20 });
21 }
22
23 public function sendEmail($request)
24 {
25 if (!wp_verify_nonce($request->get_header('X-WP-Nonce'), 'wp_rest')) {
26 return [
27 'status' => 'fail',
28 'message' => [__('Nonce mismatch.', 'emailkit')]
29 ];
30 }
31
32 if (!is_user_logged_in() || !current_user_can('manage_options')) {
33 return [
34 'status' => 'fail',
35 'message' => [ __('Access denied.', 'emailkit')]
36 ];
37 }
38
39 $post_id = $request->get_param('post_id');
40 $from = get_option('admin_email');
41 $to = $request->get_param('email');
42
43 $pre_header = get_post_meta($post_id, 'emailkit_email_preheader', true);
44 $pre_header = !empty($pre_header) ? $pre_header : esc_html__('This is a test email.', 'emailkit');
45 $subject = get_post_meta($post_id, 'emailkit_email_subject', true);
46 $subject = !empty($subject) ? $subject . ' - ' . $pre_header : $request->get_param('subject') . ' - ' . $pre_header;
47
48 $message = $request->get_param('message');
49 $headers = [
50 'From: ' . $from . "\r\n",
51 'Reply-To: ' . $from . "\r\n",
52 'Content-Type: text/html; charset=UTF-8',
53 ];
54
55 $mail_error = null;
56 add_action( 'wp_mail_failed', function( \WP_Error $wp_error ) use ( &$mail_error ) {
57 $mail_error = $wp_error;
58 } );
59
60 $sent = wp_mail( $to, $subject, $message, $headers );
61
62 if ( ! $sent ) {
63 $error_message = __( 'Failed to send the test email. Please configure an SMTP plugin.', 'emailkit' );
64
65 if ( $mail_error instanceof \WP_Error ) {
66 $raw = $mail_error->get_error_message();
67
68 $error_map = [
69 'Could not instantiate mail function' => __( 'Mail server is not configured on this server. Please use an SMTP plugin.', 'emailkit' ),
70 'SMTP connect() failed' => __( 'Failed to connect to the SMTP server. Please check your SMTP settings.', 'emailkit' ),
71 'Failed to connect to mailserver' => __( 'Failed to connect to the mail server. Please check your SMTP host and port.', 'emailkit' ),
72 'Could not connect to SMTP host' => __( 'Could not connect to the SMTP host. Please verify your SMTP settings.', 'emailkit' ),
73 'SMTP Error: Could not authenticate' => __( 'SMTP authentication failed. Please check your username and password.', 'emailkit' ),
74 'Invalid address' => __( 'The recipient email address is invalid. Please enter a valid email.', 'emailkit' ),
75 'Connection refused' => __( 'Connection to the mail server was refused. Please check your SMTP host and port.', 'emailkit' ),
76 'Connection timed out' => __( 'Connection to the mail server timed out. Please check your SMTP host and port.', 'emailkit' ),
77 ];
78
79 foreach ( $error_map as $keyword => $friendly_message ) {
80 if ( str_contains( $raw, $keyword ) ) {
81 $error_message = $friendly_message;
82 break;
83 }
84 }
85 }
86 }
87
88
89 if ($sent) {
90 return [
91 'status' => 'success',
92 'message' => [ __( 'Test email sent successfully.', 'emailkit' ) ],
93 ];
94 } else {
95 return [
96 'status' => 'fail',
97 'message' => [ esc_html( $error_message) ],
98 ];
99 }
100 }
101 }