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
2 Answers
Reset to default 0You 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()
版权声明:本文标题:php - Cron not sending wp-mail() 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1744921276a2632307.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论