title: WP send mail notification to new user created programmatically
body: <p>I have a script to create a new wordpress user by checking the email and password in a external database. I have a script in place to enable login in with the email address.</p>
  
  <p>The script to add the users from the external database works as intend, but the notification emails don't.</p>
  
  <p>I've tried (the password is stored in plain text in the external db - I know it's not recommended at all):</p>
  
  <pre><code>wp_new_user_notification($new_user_id, $result-&gt;det-&gt;USR_PASSWORD);
  wp_new_user_notification($user-&gt;ID, $result-&gt;det-&gt;USR_PASSWORD);
  wp_new_user_notification($user-&gt;data-&gt;ID, $result-&gt;det-&gt;USR_PASSWORD);
  </code></pre>
  
  <p>Nothing seems to work. Here is the full script I use:</p>
  
  <pre><code>add_filter('authenticate', 'uj_auth', 20, 3);
  
  function uj_auth( $user, $username, $password ){
   if ( is_a($user, 'WP_User') ) { return $user; } //check if existing user
   if ( empty($username) || empty($password) ) {
    $error = new WP_Error();
    if ( empty($username) )
      $error-&gt;add('empty_username', __('&lt;strong&gt;ERROR&lt;/strong&gt;: The username field is empty.'));
    if ( empty($password) )
      $error-&gt;add('empty_password', __('&lt;strong&gt;ERROR&lt;/strong&gt;: The password field is empty.'));
  return $error;
   }
  
  $userdata = get_user_by('login', $username);
  
  if ( !$userdata ): //if not an existing user
  
  $ch = curl_init();
  
  curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
  curl_setopt($ch, CURLOPT_URL,"http://webstite.com");
  curl_setopt($ch, CURLOPT_POST, true);
  curl_setopt($ch, CURLOPT_FORBID_REUSE, true);
  curl_setopt($ch, CURLOPT_FRESH_CONNECT, true);
  curl_setopt($ch, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_1);  
  curl_setopt($ch, CURLOPT_POSTFIELDS,"apikey=apikey&amp;username=$username&amp;password=$password");
  
  $json = curl_exec ($ch);
  curl_close ($ch); 
  $result = json_decode($json); //the result array from the external api
  
  if ($result-&gt;result == 'OK'): //if user and pass match those in database result is OK
      //Verify if the username is not NULL. User user-id instead
      if(is_null($result-&gt;det-&gt;USR_USERNAME)) $result-&gt;det-&gt;USR_USERNAME = $result-&gt;det-&gt;USR_ID;
    // Setup the required user information
      $userdata = array( 
            'user_email' =&gt; $result-&gt;det-&gt;USR_EMAIL,
            'user_pass' =&gt; $result-&gt;det-&gt;USR_PASSWORD,
            'user_login' =&gt; $result-&gt;det-&gt;USR_USERNAME,
            'first_name' =&gt; $result-&gt;det-&gt;USR_PRENUME,
            'last_name' =&gt; $result-&gt;det-&gt;USR_NUME
            );
    // A new user has been created
      $new_user_id = wp_insert_user( $userdata );
    // Load the new user info
      $user = new WP_User ($new_user_id);
  
    // Email the user
      wp_new_user_notification($new_user_id, $result-&gt;det-&gt;USR_PASSWORD);
  
     else:
  
    return new WP_Error('invalid_username', sprintf(__('&lt;strong&gt;ERROR&lt;/strong&gt;: Utilizator și/sau parolă introduse greșit sau inexistente. &lt;a href="'.site_url('inregistrare').'" title="Inregistreaza-te"&gt;Inregistrează-te!&lt;/a&gt;')));
  
     endif;
  
  else:
  
  $userdata = apply_filters('wp_authenticate_user', $userdata, $password);
  if ( is_wp_error($userdata) )
      return $userdata;
  
  if ( !wp_check_password($password, $userdata-&gt;user_pass, $userdata-&gt;ID) )
      return new WP_Error( 'incorrect_password', sprintf( __( '&lt;strong&gt;ERROR&lt;/strong&gt;: The password you entered for the username &lt;strong&gt;%1$s&lt;/strong&gt; is incorrect. &lt;a href="%2$s" title="Password Lost and Found"&gt;Lost your password&lt;/a&gt;?' ),
      $username, wp_lostpassword_url() ) );
  
  $user =  new WP_User($userdata-&gt;ID);
  
  endif;
  
  //remove_action('authenticate', 'wp_authenticate_username_password', 20);
  
  return $user;
  
  }
  </code></pre>
  
  <p>So what am I doing wrong?</p>