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

WordPress中如何给X天未登录用户发邮件并每周重复发送?

WordPress 未登录用户重复提醒邮件实现方案

1. 新增用户发送状态存储

使用WordPress内置的usermeta表存储两个关键记录,用于后续发送规则判断:

  • last_reminder_sent:存储上一次提醒邮件发送的Unix时间戳
  • reminder_send_count:累计已发送的提醒邮件数量,默认值为0

在你已有的首封邮件发送逻辑中,发送成功后必须写入这两条元数据:首封发送后将reminder_send_count设为1,last_reminder_sent写入当前时间戳。

2. 注册每周执行的定时任务

WordPress自带WP Cron机制可实现周期调度,将以下代码加入主题functions.php或自定义插件中:

// 注册每周的调度间隔(WordPress默认不带每周间隔,需手动新增)
add_filter('cron_schedules', function ($custom_schedules) {
    $custom_schedules['weekly'] = [
        'interval' => 604800, // 7天对应的秒数
        'display' => '每周执行一次'
    ];
    return $custom_schedules;
});

// 注册定时事件,避免重复注册
if (!wp_next_scheduled('weekly_login_reminder_cron')) {
    wp_schedule_event(time(), 'weekly', 'weekly_login_reminder_cron');
}

// 绑定定时任务执行的回调函数
add_action('weekly_login_reminder_cron', 'send_weekly_reminder_emails');

如果站点访问量较低,可配置服务器系统级Cron定时请求站点根目录的wp-cron.php文件,避免WP Cron延迟触发。

3. 编写重复发送判断逻辑

在上述回调函数send_weekly_reminder_emails中实现发送规则校验:

  • 首先筛选出所有满足「X天未登录」条件的用户列表
  • 遍历用户列表,读取对应用户的last_reminder_sent、reminder_send_count元数据
  • 计算当前时间与last_reminder_sent的差值,差值≥604800秒才满足发送间隔要求
  • 可自行配置最大发送次数上限(例如最多发送4次),超过上限则不再发送
  • 若用户在此期间已登录,直接清空对应的两条元数据,终止后续提醒
  • 邮件发送成功后,更新last_reminder_sent为当前时间戳,reminder_send_count数值+1

4. 优化建议

  • 批量发送邮件时拆分批次执行,单次最多发送20-50封,避免执行超时或触发邮件服务商的频率限制
  • 增加发送失败重试逻辑,发送失败时不更新发送时间,下次定时任务触发时可再次尝试

内容的提问来源于stack exchange,提问作者Ken Clay

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.01 12:36:03