我想使用wp_mail
向我发送一些数据,但这只能一次完成。
为了避免持续发送邮件,我创建了一个额外的option。
但是,当我测试它时,它仍然不断重复发送相同的邮件。
我的代码如下:
function track_details_admin() {
if( ! current_user_can('administrator') && ! is_admin ) {
return false;
}
$tracking = get_option('track_details');
// mail settings
$send_mail = wp_mail( $to, $subject, $message, $headers );
// Send mail only if it wasn't sent before
if( $tracking === false && $tracking != '1' ) {
$send_mail;
}
// If email is sent succesfully update option so it won't be sent again
if( $send_mail ) {
//If option already exists, update else create new
if ( $tracking !== false ) {
update_option( 'track_details', '1' );
} else {
add_option( 'track_details', '1' );
}
}
}
add_action( 'admin_init', 'track_details_admin', 99 );
在选项表中,我可以看到在发送邮件之后创建了选项'track_details'
并将其设置为1
。
在正确方向上的任何帮助都将受到赞赏。
答案 0 :(得分:0)
您可以尝试以下脚本,该脚本具有不同的方法来检测邮件是否发送。我实际上使用的变量$is_sent
的值为0
,但是一旦发送电子邮件或if语句为true,则$is_sent
的值将被更新并变为1
function track_details_admin() {
if( ! current_user_can('administrator') && ! is_admin ) {
return false;
}
$tracking = get_option('track_details');
// mail settings
$send_mail = wp_mail( $to, $subject, $message, $headers );
$is_sent = 0;
// Send mail only if it wasn't sent before
if( $tracking === false && $tracking != '1' ) {
$send_mail;
$is_sent = 1;
}
// If email is sent succesfully update option so it won't be sent again
if( $is_sent == 1 ) {
//If option already exists, update else create new
if ( $tracking !== false ) {
update_option( 'track_details', '1' );
} else {
add_option( 'track_details', '1' );
}
}
}
add_action( 'admin_init', 'track_details_admin', 99 );