v2.2.0: includes/Tools/EmailTools.php
This commit is contained in:
@@ -0,0 +1,42 @@
|
|||||||
|
<?php
|
||||||
|
namespace AskTamMCP\Tools;
|
||||||
|
defined('ABSPATH') || exit;
|
||||||
|
|
||||||
|
class EmailTools {
|
||||||
|
|
||||||
|
public static function sendTestEmail(array $args): array {
|
||||||
|
$to = $args['to'] ?? get_option('admin_email');
|
||||||
|
$subject = $args['subject'] ?? 'AskTam MCP Test Email';
|
||||||
|
$message = $args['message'] ?? 'This is a test email sent from AskTam MCP plugin at ' . get_site_url() . ' on ' . current_time('mysql') . '.';
|
||||||
|
$headers = ['Content-Type: text/html; charset=UTF-8'];
|
||||||
|
|
||||||
|
$result = wp_mail($to, $subject, $message, $headers);
|
||||||
|
return ['sent'=>$result,'to'=>$to,'subject'=>$subject,'smtp_host'=>defined('SMTP_HOST')?SMTP_HOST:(ini_get('SMTP')?:null)];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function getEmailConfig(array $args): array {
|
||||||
|
return [
|
||||||
|
'admin_email' => get_option('admin_email'),
|
||||||
|
'from_name' => get_option('blogname'),
|
||||||
|
'smtp_host' => defined('SMTP_HOST') ? SMTP_HOST : (ini_get('SMTP') ?: 'php mail()'),
|
||||||
|
'smtp_port' => defined('SMTP_PORT') ? SMTP_PORT : ini_get('smtp_port'),
|
||||||
|
'phpmailer' => class_exists('PHPMailer\\PHPMailer\\PHPMailer') ? 'available' : 'not found',
|
||||||
|
'wp_mail_smtp' => is_plugin_active('wp-mail-smtp/wp_mail_smtp.php') ? 'active' : 'inactive',
|
||||||
|
];
|
||||||
|
}
|
||||||
|
|
||||||
|
public static function listEmailLog(array $args): array {
|
||||||
|
// Works with WP Mail SMTP log or generic fallback
|
||||||
|
$per_page = (int)($args['per_page'] ?? 20);
|
||||||
|
global $wpdb;
|
||||||
|
// Try WP Mail SMTP log table
|
||||||
|
$table = $wpdb->prefix . 'wpms_logs';
|
||||||
|
if ($wpdb->get_var("SHOW TABLES LIKE '$table'")) {
|
||||||
|
$rows = $wpdb->get_results($wpdb->prepare(
|
||||||
|
"SELECT id, subject, people, status, date_sent FROM $table ORDER BY id DESC LIMIT %d", $per_page
|
||||||
|
), ARRAY_A);
|
||||||
|
return ['source'=>'wp-mail-smtp','logs'=>$rows,'count'=>count($rows)];
|
||||||
|
}
|
||||||
|
return ['source'=>'none','message'=>'No email log plugin detected. Install WP Mail SMTP for logging.','logs'=>[]];
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user