如何为订阅到期前 7 天发送可编辑的邮件模板?

解读

国内 WordPress 面试中,这道题考察的是“把业务需求拆成技术动作”的能力:

  1. 识别“订阅”——国内主流场景是 WooCommerce Subscriptions、MemberPress 或自研 CPT+Meta;
  2. 识别“到期前 7 天”——需要定时任务与时间窗口计算;
  3. 识别“可编辑”——运营同事要在后台无需改代码即可改文案;
  4. 识别“邮件模板”——必须走WordPress 邮件钩子,兼容 SMTP 与国内云邮件(阿里云 DirectMail、SendCloud)。
    答不出“如何让用户改模板”或“如何防止重复发送”,都会直接减分。

知识点

  • WP-Cron 与服务器真 Cron:国内高并发站点必须关闭 WP-Cron,用宝塔或阿里云定时 wget 触发,防止 502 时漏发。
  • Action Scheduler:Automattic 开源的队列库,已内置在 WooCommerce,可可靠异步发送,比 WP-Cron 稳定。
  • post_meta 日期计算:MySQL 8.0 以下时区容易出错,必须用gmt 时间戳比对,禁止用 post_date。
  • wp_mail() 与 phpmailer_init 钩子:国内邮件服务商要求 SMTP 鉴权,需在 phpmailer_init 里动态注入 AccessKey。
  • get_locale() 与 i18n:国内站点常中英双语,模板要支持 () 函数,邮件标题记得 esc_html()。
  • template_override 机制:把模板文件放进主题文件夹 /subscription-email/,插件里通过locate_template() 优先加载,实现“可编辑”。
  • wp_mail_content_type 过滤器:强制 text/html,否则 QQ 邮箱会丢换行。
  • 重复发送锁:使用wp_cache_set() 加 24h TTL 或 uniqid token 写入 post_meta,防止 Action Scheduler 重试时骚扰用户。
  • 单元测试:使用 WP_UnitTestCase + Brain\Monkey 模拟 wp_mail(),断言邮件标题包含“即将到期”四个字,国内 CI 用码云 Gitee Go 跑。

答案

分五步落地,代码可直接搬进面试白板:

  1. 注册每天凌晨的 Action Scheduler 队列
add_action('init', function(){
    if (!as_next_scheduled('daily_subscribe_check'))
        as_schedule_recurring_action(time(), DAY_IN_SECONDS, 'daily_subscribe_check');
});
  1. 查询“正好 7 天后到期”的订阅
add_action('daily_subscribe_check', function(){
    global $wpdb;
    $seven_days = strtotime('+7 days', current_time('timestamp', true));
    $ids = $wpdb->get_col($wpdb->prepare(
        "SELECT post_id FROM {$wpdb->postmeta}
         WHERE meta_key='_subscription_end' AND meta_value=%d",
        $seven_days
    ));
    foreach ($ids as $id) {
        if (!get_post_meta($id, '_reminder_sent', true)) {   // 重复发送锁
            as_enqueue_async_action('send_subscribe_reminder', ['id'=>$id]);
        }
    }
});
  1. 发送邮件,模板可编辑
add_action('send_subscribe_reminder', function($id){
    $user = get_user_by('id', get_post_field('post_author', $id));
    $subject = __('您的订阅将在7天后到期', 'tx_domain');
    // 让主题可覆盖模板
    $template = locate_template('subscription-email/reminder.php');
    if (!$template) $template = plugin_dir_path(__FILE__).'templates/reminder.php';
    ob_start();
    include $template;
    $body = ob_get_clean();
    wp_mail($user->user_email, $subject, $body, ['Content-Type: text/html; charset=UTF-8']);
    update_post_meta($id, '_reminder_sent', '1');
});
  1. 默认模板示例(templates/reminder.php)
<?php
/* 用户可在主题 /subscription-email/reminder.php 里任意修改 */
printf(
    '<p>尊敬的 %s,您的订阅 %s 将于 %s 到期,请及时续费。</p>',
    esc_html($user->display_name),
    esc_html(get_the_title($id)),
    date_i18n(get_option('date_format'), get_post_meta($id,'_subscription_end',true))
);
?>
  1. 后台加一条“邮件模板”子菜单,把模板路径写成文件编辑器,用file_get_contents+file_put_contents 实现轻量级在线改模板,权限限定给 shop_manager 以上角色,避免审计漏洞。

拓展思考

  • 如果订阅量日活 10w+,Action Scheduler 单表会爆炸,需按月份分表或迁移到阿里云 MQ;面试可提“用 wpdb->insert 把任务推到自建队列表,再让 Python 消费发邮件”,体现跨语言架构视野。
  • 国内法规要求商业邮件加退订链接,可在模板底部加 /my-account/notification-preferences/,并在 user_meta 里记“unsubscribe_reminder”,发送前检查,避免被腾讯邮箱批量举报。
  • 若要多通道(短信+邮件),可集成阿里云短信:在 same Action 里先判断 user_meta 手机是否验证,再调用 SendSms 接口,实现双通道冗余,面试提到“短信模板需在阿里云备案”可加分。