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


  • 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

1 Answer 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' );

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.