| 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.'] |
| 29 |
]; |
| 30 |
} |
| 31 |
|
| 32 |
if (!is_user_logged_in() || !current_user_can('publish_posts')) { |
| 33 |
return [ |
| 34 |
'status' => 'fail', |
| 35 |
'message' => ['Access denied.'] |
| 36 |
]; |
| 37 |
} |
| 38 |
|
| 39 |
$from = get_option('admin_email'); |
| 40 |
$to = $request->get_param('email'); |
| 41 |
$subject = $request->get_param('subject'); |
| 42 |
$message = $request->get_param('message'); |
| 43 |
$headers = [ |
| 44 |
'From: ' . $from . "\r\n", |
| 45 |
'Reply-To: ' . $from . "\r\n", |
| 46 |
'Content-Type: text/html; charset=UTF-8', |
| 47 |
]; |
| 48 |
|
| 49 |
$sent = wp_mail($to, $subject, $message, $headers); |
| 50 |
|
| 51 |
if ($sent) { |
| 52 |
return [ |
| 53 |
'status' => 'success', |
| 54 |
'message' => ['Test email sent successfully.'], |
| 55 |
]; |
| 56 |
} else { |
| 57 |
return [ |
| 58 |
'status' => 'fail', |
| 59 |
'message' => ['Failed to send the test email.'], |
| 60 |
]; |
| 61 |
} |
| 62 |
} |
| 63 |
} |