First, add a custom Action Hook that is triggered whenever the user is updated.
For example, when custom meta fields are updated in WordPress, some action hooks such as: updated_{$meta_type}_meta, updated_postmeta, etc.
So if you're using the ACF plugin, you can hook into the 'acf/save_post' action hook. The action hook lets you do something before or after the custom field are being updated. For more info on using the acf/save_post, click here.
Next, you can now send the email to the user using the wp_mail() function.
Based on the code you have provided, I made some refactoring:
add_action(‘acf/save_post’, ‘my_save_post’);
function my_save_post( $post_id ) {
// create some logic here to check if you are editing a user
// Keep an eye on this pagenow check to see if it's correct
// Use wp_die( var_dump( $pagenow ); to debug
global $pagenow;
if ($pagenow == ‘user-edit.php’) {
//get the value of the field
$value = get_field(‘login_allowed’,$post_id);
// Remove this, just here for debugging
// wp_die( var_dump( $value ) );
// check if the checkbox is filled
if ( $value == 'unchecked' ) {
return false;
}
// Company information
$email = “removed”;
$name = “removed”;
// Debug: I see $useremail variable, is it set somewhere?
//get user's email
$user = get_user_by('email', $useremail);
if ($user) {
$details['email'] = sanitize_email( $user->user_email );
// email data
$to = $useremail;
$subject = 'The subject';
$body = 'The email body content';
$headers = ‘From:‘ . $name . ‘ ’ . “\r\n”;
// send email
wp_mail($to, $subject, $body, $headers );
}
}
}