如何在注册后的x天内发送电子邮件

时间:2017-09-29 07:11:34

标签: php wordpress email buddypress

在我们的社区,我们希望在注册后x天发送一些跟进电子邮件,例如使用我们平台的教程。

例如,注册后1天我们会教您如何做某事,第3天我们会向您发送另一封电子邮件,说明另一件事......所以我们可以跟进人员,因此他们不会感到放弃了社区。

我一直在寻找能够做到这一点但没有成功的插件。

所以我进入了编码部分,看看我是否可以做到。 使用CronJob和Wordpress外部的自定义脚本当然可以,但是当平台由没有编码知识的人管理时,这不是一个奇特的解决方案。我正在寻找可以在Wordpress的默认电子邮件部分添加电子邮件的内容。

我知道我应该发布一些我尝试过的东西,但遗憾的是我找不到任何解决办法或任何解决办法。

我们正在运行Wordpress + Buddypress + Learndash。

2 个答案:

答案 0 :(得分:0)

好吧,我们这里通常不建议插件。对于这种建议,请访问他们有 WordPress 标记的Software Recommendations网站。至于使用此 WordPress 标记的建议。

现在来看编码概念解决方案我们如何在一定时间后处理发送邮件。在这里,我建议你一个方法 -

function the_dramatist_handle_scheduled_mail( $arg_1 = '', $arg_2 = [] ) {
    wp_schedule_single_event(
        // Here time() is the time when this is firing and 259200s = 72h = 3d
        time() + 259200,
        // Declaring a hook at this point. You can hook any function to this point which you want to fire after three days of any base event.
        'the_dramatist_send_email_after_three_days',
        // You can add number of arguments to the hook also.
        [ $arg_1, $arg_2 ]
    );
    return true;

}

现在,在此the_dramatist_send_email_after_three_days挂钩处,您可以添加如下函数来发送电子邮件 -

add_action( 'the_dramatist_send_email_after_three_days', 'the_dramatist_send_email_after_three_days_function', 10, 2 );

function the_dramatist_send_email_after_three_days_function( $arg_1, $args_2 ) {

    $to = 'sendto@example.com';
    $user = get_user_by( 'email', $to );
    if ( 1 === get_user_meta( $user->ID, 'after_three_days_email', true ) ) {
        return false;
    }

    $subject = 'The subject';
    $body = 'The email body content';
    $headers = array('Content-Type: text/html; charset=UTF-8');

    $mail_sent = wp_mail( $to, $subject, $body, $headers );
    // After sending the email to every person I prefer to put a record.
    if ( $mail_sent ) {
        update_user_meta( $user->ID, 'after_three_days_email', 1 );

        return true;
    }
}

现在在您的基本事件之后,您只需使用参数调用the_dramatist_handle_scheduled_mail()。这样,您就可以通过wp_schedule_single_event()函数处理WordPress中的预定事件。

希望以上答案有所帮助。

答案 1 :(得分:0)

我建议扩展用户个人资料,添加一个有效期的字段。例如,以下示例使用字段_profile_extend_expires。然后,您可以使用meta_query过滤器来检查到期日。

此致 编



function member_expires_one_day() {

    $date_today = today_date();

    $date_to_expire = new DateTime($date_today);
    $date_to_expire->add(new DateInterval('P1D'));
    $expires = $date_to_expire->format('Y-m-d');

    $args = array(
        'meta_query' => array(
                    array(
                        'key' =>        '_profile_extend_expires',
                        'value' => $expires,
                        'compare' => '=='
                    ),
        )
    );

    $users = get_users( $args );

}