<p>I'm working on a project where I use Craft CMS to manage user sessions and store user preferences such as site version and preferred language. However, for privacy reasons (GDPR compliance), I'm not storing any user data, including name, first name, or email address, in Craft CMS.</p>
<p>I need to create users from a Controller without requiring an email address and automatically activate their accounts. The idea is that I handle user creation and login using a third-party API.</p>
<p>I've tried creating the user without an email address using the following code, but the account remains inactive, so I can't start the session:</p>
<pre><code>$user = new User();
$user-&gt;username = $apiUserId;
$user-&gt;password = StringHelper::randomString(16, true);
Craft::$app-&gt;getElements()-&gt;saveElement($user);
</code></pre>
<p>Is there a way to create a user without an email address and automatically activate the account on save?</p>
<p><strong>UPDATE:</strong></p>
<p>I've found that I can activate the user after that code using <code>Craft::$app-&gt;users-&gt;activateUser($user);</code>.</p>
<p>However, I'm still looking to register the user without any email address (currently I'm setting a dummy one from <code>$user-&gt;email = 'john@doe.com';</code>).</p>

CC BY-SA 4.0
2023-10-17T16:43:21.920Z
Can you generate a junk email address? i.e. `randomstring@randomstring.com`?
Brad Bell
CC BY-SA 4.0
2023-10-18T07:44:12.760Z
Yes that's what I did, but it feels a bit dirty.
Romain P.
<controller><registration><session>
Creating a User without email requirement and auto-activation from a Controller
# Creating a User without email requirement and auto-activation from a Controller

(Asked by: username_0 on 2023-10-06)

I'm working on a project where I use Craft CMS to manage user sessions and store user preferences such as site version and preferred language. However, for privacy reasons (GDPR compliance), I'm not storing any user data, including name, first name, or email address, in Craft CMS.
I need to create users from a Controller without requiring an email address and automatically activate their accounts. The idea is that I handle user creation and login using a third-party API.
I've tried creating the user without an email address using the following code, but the account remains inactive, so I can't start the session:
$user = new User();
$user->username = $apiUserId;
$user->password = StringHelper::randomString(16, true);
Craft::$app->getElements()->saveElement($user);

Is there a way to create a user without an email address and automatically activate the account on save?
UPDATE:
I've found that I can activate the user after that code using Craft::$app->users->activateUser($user);.
However, I'm still looking to register the user without any email address (currently I'm setting a dummy one from $user->email = 'john@doe.com';).

**Tags:** controller, registration, session
> **Comments:**
> **username_1**: Can you generate a junk email address? i.e. `randomstring@randomstring.com`?  
> **username_0**: Yes that's what I did, but it feels a bit dirty.  

---

## 0 answers

