Jump to content

LoginRegister Module - Hook for admin notifications


JayGee
 Share

Recommended Posts

Hi all,

I'm trying to hook the createdUser method of @ryan's LoginRegister module to send a notification by email to site admins when someone registers and clicks the confirm link. But struggling to get it to work. Any pointers much appreciated. I think I'm getting code blindness! ? 

I think the error may relate to my use of anonymous functions which I haven't used much in PHP before or object scope. I've also not made much use of ProcessWire hooks to-date either! (In at the deep end here). I know the hook fires as if I replace my wireMail code with a simple echo and die() it works.

I'm not getting any errors, but no message gets sent.

	$loginRegister = $modules->get('LoginRegister');

	$loginRegister->addHookAfter('createdUser', function($user) use (&$mail) {
		
		// Init wiremail
		$notificationMsg = $mail->new();

		//Prep message body
		$msgTitle = 'A new user has registered';
		$msgContent = 'Email message content';
		$msgBody = '<p style="font-weight:bold;font-size:20px;">'.$msgTitle.'</p><p>'.$msgContent.'</p>'; 

		//Create array of recipients (from on-page repeater field)
		$emailRecipients = array();
		foreach ($page->notification_recipients as $receipient) {
			$emailRecipients[] = $receipient->email;
		}

		//Create array of recipients (from charge page repeater field)
		$emailRecipients = array();
		foreach ($page->notification_recipients as $receipient) {
			$emailRecipients[] = $receipient->email;
		}

		//Send message
		$notificationMsg = $notificationMsg->send(
								$to = $emailRecipients,
								$from = 'no-reply@mydomain.com',
								$subject = $subjectMsg,
								$options = [
									'bodyHTML' => $msgBody
								]
							);

	});

	$loginRegister->set('renderStyles', false); 
	$loginRegister->execute();

 

Link to comment
Share on other sites

Did you check your server error logs? Or PW's own warning/error logs? Is this running in ready.php or your own module? Are you in debug mode? Perhaps also install Tracy Debugger to get more clues. Are you sure that otherwise wiremail is working OK?

Sorry, not a very helpful post, but I haven't use this module myself yet.

Link to comment
Share on other sites

3 hours ago, dragan said:

. Are you sure that otherwise wiremail is working OK?

The wireMail code definitely works as it was ported from another project. But double checking this has narrowed it down a bit thanks - It's definitely something to do with how I'm implementing the hook as if I move the mail sending code out of the hook it works fine.

Link to comment
Share on other sites

GOT IT! ....obvious mistake in hindsight! I needed to pass the $page object into the closure. Because I'm populating the email to field from a page template field it wasn't getting populated as the $page object wasn't there.

so 

$loginRegister->addHookAfter('createdUser', function($user) use (&$mail) {

becomes

$loginRegister->addHookAfter('createdUser', function($user) use (&$mail, &$page) {

 

Thanks all ? 

Link to comment
Share on other sites

7 minutes ago, Guy Incognito said:

GOT IT! ....obvious mistake in hindsight! I needed to pass the $page object into the closure. Because I'm populating the email to field from a page template field it wasn't getting populated as the $page object wasn't there.

so 


$loginRegister->addHookAfter('createdUser', function($user) use (&$mail) {

becomes


$loginRegister->addHookAfter('createdUser', function($user) use (&$mail, &$page) {

 

Thanks all ? 

You can also just use:

$this->wire('page')

$this->wire('mail')

etc inside the hook function.

  • Like 3
Link to comment
Share on other sites

35 minutes ago, adrian said:

You can also just use:


$this->wire('page')

$this->wire('mail')

etc inside the hook function.

Ah ok - that's interesting thanks. So are all Processwire object accessible like this within hook context?

Link to comment
Share on other sites

On 8/7/2018 at 11:21 PM, Guy Incognito said:

Ah ok - that's interesting thanks. So are all Processwire object accessible like this within hook context?

Just to clarify:
Usually there is only one instance/ object of ProcessWire which is the ProcessWire boot object represented by the variable $wire.
ProcessWire is a child class of Wire.

Assuming you are talking about all Wire objects the answer is YES and more:

// Access to a wire object from inside a wire derived class:
$pages = $this->wire('pages');

// Access to the current ProcessWire instance:
$wire = $this->wire();

// Create new wire object inside your ready.php
$this->wire('ferry', '/site/translations.php');

Read more: https://processwire.com/api/ref/wire/wire/

  • Like 2
Link to comment
Share on other sites

6 hours ago, kixe said:

Just to clarify:
Usually there is only one instance/ object of ProcessWire which is the ProcessWire boot object represented by the variable $wire.
ProcessWire is a child class of Wire.

Assuming you are talking about all Wire objects the answer is YES and more:


// Access to a wire object from inside a wire derived class:
$pages = $this->wire('pages');

// Access to the current ProcessWire instance:
$wire = $this->wire();

// Create new wire object inside your ready.php
$this->wire('ferry', '/site/translations.php');

Read more: https://processwire.com/api/ref/wire/wire/

Awesome thanks - this has been a useful lesson ? 

I think my inital confusion on this stemmed from my hook function being my own code and not part of PW, but I guess as it is an extension of an existing class then it makes sense the other variables should be accessible. (I may be rambling here... but I know what I mean!).

  • 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
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...