admin管理员组

文章数量:1410730

I have a problem with a cron schedule function, it does not want to send an email with the wp_mail() function.

This is my code:

It is placed in the functions.php

<?php

    add_filter('cron_schedules', 'add_review_invites_schedule');
    function add_review_invites_schedule($schedules){
        $schedules['send_review_invites_interval'] = array(
                'interval' => 5*60,
                'display' => __('Every Five Minutes'),
        );
        return $schedules;
    }

   if (!wp_get_schedule('send_review_invites_event')) {
       wp_schedule_event( time(), 'send_review_invites_interval', 'send_review_invites_event');
   }

   add_action( 'send_review_invites_event', 'send_review_invites' );

   function send_review_invites(){
       echo 'test';
       wp_mail( '[email protected]', 'Automatic email', 'Automatic scheduled email from WordPress to test cron');
   }

?>

Some things you should know I have connected the wp-cron to my server cron so it runs this code on my website. I have also put define('DISABLE_WP_CRON', false); in the wp-config.php.

The output I get in my mail when the server cron runs, echos test. This shows me that the function send_review_invites() is being run. Yet somehow I don't get another email on the email address given in the wp_mail() function.

Thanks in advance for your help.

I have a problem with a cron schedule function, it does not want to send an email with the wp_mail() function.

This is my code:

It is placed in the functions.php

<?php

    add_filter('cron_schedules', 'add_review_invites_schedule');
    function add_review_invites_schedule($schedules){
        $schedules['send_review_invites_interval'] = array(
                'interval' => 5*60,
                'display' => __('Every Five Minutes'),
        );
        return $schedules;
    }

   if (!wp_get_schedule('send_review_invites_event')) {
       wp_schedule_event( time(), 'send_review_invites_interval', 'send_review_invites_event');
   }

   add_action( 'send_review_invites_event', 'send_review_invites' );

   function send_review_invites(){
       echo 'test';
       wp_mail( '[email protected]', 'Automatic email', 'Automatic scheduled email from WordPress to test cron');
   }

?>

Some things you should know I have connected the wp-cron to my server cron so it runs this code on my website. I have also put define('DISABLE_WP_CRON', false); in the wp-config.php.

The output I get in my mail when the server cron runs, echos test. This shows me that the function send_review_invites() is being run. Yet somehow I don't get another email on the email address given in the wp_mail() function.

Thanks in advance for your help.

Share Improve this question asked Apr 26, 2019 at 9:05 nvelinznvelinz 131 silver badge5 bronze badges 2
  • is your server correctly configured to send emails? – Vishwa Commented Apr 26, 2019 at 9:07
  • @Vishwa yes, it is – nvelinz Commented Apr 26, 2019 at 11:18
Add a comment  | 

2 Answers 2

Reset to default 0

You can test if mail has been sent or not using the following:

// Set $to as the email you want to send the test to.
$to = "[email protected]";

// Email subject and body text.
$subject = 'wp_mail function test';
$message = 'This is a test of the wp_mail function: wp_mail is working.woo!';
$headers = '';

// Load WP components, no themes.
define('WP_USE_THEMES', false);
require('wp-load.php');

// send test message using wp_mail function.
$sent_message = wp_mail( $to, $subject, $message, $headers );

//display message based on the result.
if ( $sent_message ) {
    // The message was sent.
    echo 'The test message was sent. Check your email inbox.';
} 
else {
    // The message was not sent.
    echo 'The message was not sent!';
}

Check whether your server is configured correctly to send emails, which wp_mail() requires. When in doubt, you can also try using SMTP to confirm it. By using the SMTP method, if it delivers the mail then your problem is with your mailserver.

In addition to @Vishwa answer you could log the wp_mail() function according to this link: Wordpress reference

add_action('wp_mail_failed', 'log_mailer_errors', 10, 1);
function log_mailer_errors( $wp_error ){
  $fn = ABSPATH . '/mail.log'; // say you've got a mail.log file in your server root
  $fp = fopen($fn, 'a');
  fputs($fp, "Mailer Error: " . $wp_error->get_error_message() ."\n");
  fclose($fp);
}

本文标签: phpCron not sending wpmail()