You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何为WordPress后台新用户注册邮件添加发送日志

实现新用户注册邮件的发送日志记录

嘿,你这个需求完全可以通过WordPress的钩子系统来实现,核心是要捕获邮件发送的成功/失败状态,同时结合你已经在用的wp_new_user_notification_email过滤器来关联新用户的信息。下面给你分步讲解具体的实现方案:

一、核心思路

WordPress本身提供了wp_mail_succeeded和wp_mail_failed两个动作钩子(WP 5.4+版本引入),可以直接捕获邮件发送的状态。我们只需要:

  1. 在自定义新用户邮件时,给邮件添加一个唯一标记,方便后续识别这是新用户注册通知邮件;
  2. 暂存新用户的相关信息(收件人、邮件内容、发送时间等);
  3. 利用上述两个钩子捕获发送状态,将完整数据写入日志文件。

二、具体代码实现

1. 自定义新用户邮件并添加标记

首先,在你已有的wp_new_user_notification_email过滤器中,添加一个自定义邮件头作为标记,同时暂存邮件相关数据:

add_filter('wp_new_user_notification_email', 'custom_new_user_email', 10, 3);
function custom_new_user_email($wp_new_user_notification_email, $user, $blogname) {
    // 你的自定义邮件主题和内容(保留你原来的逻辑)
    $wp_new_user_notification_email['subject'] = '欢迎加入' . $blogname . '!';
    $wp_new_user_notification_email['message'] = "Hi {$user->user_login},\n\n欢迎注册我们的网站,你的账户已创建完成。";

    // 添加自定义邮件头,标记这是新用户注册邮件
    $wp_new_user_notification_email['headers'][] = 'X-New-User-Notification: true';

    // 暂存当前新用户邮件的核心数据到全局变量
    global $current_new_user_email_data;
    $current_new_user_email_data = [
        'user_id' => $user->ID,
        'to' => $wp_new_user_notification_email['to'],
        'subject' => $wp_new_user_notification_email['subject'],
        'message' => $wp_new_user_notification_email['message'],
        'sent_time' => current_time('mysql') // 按MySQL格式记录时间
    ];

    return $wp_new_user_notification_email;
}

2. 捕获发送状态并写入日志

接下来,利用wp_mail_succeeded和wp_mail_failed钩子分别处理成功和失败的情况,调用日志写入函数:

// 邮件发送成功时记录日志
add_action('wp_mail_succeeded', 'log_new_user_email_success', 10, 1);
function log_new_user_email_success($to) {
    global $current_new_user_email_data;
    // 验证是否是当前暂存的新用户邮件
    if (!isset($current_new_user_email_data) || $current_new_user_email_data['to'] !== $to) {
        return;
    }

    // 标记成功状态并写入日志
    $current_new_user_email_data['status'] = 'success';
    write_new_user_email_log($current_new_user_email_data);
    
    // 清空全局变量,避免干扰其他邮件
    unset($current_new_user_email_data);
}

// 邮件发送失败时记录日志
add_action('wp_mail_failed', 'log_new_user_email_failure', 10, 1);
function log_new_user_email_failure($wp_error) {
    global $current_new_user_email_data;
    if (!isset($current_new_user_email_data)) {
        return;
    }

    // 标记失败状态并记录错误信息
    $current_new_user_email_data['status'] = 'failed';
    $current_new_user_email_data['error_message'] = $wp_error->get_error_message();
    write_new_user_email_log($current_new_user_email_data);
    
    // 清空全局变量
    unset($current_new_user_email_data);
}

// 日志写入函数
function write_new_user_email_log($log_data) {
    // 日志文件保存路径(建议放在uploads目录,确保目录可写)
    $log_file = WP_CONTENT_DIR . '/uploads/new_user_email_logs.log';

    // 格式化日志内容
    $log_entry = sprintf(
        "[%s] 用户ID: %d | 收件人: %s | 状态: %s | 邮件主题: %s\n邮件内容:\n%s\n",
        $log_data['sent_time'],
        $log_data['user_id'],
        $log_data['to'],
        $log_data['status'],
        $log_data['subject'],
        $log_data['message']
    );

    // 如果有错误信息,追加到日志中
    if (isset($log_data['error_message'])) {
        $log_entry .= "错误信息: " . $log_data['error_message'] . "\n";
    }

    // 分割线,方便区分不同日志条目
    $log_entry .= "----------------------------------------\n";

    // 写入日志(FILE_APPEND追加内容,LOCK_EX避免并发写入冲突)
    file_put_contents($log_file, $log_entry, FILE_APPEND | LOCK_EX);
}

三、兼容旧版本WordPress(低于5.4)

如果你的WP版本低于5.4,没有wp_mail_succeeded和wp_mail_failed钩子,可以通过重写wp_mail函数来捕获发送状态:

// 先保存原始的wp_mail函数引用
if (!function_exists('original_wp_mail')) {
    function original_wp_mail($to, $subject, $message, $headers = '', $attachments = array()) {
        return wp_mail($to, $subject, $message, $headers, $attachments);
    }
}

// 重写wp_mail函数,捕获发送结果
add_filter('wp_mail', 'custom_wp_mail', 999, 5);
function custom_wp_mail($to, $subject, $message, $headers = '', $attachments = array()) {
    // 调用原始函数发送邮件并获取结果
    $send_result = original_wp_mail($to, $subject, $message, $headers, $attachments);

    // 检查是否是新用户注册邮件(通过自定义邮件头判断)
    $is_new_user_email = false;
    if (is_array($headers)) {
        foreach ($headers as $header) {
            if (strpos($header, 'X-New-User-Notification: true') !== false) {
                $is_new_user_email = true;
                break;
            }
        }
    } elseif (is_string($headers)) {
        if (strpos($headers, 'X-New-User-Notification: true') !== false) {
            $is_new_user_email = true;
        }
    }

    if ($is_new_user_email) {
        global $current_new_user_email_data;
        $log_data = $current_new_user_email_data;
        $log_data['status'] = $send_result ? 'success' : 'failed';
        
        // 如果发送失败,尝试获取错误信息
        if (!$send_result) {
            $last_error = error_get_last();
            $log_data['error_message'] = $last_error ? $last_error['message'] : '未知错误';
        }

        write_new_user_email_log($log_data);
        unset($current_new_user_email_data);
    }

    return $send_result;
}

四、注意事项

  • 权限问题:确保wp-content/uploads目录有写入权限,否则日志无法生成;
  • 安全问题:日志文件包含用户邮箱等敏感信息,建议通过.htaccess限制访问,或者将日志文件放在非公开目录;
  • 测试验证:创建一个测试用户,检查uploads/new_user_email_logs.log文件是否生成了对应的日志条目;
  • SMTP插件兼容:如果使用了WP Mail SMTP等邮件插件,上述代码依然有效,因为这些插件都是基于WordPress原生的wp_mail函数扩展的。

内容的提问来源于stack exchange,提问作者samjhana joshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:28:29