JayGee Posted August 7, 2018 Share Posted August 7, 2018 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 More sharing options...
dragan Posted August 7, 2018 Share Posted August 7, 2018 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 More sharing options...
flydev Posted August 7, 2018 Share Posted August 7, 2018 (edited) Hi, replace the $notificationMsg->send(...) call by $notificationMsg->mail(...) if you use WireMailSmtp module. or read how to send an email there: https://github.com/processwire/processwire/blob/dev/wire/core/WireMail.php#L13-L46 Edited August 7, 2018 by flydev missing text Link to comment Share on other sites More sharing options...
JayGee Posted August 7, 2018 Author Share Posted August 7, 2018 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 More sharing options...
JayGee Posted August 7, 2018 Author Share Posted August 7, 2018 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 More sharing options...
adrian Posted August 7, 2018 Share Posted August 7, 2018 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. 3 Link to comment Share on other sites More sharing options...
JayGee Posted August 7, 2018 Author Share Posted August 7, 2018 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 More sharing options...
kixe Posted August 9, 2018 Share Posted August 9, 2018 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/ 2 Link to comment Share on other sites More sharing options...
JayGee Posted August 9, 2018 Author Share Posted August 9, 2018 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!). 2 Link to comment Share on other sites More sharing options...
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now