限制当前用户使用wp_mail接收电子邮件

时间:2018-07-22 作者:Minesh

我创建了一个代码,当前用户可以通过该代码将其他用户添加到他的项目中。这些用户仅显示在项目页面上。他们无权修改该项目。

一旦这些用户被插入数据库,它就会向他们发送一封电子邮件。我想限制向当前用户发送邮件。我该怎么做?

$member_details->user_email = array_map( \'sanitize_text_field\', $_POST[\'user_email\'] );
$member_details->user_role = array_map( \'sanitize_text_field\', $_POST[\'user_role\'] );
$member_details->status = array_map( \'sanitize_text_field\', $_POST[\'status\'] );

$member_details_encode = wp_json_encode( $member_details );

  global $wpdb;

  $member_result = $wpdb->insert( \'wpxa_project_members\',

         array( 

              \'project_id\'     => $_SESSION[\'project_id\'],
              \'author_id\'      => $post_author,
              \'member_details\' => $member_details_encode

              ),

         array( 

              \'%d\',
              \'%d\',
              \'%s\'

      )

    );

$user_email = $member_details->user_email;
$subject = "Congrats! You are added to the Project  -  " . "\'" . $project_title . "\'";
$message = \'If you are not the member of project plz contact us to remove at [email protected]\';
wp_mail( $user_email, $subject, $message );

2 个回复
最合适的回答,由SO网友:Minesh 整理而成

我找到了答案。这是:

if ( ! empty( $member_result ) ) {

$user_emails = $member_details->user_email;

for ($i=1; $i < count($user_emails); $i++) { 
    $user_email[] = $user_emails[$i];
}

$to = $user_email;
$subject = "Congrats! You are added to the Project  -  " . "\'" . $project_title . "\'";
$message = \'If you are not the member of project plz contact us to remove at [email protected]\';

wp_mail( $to, $subject, $message );

}

SO网友:Andrea Somovigo

Try this:

//after $wpdb->insert function

$current_user =  wp_get_current_user();
$current_mail = $current_user->user_email;
//$user_email = $member_details->user_email;
if($current_mail !== $_POST[\'user_email\'] ){ // send mail only if different from the current_user email
  $subject = "Congrats! You are added to the Project  -  " . "\'" . 
  $project_title . "\'";
  $message = \'If you are not the member of project plz contact us to remove at [email protected]\';
  wp_mail( $user_email, $subject, $message );
}
结束

相关推荐