1

I have created a code through which the current user can add other users to his project. These users are only shown on the project page. They will not have any right to modify the project.

Once these users are inserted to database it sends an email to them. I want to restrict sending mail to the current user. How can I do that?

$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
  • it's not clear to me what you want to achieve compared to what happens now ( if something happens right now..) can you clarify? Commented Jul 23, 2018 at 10:21
  • @AndreaSomovigo.. I have created an add / remove fields in which I collect emails & other details of per-registered users. The data collected is inserted into database after inserting I email all these members. The first email will be always current user hence I don't want current user to get email. How to restrict current user from receiving mail.
    – Minesh
    Commented Jul 23, 2018 at 12:07

2 Answers 2

1

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 );
}
5
  • No its not working as $member_details->user_email; has more than 1 email. I think it will require foreach. But not getting how to do.
    – Minesh
    Commented Jul 23, 2018 at 12:41
  • I've edited my answer: use if($current_mail !== $_POST['user_email'] ) Commented Jul 23, 2018 at 13:13
  • $member_details->user_email; is an array that contains more than one email. How can we use this $current_mail !== $_POST['user_email']
    – Minesh
    Commented Jul 23, 2018 at 13:27
  • so $_POST['user_email'] is an array? and you're inserting multiple email values in the details of a single user? you should array_diff($member_details->user_email, Commented Jul 23, 2018 at 21:56
  • sorry the comment above was sent by mistake: $user_email = $member_details->user_email; //array as stated $arr_current=array($current_mail) // create array with the current user_email; $to_send=array_diff($user_email, $arr_current); //exclude the current user email comparing arrays wp_mail( $to_send, $subject, $message ); Commented Jul 23, 2018 at 22:02
0

I have found the answer. Here it is:

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 );

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.