Answers.Body: <p>Rolling with this does what I need</p>
  <pre class="lang-php prettyprint-override"><code>$newUser = new User([
    'firstName' =&gt; $submission-&gt;firstName-&gt;getValue(),
    'lastName' =&gt; $submission-&gt;lastName-&gt;getValue(),
    'username' =&gt; $email,
    'email' =&gt; $email,
    'password' =&gt; Yii::$app-&gt;security-&gt;generatePasswordHash($email),
    'isCandidate' =&gt; true,
    'pending' =&gt; true,
  ]);
  
  $newUser-&gt;validate(null, false) &amp;&amp; Craft::$app-&gt;getElements()-&gt;saveElement($newUser, false);
  
  Craft::$app-&gt;getUsers()-&gt;sendNewEmailVerifyEmail($newUser);
  </code></pre>
  <p>I was a bit confused because after sending the verification email, my user was always enabled. Turns out my local mail testing tool was verifying the validity of the link in the email by doing a GET request  thus enabling the user.</p>
  
Answers.ContentLicense: CC BY-SA 4.0
Answers.CreationDate: 2022-06-03T09:50:33.157Z
Answers.LastActivityDate: 2022-06-03T09:50:33.157Z
Answers.UserDisplayName: Steven Grant
Body: <p>I'm trying to create a pending user account when an event occurs</p>
  <pre class="lang-php prettyprint-override"><code>$newUser = new User([
    'firstName' =&gt; $submission-&gt;firstName-&gt;getValue(),
    'lastName' =&gt; $submission-&gt;lastName-&gt;getValue(),
    'username' =&gt; $email,
    'email' =&gt; $email,
    'password' =&gt; Yii::$app-&gt;security-&gt;generatePasswordHash($submission-&gt;password-&gt;getValue()),
    'isCandidate' =&gt; true,
  ]);
  
  Craft::$app-&gt;getElements()-&gt;saveElement($newUser, false);
  
  </code></pre>
  <p>but that always creates an enabled user.</p>
  <p>I know that I want to do <code>Craft::$app-&gt;getUsers()-&gt;sendActivationEmail($newUser);</code> after the fact to send the activation email that works.</p>
  
Tags: <users><modules><status>
Title: Create a pending user from plugin
ThreadText: # Create a pending user from plugin
  
  (Asked by: username_0 on 2022-06-02)
  
  I'm trying to create a pending user account when an event occurs
  
  ```php
  $newUser = new User([
    'firstName' => $submission->firstName->getValue(),
    'lastName' => $submission->lastName->getValue(),
    'username' => $email,
    'email' => $email,
    'password' => Yii::$app->security->generatePasswordHash($submission->password->getValue()),
    'isCandidate' => true,
  ]);
  
  Craft::$app->getElements()->saveElement($newUser, false);
  
  ```
  
  but that always creates an enabled user.
  
  I know that I want to do `Craft::$app->getUsers()->sendActivationEmail($newUser);` after the fact to send the activation email that works.
  
  **Tags:** users, modules, status
  
  
  ---
  
  ## 1 answers
  
  ### Answer by username_0 on 2022-06-03. Score: 6 | Accepted answer
  
  Rolling with this does what I need
  
  ```php
  $newUser = new User([
    'firstName' => $submission->firstName->getValue(),
    'lastName' => $submission->lastName->getValue(),
    'username' => $email,
    'email' => $email,
    'password' => Yii::$app->security->generatePasswordHash($email),
    'isCandidate' => true,
    'pending' => true,
  ]);
  
  $newUser->validate(null, false) && Craft::$app->getElements()->saveElement($newUser, false);
  
  Craft::$app->getUsers()->sendNewEmailVerifyEmail($newUser);
  
  ```
  
  I was a bit confused because after sending the verification email, my user was always enabled. Turns out my local mail testing tool was verifying the validity of the link in the email by doing a GET request thus enabling the user.
  
  
  ---
  
  