Welcome email not sent when adding user from console

I’m trying to create users manually (I have a few reasons for doing this). I tried various combinations of

  • User.create()
  • User.new()
  • setting the active:true or approved:true or approve_by_id:1 or password:'non-empty-string' in the arguments of the above
  • calling .approve(<user>, true) ← according to user.rb#L270 it should trigger an email?
  • calling .activate()

While new users are created fine, no email is ever sent to the new user. I can skip the .activate() step and then go to the GUI and click on the Send activation email which does send them an email to activate their account. Getting an activatin email is still not great, because the user does not know their password and involves a lot of mouse clicking. I would then either have to send them a separate email with their password, or edit the activation email to tell them to change their password … neither of those is really great.

What I really want is to send them the “Welcome, please choose a password” email that has a link taking them straight to the screen where they choose a new password.

How can I trigger that email from the console?

1 Like

Correction: If I enable the must approve users options in Settings then an email is sent, but it is an activation email (similar to what I can generate by clicking “Send activation email”), whereas what I’d like is an email that takes them directly to the screen for choosing a password

I guess you can use Invites feature for this functionality. Invited user can choose their password (optional) while accepting the invitation.

1 Like

I want to do it from the console with predefined usernames, and force users to only chose a password (I don’t want them to choose the username, the setting username change period is set to 0). I don’t think invites help me in this context …

Is there no way to trigger a “choose a password” email from the console?

I tried to to use Jobs.enqueue(), similarly to how it’s used in User.send_approval_email() here but I failed at generating an email hash

You can do the same as what happens when they click “Send password reset email” in their preferences:

https://github.com/discourse/discourse/blob/master/app/controllers/session_controller.rb#L248-L249

3 Likes

@neil awesome! Thanks. That was the pointer I needed.

I wanted the “choose a password” email (rather than “password reset” which is confusing for a new user), so I replaced type: :forgot_password with type: :account_created … job done.

For anyone looking for the full command list:

rails c
u = User.new({username: 'foo', email: 'foo@domain.com', name: 'Foo', trust_level: 1, password: SecureRandom.hex});
u.activate();
u.approve(1); # approved by user id 1, which is normally the first admin account
t = u.email_tokens.create(email: u.email);
Jobs.enqueue(:critical_user_email, type: :account_created, user_id: u.id, email_token: t.token);

The option must approve users is disabled in Settings.

3 Likes