1

We have a website that allows users to register themselves with a front-end registration form, and are automatically logged-in once they're done (thanks to autoLoginAfterAccountActivation).

But we also need to create users directly from the Control Panel on a regular basis. Since autoLogin doesn't work with the "Verify email addresses?" user setting, I'm looking for a way to send the activation email only to the users that we have manually created over the Control Panel, so they're able to set a password.

Is there any way to send the verification email only to users created over the Control Panel?

1

You'll need to create a small custom plugin to do this. You can create the scaffolding for a plugin using the excellent Plugin Factory site. You'll just need to create a basic plugin, with none of the component options selected.

What you need to do is to use Craft's events to detect when a new user is created using the Control Panel.

The specific event you need to listen for is the users.onActivateUser event.

You'll need something like this in your plugin's init() method:

/**
 * @return mixed
 */
public function init()
{
    // When creating new users through the control panel, send them a password reset email.
    craft()->on('users.activateUser', function(Event $event) {

        // only fire if this is a CP request
        if (craft()->request->isCpRequest()) {

            // retrieve the userModel from the event
            $user = $event->params['user'];

            // send password reset email
            craft()->users->sendPasswordResetEmail($user);
        }
    });
}
  • Thank you! While it does correctly send the "password reset" email to the user created user over the CP, it seems that the password reset url is invalid (Invalid verification code error). The weird part is that when I send the password reset directly from the CP, the link in the email works as intended. Do you have any idea why? – José Manosalvas Feb 14 '17 at 9:14
  • When you generate a new password reset link, all previous links are automatically invalidated. So if one is generated by the code above when the user is first created, then you send a second one via the CP, the first one will no longer be valid. Is that what might have happened? – Steve Rowling Feb 14 '17 at 9:33
  • After taking a look in the DB, it seems that "verificationCode" stays NULL even after sending the email and is the reason why the link won't work. Could this be because of "activateUser" function deleting the verification code, firing after the saveUser event? – José Manosalvas Feb 14 '17 at 9:48
  • Sorry @Steve, didn't saw your answer before. What I meant is that the email link threw an error even before generating a new password reset link from the CP (I did so to be sure that the password reset pages were working). – José Manosalvas Feb 14 '17 at 9:53
  • 1
    Yes, I think you're right, it could be that activateUser() is deleting the verification code. I've modified the code in my answers to use the users.activateUser event instead. Try that and let me know if it works! – Steve Rowling Feb 14 '17 at 10:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.