You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

282 lines
11 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

<?php
/**
* SMTP邮件配置
* 配置WordPress使用SMTP发送邮件
*/
// 防止直接访问
if (!defined('ABSPATH')) {
exit;
}
/**
* 配置WordPress使用SMTP发送邮件
*/
function nenghui_configure_smtp($phpmailer) {
$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');
$smtp_from_email = get_option('nenghui_smtp_from_email');
$smtp_from_name = get_option('nenghui_smtp_from_name');
// 如果SMTP配置不完整则不启用SMTP
if (empty($smtp_host) || empty($smtp_username) || empty($smtp_password)) {
return;
}
$phpmailer->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 '<div class="notice notice-error is-dismissible"><p><strong>SMTP配置不完整</strong>' . esc_html($error_list) . '</p></div>';
});
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 '<div class="notice notice-success is-dismissible"><p><strong>测试邮件发送成功!</strong><br>测试邮件已发送到:' . esc_html($test_email) . '<br>请检查您的邮箱(包括垃圾邮件文件夹)。</p></div>';
});
} 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 = '<strong>测试邮件发送失败!</strong><br>';
$error_info .= '<br><strong>当前SMTP配置</strong><br>';
$error_info .= '• 服务器:' . esc_html($smtp_host) . '<br>';
$error_info .= '• 端口:' . esc_html($smtp_port) . '<br>';
$error_info .= '• 加密:' . esc_html($smtp_secure) . '<br>';
$error_info .= '• 用户名:' . esc_html($smtp_username) . '<br>';
if (!empty($error_message)) {
$error_info .= '<br><strong>WordPress错误信息</strong><br><code>' . esc_html($error_message) . '</code><br>';
}
if (!empty($debug_output)) {
$error_info .= '<br><strong>SMTP调试输出</strong><br><pre style="background:#f1f1f1;padding:10px;font-size:12px;max-height:200px;overflow-y:auto;">' . esc_html($debug_output) . '</pre>';
}
if (!empty($recent_errors)) {
$error_info .= '<br><strong>最近的错误日志:</strong><br><pre style="background:#f1f1f1;padding:10px;font-size:12px;max-height:200px;overflow-y:auto;">' . esc_html($recent_errors) . '</pre>';
}
$error_info .= '<br><strong>常见问题排查:</strong><br>';
// 检查是否是认证失败错误
if (strpos($debug_output, '526 Authentication failure') !== false || strpos($debug_output, 'Authentication failure') !== false) {
$error_info .= '<span style="color:red;"><strong>⚠️ 认证失败 (526错误) 解决方案:</strong></span><br>';
$error_info .= '• <strong>阿里云邮箱</strong>:必须使用授权码,不能使用登录密码<br>';
$error_info .= '• <strong>QQ邮箱</strong>需要开启SMTP服务并使用授权码<br>';
$error_info .= '• <strong>163邮箱</strong>:需要开启客户端授权密码<br>';
$error_info .= '• <strong>Gmail</strong>:需要开启两步验证并使用应用专用密码<br>';
$error_info .= '• 确认用户名格式正确(通常是完整邮箱地址)<br>';
$error_info .= '• 检查是否已在邮箱设置中开启SMTP服务<br><br>';
}
$error_info .= '• 检查SMTP服务器地址和端口是否正确<br>';
$error_info .= '• 验证用户名和密码是否正确<br>';
$error_info .= '• 确认加密方式SSL/TLS设置正确<br>';
$error_info .= '• 检查邮箱服务商是否允许SMTP发送<br>';
$error_info .= '• 确认服务器防火墙没有阻止SMTP端口<br>';
$error_info .= '• 检查服务器的PHP扩展是否支持SSL/TLS';
echo '<div class="notice notice-error is-dismissible"><p>' . $error_info . '</p></div>';
});
}
}
/**
* 测试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 '<div class="notice notice-error is-dismissible"><p><strong>SMTP配置不完整</strong>请先完成所有必要的SMTP配置。</p></div>';
});
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 '<div class="notice notice-success is-dismissible"><p><strong>SMTP连接测试成功</strong><br>成功连接到 ' . esc_html($smtp_host) . ':' . esc_html($smtp_port) . '<br>现在可以尝试发送测试邮件。</p></div>';
});
} else {
add_action('admin_notices', function() use ($smtp_host, $smtp_port, $error_message) {
echo '<div class="notice notice-error is-dismissible"><p><strong>SMTP连接测试失败</strong><br>无法连接到 ' . esc_html($smtp_host) . ':' . esc_html($smtp_port) . '<br>错误信息:' . esc_html($error_message) . '</p></div>';
});
}
}
// 处理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');
}
?>