0

I have create a code through which current user can add other users in 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 in database it sends email to them. I want to restrict sending mail to the current user. How to do?

$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 info@gmail.com';
wp_mail( $user_email, $subject, $message );

Please help me. Thanks...

  • it's not clear to me what you want to achieve compared to what happens now ( if something happens right now..) can you clarify? – Andrea Somovigo Jul 23 '18 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 Jul 23 '18 at 12:07
1

Perhaps:

//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 info@gmail.com';
  wp_mail( $user_email, $subject, $message );
}
  • 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 Jul 23 '18 at 12:41
  • I've edited my answer: use if($current_mail !== $_POST['user_email'] ) – Andrea Somovigo Jul 23 '18 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 Jul 23 '18 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, – Andrea Somovigo Jul 23 '18 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 ); – Andrea Somovigo Jul 23 '18 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 mineshrai@gmail.com';

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

}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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