最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

wp mail - Send email daily using `wp_mail`

programmeradmin6浏览0评论

I want to send email daily 12:54pm using wp_mail function, but it send mail continuously.

Plz suggest what is wrong?

<?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);

}

?>

I want to send email daily 12:54pm using wp_mail function, but it send mail continuously.

Plz suggest what is wrong?

<?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);

}

?>
Share Improve this question edited Jun 27, 2016 at 7:36 Pieter Goosen 55.4k23 gold badges115 silver badges209 bronze badges asked Jun 27, 2016 at 7:31 Lalit PanwarLalit Panwar 12 bronze badges 3
  • are you sure that you call this code everyday at 12:54pm ? – mmm Commented Jun 27, 2016 at 7:48
  • 1 Don't try to match the exact time using php only. It's inefficient and will cost you too much resources to accomplish Instead you should set up a cronjob. See threads like drupal.stackexchange.com/questions/73627/… and unix.stackexchange.com/questions/48538/… for details – Z. Zlatev Commented Jun 27, 2016 at 8:31
  • You're matching the minute only, so every time the code is run that minute it'll send, not just the first time. So (depending where you put this) I'd guess that's why you see it sending mail continuously. But as others have said a scheduled job is the answer. – Rup Commented Jun 27, 2016 at 15:42
Add a comment  | 

1 Answer 1

Reset to default 1

I would explore the use of scheduled events in WorPdress, known as wp-cron.

Here is a example for that (not tested, just written here as proof of concept):

// 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' );
}
发布评论

评论列表(0)

  1. 暂无评论