I have following piece of code, which inserts usernames and other details of users in the database. After inserting the usernames I want to email them using wp_mail();
. I am unable to do so. How can I do this?
$member_details->user_login = array_map( 'sanitize_text_field', $_POST['user_login'] );
$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'
)
);
2 Answers
Assuming that $member_details->user_login
is already a user in the wp_users
table, then you could use the following to get their email address:
$user = get_user_by( $member_details->user_login, 'login' );
From there, you have the user’s email address and can use wp_mail()
to email them:
$subject = "My email subject";
$message = "My message body...";
wp_mail( $user->user_email, $subject, $message );