title: Drupal 8 - how to create a user account programmatically and validating the email address later
body: <p>I developed a custom module to create users using the business rules module. This works well.</p>
  
  <pre><code>$lang = \Drupal::languageManager()-&gt;getCurrentLanguage()-&gt;getId();
          $user = \Drupal\user\Entity\User::create();
  
          // The Basics
          $user-&gt;setUsername(str_replace('@','',$mail));  
          $user-&gt;setPassword(user_password());
          $user-&gt;setEmail($mail);
          $user-&gt;enforceIsNew();  
          $user-&gt;set("init", $mail);
          $user-&gt;set("langcode", $lang);
          $user-&gt;set("preferred_langcode", $lang);
          $user-&gt;set("preferred_admin_langcode", $lang);
          $result = $user-&gt;save();
  </code></pre>
  
  <p>After this I am sending an email to the user with the "user rest link" <code>_user_mail_notify('register_no_approval_required', $user);</code></p>
  
  <p>When the user clicks on the link he get "access denied" error.</p>
  
  <p>What do I have to change in the user creation code?</p>