Jump to content

Email New User


adrian

Recommended Posts

@adrian Sending copies to a list of users would certainly come in very handy! For debugging user registration scripts, I'd usually have to switch Bcc recipients regularly.

As for re-branding the Bcc option as "notify other users": sounds good, too. I'm fine with the way it works now and would see the ability to send multiple copies as a priority, but I certainly see your point in simplifying it.

  • Like 1
Link to comment
Share on other sites

20 hours ago, d'Hinnisdaël said:

@adrian Sending copies to a list of users would certainly come in very handy! For debugging user registration scripts, I'd usually have to switch Bcc recipients regularly.

As for re-branding the Bcc option as "notify other users": sounds good, too. I'm fine with the way it works now and would see the ability to send multiple copies as a priority, but I certainly see your point in simplifying it.

Ok, It now supports a comma-separated list of email addresses and it has been rebranded as "Notify Other Users" and it now uses "To" for sending to these addresses as well.

Please let me know if you see any problems.

  • Like 1
Link to comment
Share on other sites

  • 4 weeks later...

Hi @adrian thanks for this great module.

I want to always show the "Send welcome message" field/checkbox for new users, that I create in the admin.
But this checkbox never shows. I filled out the email-address but it still does not show up.
I unchecked the option "Automatic Email Send" in the modules settings, because I want to choose if a user should get a welcome email or not.

Could this be because of the core version of PW that I am using? I am using 3.0.71 right now.

I turned on ProcessWire debug now, and EmailNewUser triggers an error:

Trying to get property of non-object in K:\xampp\htdocs\myproject\src\site\assets\cache\FileCompiler\site\modules\EmailNewUser\EmailNewUser.module on line 54

That line reads 

if ($this->wire('page')->process == "ProcessUser") $this->addHookAfter('ProcessPageEdit::buildFormContent', $this, 'addEmailFields');

I think the problem is 

$this->wire('page')->process == "ProcessUser"

but I am not sure. Any suggestions?

emailnewuser.png

  • Like 1
Link to comment
Share on other sites

Hi @jmartsch - glad you are finding it useful. 

I can't seem to reproduce any of the problems you are having

What version of the module are you running? Line #54 isn't that line in the current version, although it's only one off, so maybe it's a misreporting?

Do the various fields show when you have the automatic option check in the module config settings?

Also, what is it that you are dumping on line 72 that is returning "FieldtypePassword" ?

One thing to try here - can you change the:

public function init() {

on line 48 to:

public function init() {

}

public function ready() {

Yes, it is making the init function empty and converting the current init() function into a ready() function.

Please let  me know if that helps.

  • Like 1
Link to comment
Share on other sites

Hey @adrian. Thanks for your quick reply :)

42 minutes ago, adrian said:

Do the various fields show when you have the automatic option check in the module config settings?

 

No they did not show even when the option was checked.

Switching the init function to a ready function fixes the problem. I also thought of doing this, but didn't do it.
Now it is working fine :)

42 minutes ago, adrian said:

What version of the module are you running? Line #54 isn't that line in the current version, although it's only one off, so maybe it's a misreporting?

 

I use the latest version from github = 1.0.5 and if I open the file in a texteditor or in my IDE then line 54 is the correct line.

EDIT: Weird, on github it is line 55. Must be something in my editors.

Regarding the output of line 72 (that was something that I added):

bd($event->object->attributes["class"]);

There is a problem with removing the required attribute from password fields. It gets removed, but if you accidentally enter something in the password field and then delete it, you can not submit the form, because the required attribute gets added back in. This is coming from the InputfieldPassword.js which gets added to the page. Starting in line 120 it reads

$input.on('change', function() {
			var val = $(this).val();
			if(val.length > 0) {
				$input.attr('required', 'required');	
				$confirm.attr('required', 'required');
			} else if(!$(this).closest('.InputfieldStateRequired').length) {
				$input.removeAttr('required');
				$confirm.removeAttr('required');
			}
		});

So if the "Generate Password" Option is set, the script should not be executed or loaded.

One approach would be, to remove the CSS class "InputfieldPasswordComplexify" so the script does not react to it.

I tried to make a hook for this, but it does not work yet. Maybe you have some suggestions?

Here is my hook in the ready function:

$this->addHookBefore('InputfieldPassword::render', $this, 'removeComplexify');

and here the function

 protected function removeComplexify(HookEvent $event)
    {
        $f = $event->object;
        bd($f);
        $f->removeClass('InputfieldPasswordComplexify');
    }

I don't know what is wrong or if this is the right approach. I even tried to use addHookAfter, which also did not work. 

Maybe you have an idea?!

  • Like 2
Link to comment
Share on other sites

Wow, thanks for noticing that issue when manually entering and deleting a password - I had never come across that before.

The reason that "before" hook doesn't work for is because the InputfieldPassword::render() method uses addClass('InputfieldPasswordComplexify'), so you are removing it before it's actually added. I have achieved the same thing though with a str_replace on an "after" hook - not as elegant, but works fine.

You actually got me thinking about the whole approach to automatically generating the password and I thought it might be better to actually populate the fields when creating the user, rather than leaving them blank and populating when publishing the user.

I am going to PM you my current dev version to take a look at as see what you think of this new approach.

Thanks again for your help/feedback with this!

  • Like 1
Link to comment
Share on other sites

Ok, I have committed a new version which fixes the two bugs that @jmartsch reported.

As mentioned above, I have also revamped the password generator - it now populates the password fields while you are creating the user and the generated password matches the options defined in the PW "pass" field's config settings. The only one I can't match is the complexity factor, but so long as you stick to the default (or lower) it should all be fine - thanks to @Robin S's Password Generator (https://processwire.com/talk/topic/17406-password-generator/) for some inspiration :) 

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

  • Like 2
Link to comment
Share on other sites

Hey @adrian. I installed the new version from Github. There is a small bug that I found:

If I create a user and don't send him a welcome message, then the next time I edit this user (maybe I want to add some details to custom fields) the label of the email function reads "Re-send welcome message" although I did not send an email yet.

Feature request:

If "Generate password" is checked, and the "PasswordForceChange" module is installed, the checkbox "Force password change on next login" should be checked, because it is not save, to send passwords via email. But maybe that is just my opinion. What do you think of that?

  • Like 1
Link to comment
Share on other sites

Here is another one: If I create a user via the API and use $new_user->sendEmail = true; an email is not being sent.

Here is the code that I execute via Tracy console:

$new_user = new User();
$new_user->of(false);
$new_user->addRole("editor");
$new_user->email = "aceman3000@gmail.com";
$new_user->name ="aceman3000-gmail.com";
$new_user->sendEmail = true;
$new_user->save();

 

  • Like 1
Link to comment
Share on other sites

Ok @jmartsch, getting through these:

3 hours ago, jmartsch said:

If I create a user and don't send him a welcome message, then the next time I edit this user (maybe I want to add some details to custom fields) the label of the email function reads "Re-send welcome message" although I did not send an email yet.

Not sure how to handle this - the label changes from "Send" to "Resend" once the user has been published. I don't otherwise store a record of the email being sent. Sure it's possible, but I think it might get unnecessarily complicated. Do you have any thoughts for how it could be implemented well?

 

4 hours ago, jmartsch said:

Feature request:

If "Generate password" is checked, and the "PasswordForceChange" module is installed, the checkbox "Force password change on next login" should be checked, because it is not save, to send passwords via email. But maybe that is just my opinion. What do you think of that?

EmailNewUser respects the "Automatic Force Change" option in the PasswordForceChange module which ensures it is automatically checked when a new user is created. Does that work for you? I am hesitant to override that setting if it's intentionally unchecked. What do you think?

 

4 hours ago, jmartsch said:

Another bug: If you choose to re-send the welcome message, a new password is not generated.

Thank you - that is a new bug - because the password was populating the pass fields, rather than being generated on publish. Now it will generate if nothing is already set, which is the case when re-sending the welcome message.

 

4 hours ago, jmartsch said:

Here is another one: If I create a user via the API and use $new_user->sendEmail = true; an email is not being sent.

Here is the code that I execute via Tracy console:


$new_user = new User();
$new_user->of(false);
$new_user->addRole("editor");
$new_user->email = "aceman3000@gmail.com";
$new_user->name ="aceman3000-gmail.com";
$new_user->sendEmail = true;
$new_user->save();

 

Thanks - there are two things going on here. I broke this when I moved all the hooks yesterday from ready() to init(). I have fixed this now, but the other thing is that you are missing this:

$modules->get("EmailNewUser"); // call the module since it is not autoload on the front end

I just tested with your code with this on top in Tracy's console and it's now working fine.

Please test the latest version and let me know how you go.

Link to comment
Share on other sites

22 minutes ago, adrian said:
4 hours ago, jmartsch said:

Feature request:

If "Generate password" is checked, and the "PasswordForceChange" module is installed, the checkbox "Force password change on next login" should be checked, because it is not save, to send passwords via email. But maybe that is just my opinion. What do you think of that?

EmailNewUser respects the "Automatic Force Change" option in the PasswordForceChange module which ensures it is automatically checked when a new user is created. Does that work for you? I am hesitant to override that setting if it's intentionally unchecked. What do you think?

Actually, maybe the solution here is another config setting in EmailNewUser. The setting would force automatic selection of "Force password change on next login", even if the "Automatic Force Change" option isn't checked in the PasswordForceChange module. Do you think this would work ok?

Link to comment
Share on other sites

Hey @adrian. The password generation in the latest version does not work correctly. It only generates passwords that are 1 char long.

In line https://github.com/adrianbj/EmailNewUser/blob/master/EmailNewUser.module#L158 you generate the new password. The output of 

bd($this->wire('fields')->get('pass')->minlength);

is null. If I try to insert a number instead, it works.

 $newPass = $this->passwordGenerator(6, false, $this->buildPasswordCharacterSets($this->wire('fields')->get('pass')));

I get back to the other questions soon.

Link to comment
Share on other sites

Hi @jmartsch - I can't seem to reproduce this - it always returns the correct value from the "pass" field's settings. I tried an API send and a Re-send message and they both returned 10 correctly.

In my case it is set to 10. Any chance your pass field is set to 1, or it's not named "pass".

What happens if you do: 

d($fields->get('pass')->minlength);

in the Tracy Console panel?

59f72a3c2a01d_ScreenShot2017-10-30at6_33_26AM.png.548dc5754c150787540faa72378716b0.png

Link to comment
Share on other sites

Actually, I think there is a bigger issue at hand - setting the password generator to less than 8 characters results in "too weak" from the PW password validation, so I am going to force the number of characters higher for the automatic generation. After all,  this is only meant to be a temporary password so probably no real need to worry because users will be copying/pasting anyway.

I'll commit an update shortly. Hopefully this will deal with your issue anyway, although it would still be nice to know why you are getting "1".

Link to comment
Share on other sites

Ok, new version committed. I am now forcing more complex password generation. Using the settings from the "pass" field just weren't working well - often not complex enough for the complexity validation, but should be good now.

Link to comment
Share on other sites

Adrian, thank you for your support. The new version is generating the password fine.

But now the problem is back that no email is being sent when I create the user via the API :(

Here is the code, that I used to test this:

$new_user = new User();
$new_user->of(false);
$new_user->addRole("editor");
$new_user->email = "someone@somewhere.com";
$new_user->name ="someone-somewhere.com";
$new_user->sendEmail = true;
$new_user->save();

In a fresh PW 3.0.80 install I tried the latest version 1.1.2 and the output of 

d($fields->get('pass')->minlength);

is still "null". If I output only the field, I get the following data (see screenshot).

chrome_2017-10-30_15-20-53.png

Link to comment
Share on other sites

1 minute ago, jmartsch said:

But now the problem is back that no email is being sent when I create the user via the API

Don't forget that you need: 

$modules->get("EmailNewUser");

Are you using that and it's still not working?

As for the minlength thing - I get the feeling that maybe you're using a version of PW without those newish settings? This is what I see.

59f73b7c92c94_ScreenShot2017-10-30at7_45_26AM.png.e8c36438f4fe522c8ea79eb3723896e3.png

 

Link to comment
Share on other sites

Thanks. I did not use 

$modules->get("EmailNewUser");

because I previously changed the module to autoload, but after the updates this setting was gone. Thanks for the hint.

I am using latest ProcessWire Dev version from Github which is 3.0.80

And my settings look like in my screenshot. I am on a Windows System if that matters. But I don't think so.

After going into the pass field and saving the settings my bar dump now shows the same settings as yours. But these settings seem to be not available by default.

image.png.9231233fce20266b31fbde4e0c6e8073.png

Link to comment
Share on other sites

Even when I use:

$modules->get("EmailNewUser");
$new_user = new User();
$new_user->of(false);
$new_user->addRole("editor");
$new_user->email = "someone@somewhere.com";
$new_user->name ="someone-somewhere.com";
$new_user->sendEmail = true;
$new_user->of(true);
$new_user->save();

in Tracy console, the mail is not being sent. Or do I do something wrong?

Link to comment
Share on other sites

9 minutes ago, jmartsch said:


 

because I previously changed the module to autoload, but after the updates this setting was gone. Thanks for the hint.

Glad that sorted things out. I could possibly make it optionally autoloaded on the frontend, but I figure that loading it manually like that is an acceptable approach - any strong thoughts on this?

9 minutes ago, jmartsch said:

After going into the pass field and saving the settings my bar dump now shows the same settings as yours. But these settings seem to be not available by default.

Interesting - is this site upgraded from an old PW version before these settings were available, or is it a new install? I wonder if that's the reason, but also wonder if it's something that the PW System Updater should have taken care of when you upgraded - maybe something to bring up with Ryan? Either way, it's good to know. I am no longer relying on these settings - all I am doing now is adding "symbols/puctuation" if selected, and using a higher min password length if it's set higher than the 12 which I am forcing as a minimum.

Link to comment
Share on other sites

5 minutes ago, jmartsch said:

Even when I use:


$modules->get("EmailNewUser");
$new_user = new User();
$new_user->of(false);
$new_user->addRole("editor");
$new_user->email = "someone@somewhere.com";
$new_user->name ="someone-somewhere.com";
$new_user->sendEmail = true;
$new_user->of(true);
$new_user->save();

in Tracy console, the mail is not being sent. Or do I do something wrong?

When I try that I get this error: 

Exception: Can’t save page 0: /admin/access/users/adrian5/: Call $page->setOutputFormatting(false) before getting/setting values that will be modified and saved. [roles] on line: 500 in /Users/ajones/Sites/ecoreportcard/wire/core/PagesEditor.php

59f74094c5c5c_ScreenShot2017-10-30at8_08_50AM.png.5dc76bf037829fa5d1fc2a80af8e2819.png

 

So, it's pretty clear that you can't turn outputformatting back on before saving (line 9). Do you not see that error in the Tracy console output?

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...