Jump to content

Email New User


adrian

Recommended Posts

Actually, on second thought (distracted thoughts while working on something totally non-PW), I think a test send would be a good idea - would be nice to see exactly what the users will getting emailed to them.

Might take me a few days to get to this, but I will get to it as soon as I can.

  • Like 1
Link to comment
Share on other sites

I have just committed an update that adds an option to send a test message from the module settings page.

The fields in the email body will come from the sending user's account so you must have an email entered for your account and if you have fields like first name etc in the body template, then you should have those completed for your account also.

If your body template makes use of the {pass} code, in the test email you will see "password" instead of your actual password. 

Please let me know if you find any problems with this new version or any other ideas for improvements.

  • Like 1
Link to comment
Share on other sites

There is something you could explain though. I'm still working on an easy login process for people so i tried putting the following in the welcome email:

Welcome {first_name} {last_name}

Please login in at: http://abandoman.co.uk/login/{name}/{pass}

Username: {name}
Password: {pass}

If you have any questions, please email us at: {fromEmail}

I enable url segments and put the following in my login page:


if($input->urlSegment(1) && $input->urlSegment(2)) {
	$username = $sanitizer->username($input->urlSegment(1));
        $pass = $input->urlSegment(2);
         $u = $session->login($username, $pass);
  		if ($u && $u->id) {
    // user is logged in, get rid of tmp_pass
    $u->of(false);
    $u->tmp_pass = '';
    $u->save(); }
         $session->redirect("/");
}

Works great if you have a simple user name and password, but with a system generated one it throws a page not found error.

I'm assuming that's because of all the stars and @s in the password.

Is there anyway around this?

Link to comment
Share on other sites

Not that I necessarily think it is a good idea that you're logging people in using the password in the url!

But I thought it would be a good idea to add this functionality anyway - so with the new version you can now control the character sets used in the automatically generated password, as well as its length.

Turning off the "Special Characters" set should take care of what you need.

Let me know if that works for you.

  • Like 1
Link to comment
Share on other sites

That's great. Logs in first time.

I don't need nca proof passwords for this site and if it got sent or intercepted by email I don't think there's too much damage that could be done anyway.

Awesome module

  • Like 1
Link to comment
Share on other sites

I've been testing a bit further and have noticed some anomalies which you may be able to advise on. 

The module works great when creating a user from the back end, but when I create a user using form builder 2.4 dev to create a page user details, the email fails to send.

I'm using the following hook to push the password and title into a session variable for later user:


wire()->addHookAfter('FormBuilderProcessor::formSubmitSuccess', function($event) {
  $form = $event->arguments(0); 
  if($form->name != 'new-user') return;
  $field = $form->get('password'); 
  $value = $field->attr('value'); 
  wire('session')->set('password', $value); 

    
      
      $field = $form->get('title');
      $value = $field->attr('value'); 
      wire('session')->set('title', $value);
}); 

I'm sure that prior 'email new user' was sending out an email on creation of a user from the front end.

The things that have changed are:

I've added the hook

upgraded for builder from 2.2 to 2.4 dev

upgraded the module

Could any of these thing have affected it's behaviour?

Link to comment
Share on other sites

Just a quick guess - I did make the module only load for the admin at some point, but it was several versions back. Try going into:

EmailNewUser.info.json and changing:

"autoload": "template=admin",

to:

    "autoload": true,

If that works, I'll consider changing it back for you in the main module code.

Let me know if that works for you.

Link to comment
Share on other sites

That change made sense, but unfortunately didn't work.

Looks like this:

{

"title": "Email New User",

"summary": "Email new user their account details, and optionally automatically generate a password for them.",

"version": 14,

"author": "Adrian Jones",

"autoload": true,

"requires": "ProcessWire>=2.4.3",

"icon": "envelope-o"

}

I'm sure this was working a while ago, but now I'm starting to doubt myself.

Thanks for your help with this.

Any ideas?

Maybe I'll have to look again with a fresh head from another angle.

Link to comment
Share on other sites

davo,

Just a quick note to say that I tested on the front-end and it works fine like this:

$modules->get("EmailNewUser");
$u = new User();
$u->name = 'newuser';
$u->email = 'newuser@gmail.com';
$u->save();

Note that I am manually calling the module - that avoids the need to make it autoload on the front-end.

I don't see where in your form builder hook that you create a new user ?

Link to comment
Share on other sites

davo,

Just a quick note to say that I tested on the front-end and it works fine like this:

$modules->get("EmailNewUser");
$u = new User();
$u->name = 'newuser';
$u->email = 'newuser@gmail.com';
$u->save();

Note that I am manually calling the module - that avoids the need to make it autoload on the front-end.

I don't see where in your form builder hook that you create a new user ?

Could that be the issue you think? form builder publishes the form as a new page rather than calling a hook to make a new user?

Link to comment
Share on other sites

I just tested this which is how I would expect form builder is probably creating the page and it also works:

$modules->get("EmailNewUser");
$p = new Page();
$p->name = "me";
$p->parent = $pages->get(29);
$p->template = $templates->get("user");
$p->email = "me@gmail.com";
$p->of(false);
$p->save();

So the only things I can think of are that the module is not being loaded. See here that again I am manually loading it, but if you change it to autoload in the module, you should be ok.

The other thing to clarify - is the form definitely creating and saving the email address?

I am not really a form builder user, but perhaps if you can explain your exact configuration I can play around with it and test at my end. 

Link to comment
Share on other sites

Ok, it all makes sense now :)

Ideally it would be best if you could hook into the point before the form builder saves the page - not sure what that hook is - sorry.

But, if you want to quickly test, the autoload = true option works, you just have to remember to refresh your modules: Modules > Refresh after you make that change to the info.json file that I mentioned above.

Do that and I think the emails should send fine even via the formbuilder user page creation.

It shouldn't be a huge problem to leave the module autoload because it checks to make sure that the page being saved has the user template, so it shouldn't affect any other pages on the front-end.

Let me know if that works for you.

  • Like 1
Link to comment
Share on other sites

I've just foolishly made two changes, and although it now works, I don't know which it was!

First I put this on the page that loads the form:

$modules->get("EmailNewUser");

Second I refreshed the module

And now it works!

  • Like 1
Link to comment
Share on other sites

So long as you are loading the module on the front end like that, the refresh shouldn't be relevant. The refresh was only if you wanted to do the autoload, but this is much better. I wasn't think about adding the load to the page containing the form - much easier than hooking - good thinking :)

Link to comment
Share on other sites

I'm sure that a couple of versions ago, it was set to only create a password if one had not already been set. Now, if I have the 'create password' checked it's creating one regardless, meaning my login mechanism isn't working as it's immediately been over written by the module.

If I uncheck the field, the password sent in the email is blank.

I assumed you'd changed something a version or two ago. I was sure it said in the text next to that field a password would only be created if the  field was blank?

Link to comment
Share on other sites

I am only testing via the admin at the moment, but everything seems to be working here whether the "Generate Password" option is checked or not. In either case I can manually enter a password and that is the one that gets emailed out.

If it isn't checked, and I don't manually enter one, I get a warning that the email wasn't sent because the password wasn't set, but if I do manually set it, it works fine.

What exact code are you using to create the user and add the password?

Link to comment
Share on other sites

Unfortunately I don't know the exact code as   I use form builder to match fields into the user template, but I'll walk you through the process....

I have a template that loads the module on the page and then loads the form in form builder like this:

<?php 
$modules->get("EmailNewUser");
echo $forms->embed('new-user'); ?>

Then, in the form there is a field called title which maps to 'name' in a user page. There is also a password field which also maps to 'password'.

The new user is created.

In the form builder include file i have the following code:

<?php 

wire()->addHookAfter('FormBuilderProcessor::formSubmitSuccess', function($event) {
  $form = $event->arguments(0); 
  if($form->name != 'new-user') return;
  $field = $form->get('password'); 
  $value = $field->attr('value'); 
  wire('session')->set('password', $value); 

    
      
      $field = $form->get('title');
      $value = $field->attr('value'); 
      wire('session')->set('title', $value);

      $field = $form->get('dob');
    $value = $field->attr('value'); 
 wire('session')->set('dob', $value);

      $field = $form->get('email');
    $value = $field->attr('value'); 
 wire('session')->set('email', $value);
}); 

The above code helps me then store the newly created variables as session variables so I can use them on the following page.

On successful page creation (user) , the user is redirections to my acc_success page which consists of the following::


<?php

$dob = $session->dob;
$email = $session->email;

$new_user = $pages->get("email=$email");
$password = $session->password;
$username = $new_user->name;

    if($session->login($username, $password)){
         $session->redirect("/");}



echo "<p>Thats great. Now just click below to log in and help judge - <i>its ok, its for science!</i></p><p><a href='/login/{$new_user->name}/{$password}'>Log in {$new_user->name} with the password {$password}</a></P>";

?>

This should look for a user matching the email address supplied and look up the user name and use the previously supplied password to log the user in.

The odd behaviour i'm suffering is that if the create user password is ticked, it over rides the user created password and the automatic login fails. I check the email sent and it references a newly created password.

If the create password is not ticked then the automatic login succeeds but of course no password is supplied by the module.

Suddenly typing this out it makes more sense!

I guess the situation I want to get to is for the module to only to create a password if it were left blank, but also to be able to include the password supplied by by the session variable.... I guess that's my next move to try.

Link to comment
Share on other sites

So this is what i tried to use the session variable in the module settings:

Welcome {first_name} {last_name}

Your photo has been submitted! Login to judge yourself against others. 

Please login in at: http://abandoman.co.uk/login/{name}/{pass}{$session->password}

the email was just blank  for both pass and session->password parts.

don't worry too much, I know what i'm trying to get it to do is a bit beyond it's original purpose

Link to comment
Share on other sites

Firstly, the $session->password will never work, because I am not using eval to pass the contents of the email, but rather using the {} as a replacement mechanism.

The problem with getting a password manually entered via the API was that I was using the InputfieldPassword::processInput hook to grab the password before it is encoded. This won't be triggered when the password is set via the API.

I just committed an update that uses the Password::setPass hook instead and it seems to be working well in all scenarios, including setting the password via the API.

Could you please test and let me know if that fixes things for you?

  • Like 2
Link to comment
Share on other sites

  • 4 months later...

Just committed some new features:

  • New checkbox "Send welcome message" added to the bottom of each user page - uncheck to NOT send the email to a new user.
  • You can use this checkobox to re-send a user's welcome email if needed.
  • You can edit the welcome message template for each user as you create and save the user.
  • Also some better error reporting, and detection of PasswordForceChange.

Please let me know if you have any problems with this new version.

  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...