每天使用`wp_mail`发送电子邮件

时间:2016-06-27 作者:Lalit Panwar

我想每天中午12:54使用wp_mail 功能,但它可以连续发送邮件。

请告诉我怎么了?

<?php $timeg = date( \'g:ia\', current_time( \'timestamp\', 0 ));
$timem = \'12:54pm\';
if ($timem == $timeg) 
{

$to = \'[email protected]\';
$subject = \'Post Published by Author\';
$headers = \'From: admin <[email protected]>\' . "\\r\\n";  
$message = \'your\';

   wp_mail($to, $subject, $message, $headers);

}

?>

1 个回复
SO网友:cybmeta

我将探索在WorPdress中使用预定事件,称为wp-cron.

下面是一个例子(未经测试,只是作为概念证明写在这里):

// 1.- Register custom escheduled event on plugin activiation
register_activation_hook( __FILE__, \'cyb_plugin_activation\' );
function cyb_plugin_activation() {

    if( ! wp_next_scheduled( \'cyb_daily_cron_job\' ) ) {

        // Set first run time to today at 12:54 (or tomorrow at that time)
        $first_run_time = new DateTime( \'12:54\', new DateTimeZone( get_option( \'timezone_string\' ) ) );
        $current_time = new DateTime(\'now\', new DateTimeZone( get_option( \'timezone_string\' ) ) );

        if( $current_time->getTimestamp() > $first_run_time->getTimestamp() ){

            $first_run_time->modify(\'+1 day\');

        }

        wp_schedule_event( first_run_time->getTimestamp(), \'daily\', \'cyb_daily_cron_job\' );

    }

}

// 2.- Hook a function to our scheduled event
add_action( \'cyb_daily_cron_job\', \'cyb_send_daily_mail\' );
function cyb_send_daily_mail() {

    $to = \'[email protected]\';
    $subject = \'Post Published by Author\';
    $headers = \'From: admin <[email protected]>\' . "\\r\\n";  
    $message = \'your\';

    wp_mail($to, $subject, $message, $headers);

}

// 3.- Remove custom escheduled event on plugin deactiviation
register_deactivation_hook( __FILE__, \'cyb_plugin_deactivation\' );
function cyb_plugin_deactivation() {
    wp_clear_scheduled_hook( \'cyb_daily_cron_job\' );
}

相关推荐

Throttling email frequency

我有一个工作委员会网站,运行着wp job manager,通常发送电子邮件的次数很低,但偶尔一个操作会触发数百名收件人的电子邮件更新,以了解列表状态的变化或雇主在申请人列表上的状态变化。我目前通过Dreamhost使用WP Mail SMTP发送。Dreamhost的电子邮件限制为每小时100封。由于这些状态更新和来自该站点的大多数电子邮件都不太需要时间,我正在寻找一种解决方案,即将电子邮件指令排队,每分钟只发送一封电子邮件,和/或计算过去人力资源中发送的数量,当达到某个上限时,延迟队列,然后重新开始。