isSMTP(); $phpmailer->Host = $smtp_host; $phpmailer->SMTPAuth = true; $phpmailer->Port = $smtp_port; $phpmailer->Username = $smtp_username; $phpmailer->Password = $smtp_password; // 设置加密方式 if ($smtp_secure === 'ssl') { $phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_SMTPS; } elseif ($smtp_secure === 'tls') { $phpmailer->SMTPSecure = PHPMailer\PHPMailer\PHPMailer::ENCRYPTION_STARTTLS; } // 设置发件人信息 if (!empty($smtp_from_email)) { $phpmailer->setFrom($smtp_from_email, $smtp_from_name ?: get_bloginfo('name')); } // 设置超时时间 $phpmailer->Timeout = 30; $phpmailer->SMTPKeepAlive = false; // 启用调试(测试时总是启用) if (defined('WP_DEBUG') && WP_DEBUG || isset($_GET['nenghui_test_smtp'])) { $phpmailer->SMTPDebug = 2; $phpmailer->Debugoutput = function($str, $level) { error_log('SMTP Debug: ' . $str); }; } } // 挂载SMTP配置 add_action('phpmailer_init', 'nenghui_configure_smtp'); /** * 测试SMTP连接 */ function nenghui_test_smtp_connection() { if (!current_user_can('manage_options')) { wp_die('权限不足'); } // 检查SMTP配置是否完整 $smtp_host = get_option('nenghui_smtp_host'); $smtp_username = get_option('nenghui_smtp_username'); $smtp_password = get_option('nenghui_smtp_password'); $smtp_from_email = get_option('nenghui_smtp_from_email'); $config_errors = []; if (empty($smtp_host)) { $config_errors[] = 'SMTP服务器地址未配置'; } if (empty($smtp_username)) { $config_errors[] = 'SMTP用户名未配置'; } if (empty($smtp_password)) { $config_errors[] = 'SMTP密码未配置'; } if (empty($smtp_from_email)) { $config_errors[] = '发件人邮箱未配置'; } if (!empty($config_errors)) { add_action('admin_notices', function() use ($config_errors) { $error_list = implode('、', $config_errors); echo '

SMTP配置不完整:' . esc_html($error_list) . '

'; }); return; } $test_email = get_option('admin_email'); $subject = '[' . get_bloginfo('name') . '] SMTP测试邮件'; $message = '这是一封SMTP配置测试邮件。如果您收到此邮件,说明SMTP配置正确。\n\n发送时间:' . current_time('Y-m-d H:i:s'); // 启用错误捕获 $original_error_reporting = error_reporting(E_ALL); $original_display_errors = ini_get('display_errors'); ini_set('display_errors', 1); // 捕获PHPMailer错误 global $phpmailer; $error_message = ''; $debug_output = ''; // 捕获调试输出 ob_start(); add_action('wp_mail_failed', function($wp_error) use (&$error_message) { $error_message = $wp_error->get_error_message(); }); // 临时启用错误日志记录(使用允许的路径) $log_file = '/tmp/smtp_debug.log'; $original_log_errors = ini_get('log_errors'); $original_error_log = ini_get('error_log'); ini_set('log_errors', 1); ini_set('error_log', $log_file); $sent = wp_mail($test_email, $subject, $message); // 获取调试输出 $debug_output = ob_get_clean(); // 恢复设置 error_reporting($original_error_reporting); ini_set('display_errors', $original_display_errors); ini_set('log_errors', $original_log_errors); ini_set('error_log', $original_error_log); // 读取最新的错误日志 $recent_errors = ''; if (file_exists($log_file)) { $log_content = file_get_contents($log_file); $log_lines = explode("\n", $log_content); $recent_lines = array_slice($log_lines, -10); // 获取最后10行 $recent_errors = implode("\n", array_filter($recent_lines)); } if ($sent) { add_action('admin_notices', function() use ($test_email) { echo '

测试邮件发送成功!
测试邮件已发送到:' . esc_html($test_email) . '
请检查您的邮箱(包括垃圾邮件文件夹)。

'; }); } else { add_action('admin_notices', function() use ($error_message, $smtp_host, $smtp_username, $debug_output, $recent_errors) { $smtp_port = get_option('nenghui_smtp_port', '587'); $smtp_secure = get_option('nenghui_smtp_secure', 'tls'); $error_info = '测试邮件发送失败!
'; $error_info .= '
当前SMTP配置:
'; $error_info .= '• 服务器:' . esc_html($smtp_host) . '
'; $error_info .= '• 端口:' . esc_html($smtp_port) . '
'; $error_info .= '• 加密:' . esc_html($smtp_secure) . '
'; $error_info .= '• 用户名:' . esc_html($smtp_username) . '
'; if (!empty($error_message)) { $error_info .= '
WordPress错误信息:
' . esc_html($error_message) . '
'; } if (!empty($debug_output)) { $error_info .= '
SMTP调试输出:
' . esc_html($debug_output) . '
'; } if (!empty($recent_errors)) { $error_info .= '
最近的错误日志:
' . esc_html($recent_errors) . '
'; } $error_info .= '
常见问题排查:
'; // 检查是否是认证失败错误 if (strpos($debug_output, '526 Authentication failure') !== false || strpos($debug_output, 'Authentication failure') !== false) { $error_info .= '⚠️ 认证失败 (526错误) 解决方案:
'; $error_info .= '• 阿里云邮箱:必须使用授权码,不能使用登录密码
'; $error_info .= '• QQ邮箱:需要开启SMTP服务并使用授权码
'; $error_info .= '• 163邮箱:需要开启客户端授权密码
'; $error_info .= '• Gmail:需要开启两步验证并使用应用专用密码
'; $error_info .= '• 确认用户名格式正确(通常是完整邮箱地址)
'; $error_info .= '• 检查是否已在邮箱设置中开启SMTP服务

'; } $error_info .= '• 检查SMTP服务器地址和端口是否正确
'; $error_info .= '• 验证用户名和密码是否正确
'; $error_info .= '• 确认加密方式(SSL/TLS)设置正确
'; $error_info .= '• 检查邮箱服务商是否允许SMTP发送
'; $error_info .= '• 确认服务器防火墙没有阻止SMTP端口
'; $error_info .= '• 检查服务器的PHP扩展是否支持SSL/TLS'; echo '

' . $error_info . '

'; }); } } /** * 测试SMTP连接(不发送邮件) */ function nenghui_test_smtp_connection_only() { if (!current_user_can('manage_options')) { wp_die('权限不足'); } // 检查SMTP配置 $smtp_host = get_option('nenghui_smtp_host'); $smtp_port = get_option('nenghui_smtp_port', '587'); $smtp_username = get_option('nenghui_smtp_username'); $smtp_password = get_option('nenghui_smtp_password'); $smtp_secure = get_option('nenghui_smtp_secure', 'tls'); if (empty($smtp_host) || empty($smtp_username) || empty($smtp_password)) { add_action('admin_notices', function() { echo '

SMTP配置不完整!请先完成所有必要的SMTP配置。

'; }); return; } // 尝试连接SMTP服务器 $connection_result = ''; $error_message = ''; try { // 创建socket连接测试 $context = stream_context_create(); if ($smtp_secure === 'ssl') { $host_with_ssl = 'ssl://' . $smtp_host; } else { $host_with_ssl = $smtp_host; } $socket = @stream_socket_client( $host_with_ssl . ':' . $smtp_port, $errno, $errstr, 10, STREAM_CLIENT_CONNECT, $context ); if ($socket) { $connection_result = 'success'; fclose($socket); } else { $connection_result = 'failed'; $error_message = "连接失败: $errstr ($errno)"; } } catch (Exception $e) { $connection_result = 'failed'; $error_message = $e->getMessage(); } if ($connection_result === 'success') { add_action('admin_notices', function() use ($smtp_host, $smtp_port) { echo '

SMTP连接测试成功!
成功连接到 ' . esc_html($smtp_host) . ':' . esc_html($smtp_port) . '
现在可以尝试发送测试邮件。

'; }); } else { add_action('admin_notices', function() use ($smtp_host, $smtp_port, $error_message) { echo '

SMTP连接测试失败!
无法连接到 ' . esc_html($smtp_host) . ':' . esc_html($smtp_port) . '
错误信息:' . esc_html($error_message) . '

'; }); } } // 处理SMTP测试请求 if (isset($_GET['nenghui_test_smtp']) && $_GET['nenghui_test_smtp'] === '1') { add_action('admin_init', 'nenghui_test_smtp_connection'); } // 处理SMTP连接测试请求 if (isset($_GET['nenghui_test_smtp_connection']) && $_GET['nenghui_test_smtp_connection'] === '1') { add_action('admin_init', 'nenghui_test_smtp_connection_only'); } ?>