Wordpress wp_邮件功能发送电子邮件两次
我正在使用cron事件并添加一个PHP代码来向我的wordpress站点的用户发送电子邮件。但是由于某些原因,我的wp_邮件功能向所有用户发送了两次电子邮件 这是我的密码,谁能告诉我我的密码有什么问题吗Wordpress wp_邮件功能发送电子邮件两次,wordpress,Wordpress,我正在使用cron事件并添加一个PHP代码来向我的wordpress站点的用户发送电子邮件。但是由于某些原因,我的wp_邮件功能向所有用户发送了两次电子邮件 这是我的密码,谁能告诉我我的密码有什么问题吗 $args = array( 'role' => 'employee', 'order' => 'ASC' ); $all_users = get_users($args); foreach ($all_use
$args = array(
'role' => 'employee',
'order' => 'ASC'
);
$all_users = get_users($args);
foreach ($all_users as $user) {
$to = esc_html($user->user_email) ;
// Always set content-type when sending HTML email
$headers = "MIME-Version: 1.0" . "\r\n";
$headers .= "Content-type:text/html;charset=UTF-8" . "\r\n";
// More headers
$headers .= 'From: TEST <test@test.com>' . "\r\n";
$subject = "Regarding auto emails";
$message = "<p>Dear Employee/Consultant,</p>
<p >Please send in your submissions</p>";
wp_mail($to, $subject, $message, $headers);
}
$args=array(
“角色”=>“员工”,
“订单”=>“ASC”
);
$all_users=get_users($args);
foreach($user为所有用户){
$to=esc_html($user->user_email);
//发送HTML电子邮件时始终设置内容类型
$headers=“MIME版本:1.0”。\r\n”;
$headers.=“内容类型:text/html;字符集=UTF-8”。\r\n”;
//更多标题
$headers.='From:TEST'。“\r\n”;
$subject=“关于自动电子邮件”;
$message=“亲爱的员工/顾问
请提交您的意见书”;
wp_邮件($to、$subject、$message、$headers);
}
您可以用这种方式编写邮件函数
if(wp_mail($to, $subject, $message, $headers)){ // Condition check if mail has been sent
continue; // Continue to next iteration
}
我觉得你的PHP不错 我很幸运(复制如下)。通过在循环中构建邮件功能,如下所示:
<?php
$to = 'email@website.com';
$subj = 'Subject';
$body ='
<br/>
Dear Recipient,<br/>
<br/>
Here is an email.<br/>
<br/>';
$headers[] = 'Content-Type: text/html; charset=UTF-8';
$headers[] = 'From: Website.com <email@website.com>' . "\r\n";
wp_mail( $to, $subj, $body, $headers );
?>
您可以安装一个插件来验证cron事件是否已调度。如果您激活了插件并且插件工作正常,那么“custom_cron_event”(或您为函数选择的任何名称)应该作为计划的cron列出
通过插件的Cron计划:
<?php
/*
Plugin Name: Custom Plugin
Plugin URI:
Description: Adds function on cron
Author:
Version: 1.0
*/
/*
* When this plugin is activated, schedule
*/
function activate_custom_cron(){
wp_schedule_event( time(), 'daily', 'do_custom_cron_event');
}
register_activation_hook(__FILE__, 'activate_custom_cron');
/*
* When this plugin is deactivated, clear_schedule cron
*/
function deactivate_custom_cron(){
wp_clear_scheduled_hook('do_custom_cron_event');
}
register_deactivation_hook(__FILE__, 'deactivate_custom_cron');
/*
* Function
*/
function custom_cron_event() {
/*
* This is where your function goes
*/
}
add_action( 'do_custom_cron_event', 'custom_cron_event' );
?>
您是如何安排cron的?它有可能发射两次吗?Hello@RameshRamGopal,请接受asma_dev的回答作为结束问题的正确答案!