我对cron调度函数有问题,它不想用wp_mail()函数发送电子邮件。
This是我的代码:
它放在functions.php中。
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');
}
?>有些事情你应该知道,我已经把wp连接到我的服务器cron,所以它在我的网站上运行这个代码。我还将define('DISABLE_WP_CRON', false);放在wp-config.php中。
当服务器cron运行时,我在邮件中获得的输出会回显test。这显示了正在运行的函数send_review_invites()。然而,不知何故,我没有收到关于wp_mail()函数中给出的电子邮件地址的另一封邮件。
提前谢谢你的帮助。
发布于 2019-04-28 08:08:23
您可以使用以下方法测试邮件是否已发送:
// 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!';
}检查您的服务器是否被正确配置为发送电子邮件,这是wp_mail()所要求的。如果有疑问,也可以尝试使用SMTP来确认它。通过使用SMTP方法,如果它传递邮件,那么您的邮件服务器就会出现问题。
发布于 2019-04-28 09:20:33
除了@Vishwa答案之外,您还可以根据以下链接记录wp_mail()函数:Wordpress参考
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);
}https://wordpress.stackexchange.com/questions/336389
复制相似问题