3

I have this PHP script that I want to call from a mobile app, that registers an email address, uses a random password, and automatically activates & asks the user to reset the password upon first login.

This is the $data array i'm passing to the UserModelRegistration register function:

$data = array( 
  'username' => $username,
  'name' => $name,
  'email1' => $email,
  'password1' => $password, // First password field
  'password2' => $password, // Confirm password field
  'sendEmail'  => '0',
  'block' => '0',
  'activation' => '0',
  'groups' =>array("2","10"));

The sendEmail, activation and groups parameters are not really doing anything... tried changing them around, using 0 instead of '0' and played around with the order, but nothing helped.

Is this possible?

If it isn't, is there any way, to make a simple email registration which registers the email and asks for a password reset?

1 Answer 1

2

I don't think there is a way, within the Joomla core, to force a password reset. There are quite a few extensions for that sort of thing though.

The activation value wont do anything, I think, if you're creating a user from the JUser object.

I think the sendEmail parameter should work, in terms of sending the user their details. Check the options in User Manager, perhaps "Send Password" is set to "No" there. It may be that it's set to No there, and that is overriding your parameter. It may also be that there is a problem with the server sending email. You can test this in "Global Configuration", under the "Server" tab, clicking the "Send Test Mail" button in the bottom right. This will show if the server is sending mail ok, and if you receive the test message, that it's not getting caught in spam filters.

You could just send the mail to the user yourself, from your script, with something like...

<?php  
$data = array( 
  'username' => $username,
  'name' => $name,
  'email1' => $email,
  'password1' => $password, // First password field
  'password2' => $password, // Confirm password field
  'sendEmail'  => '0',
  'block' => '0',
  'activation' => '0',
  'groups' =>array("2","10")
);

$user = new JUser;
if ($user->bind($data)) {
    if ($user->save()) {
        echo "user created <br/>";

        # welcome message
        $welcome_email = "Welcome . " . $name . "\n"
        $welcome_email .= "Your username is . " . $username . "\n"
        $welcome_email .= "Your password is . " . $password . "\n"        

        # email set-up
        $config = JFactory::getConfig();
        $sender = array( 
            $config->get( 'mailfrom' ),
            $config->get( 'fromname' ) 
        );
        $mailer = JFactory::getMailer();
        $mailer->setSender($sender);    
        $mailer->isHTML(false);     

        # send the email
        $mailer->addRecipient($email);
        $mailer->setSubject('Your user account is created');
        $mailer->setBody($welcome_email);
        $send = $mailer_admin->Send();
        if ( $send !== true) {
            echo "Admin email failed: \n" . print_r(error_get_last(), true) . "<br/>";
            echo "send toString: \n" . $send->__toString() . "<br/>";
        } else {
            echo "Admin email sent<br/>";
        }
    } else {
        echo "create error " . $user->getError() . "<br/>";
    }    
} else {
    echo "bind error " . $user->getError() . "<br/>";
}               
?>

All of the above is for Joomla 3.6.4, it may vary for older versions.

I hope some of this is helpful, let me know if you've any questions of ideas. :)

3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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