0

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( 'me@example.com', '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.

  • is your server correctly configured to send emails? – Vishwa Apr 26 at 9:07
  • @Vishwa yes, it is – nvelinz Apr 26 at 11:18
0

You can test if mail has been sent or not using this,

// Set $to as the email you want to send the test to.
$to = "you@yoursite.com";

// 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 has correctly configured to send emails, which wp_mail requires. if in doubt, you can also try using smtp to confirm it. by using smtp method, you can make sure if it delivers the mail then your problem is with your mailserver.

0

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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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