vxda Posted June 2, 2014 Share Posted June 2, 2014 hi im trying to send email to user after page he created has been moved to another parent/ I found this on forum : https://processwire.com/talk/topic/1705-need-to-send-email-notification-after-a-page-has-been-published/i want to do exacly what Ryan described in Option 2.but i have no idea how hooks works. I was trying to do this from my template.in my file header.inc require("phpmailer/class.phpmailer.php"); function sendMailOnChange(HookEvent $event) { $page = $event->arguments[0]; if(!$page->parentPrevious) return; // if page is not being moved then stop now if($page->parent->id != 1286 && $page->parentPrevious->id == 1286) { $mail = new PHPMailer(); .... } } function init() { wire()->addHookAfter('Page::saveReady', $this, 'sendMailOnChange'); } Please explain to me how do i use hooks from my template files. Thank you Link to comment Share on other sites More sharing options...
horst Posted June 2, 2014 Share Posted June 2, 2014 (edited) @vxda: I think you cannot do on the template. You need to do it with a module. Please refer to the API of that and to the "Hello World" module. If you have any questions than, maybe what hook to use, please ask here, (for any further help). Also you need not to include an external mailer. You can use the new wiremail() function and one of the new wireMail-extension modules: teppos SwiftMailer or my WireMailSmtp. Edited June 2, 2014 by horst 2 Link to comment Share on other sites More sharing options...
vxda Posted June 3, 2014 Author Share Posted June 3, 2014 Thanks horst i managed, it wasnt so difficult. Link to comment Share on other sites More sharing options...
adrian Posted June 3, 2014 Share Posted June 3, 2014 Care to share your final code - I bet it will help someone else here at some point in the future 2 Link to comment Share on other sites More sharing options...
vxda Posted June 4, 2014 Author Share Posted June 4, 2014 Care to share your final code - I bet it will help someone else here at some point in the future Sure will , just after il come back from work. Link to comment Share on other sites More sharing options...
vxda Posted June 5, 2014 Author Share Posted June 5, 2014 Here is the code for module that sends email to page creator, once the page is moved from one parent to another. <?php require_once("/site/templates/phpmailer/class.phpmailer.php"); class sendMail extends WireData implements Module { public static function getModuleInfo() { return array( 'title' => 'Notify module', 'version' => 101, 'summary' => 'Send email after pages is moved to another parent', 'singular' => true, 'autoload' => true ); } public function init() { $this->pages->addHookAfter('Pages::saveReady', $this, 'sendMailOnChange'); } public function sendMailOnChange($event) { $page = $event->arguments[0]; if(!$page->parentPrevious) return; // if page is not being moved then stop now // When page is moved outside its parent but not trash if(($page->parent->id != 1286 && $page->parent->id != 7) && $page->parentPrevious->id == 1286) { //Email address of user that created page $userEmail = $page->createdUser->email; //Message that appears after page is moved $this->message("Mail was sent to :{$page->createdUser->name} at his email address: {$userEmail}"); $mail = new PHPMailer(); $mail->IsHTML(true); $mail->isSMTP(); $mail->Debugoutput = 'html'; $mail->Host = "YOUR HOST"; $mail->Port = 25; $mail->SMTPAuth = true; $mail->CharSet = "UTF-8"; $mail->Username = "EMAIL ADDRESS OF MAILBOX U WANT TO SEND MESSAGE FROM"; $mail->SetFrom("I USE SAME AS ABOVE $mail->Username"); $mail->Password = "PWD TO YOUR MAILBOX"; $mail->FromName = "STRING FROM"; $mail->AddAddress($userEmail); $mail->Subject = __("YOUR MESSAGE IS MOVED"); $mail->Body = "Hello: {$page->createdUser->name} <br />". __("your post has been moved !!"). "<br />". "You can find it here: <a href='{$config->httpHost}{$page->url}'>{$config->httpHost}{$page->url}</a>"; ; $mail->send(); } } } ?> I used phpmailer since im using it from the begining of project. If u can please show how would same function look with wireMail();Cheers ! 3 Link to comment Share on other sites More sharing options...
horst Posted June 5, 2014 Share Posted June 5, 2014 A hardcoded parent folder id (1286) isn't the best way to specify it, ($page->parent->id != 1286 && $page->parent->id != 7) && $page->parentPrevious->id == 1286 ... here is the code if you use it with wiremail: public function sendMailOnChange($event) { $page = $event->arguments[0]; if(!$page->parentPrevious) return; // if page is not being moved then stop now // When page is moved outside its parent but not trash if(($page->parent->id != 1286 && $page->parent->id != 7) && $page->parentPrevious->id == 1286) { // Email address of user that created page $userEmail = $page->createdUser->email; // Message that appears after page is moved $this->message(sprintf( __("Mail was sent to: %s at his email address: %s "), $page->createdUser->name, $userEmail); // preparing and sending email $subject = __("YOUR MESSAGE IS MOVED"); $textBody = sprintf( __("Hello: %s \r\nyour post has been moved !!\r\nYou can find it here: %s"), $page->createdUser->name, $config->httpHost . $page->url); $from = 'me@example.com'; // if you have defined it in config page, you simply can send an empty string here $numSent = wireMail($userEmail, $from, $subject, $textBody); } } If you have installed one of the extended wireMail modules, you have done your smtp server config on the modules config page and don't need it to repeate in the code for every mail you send. Also you have tested via a testconnection if the settings a free of typos. One more thought: maybe a simple plaintext mail is enough, if the only html you need are 2 linebreaks. - plaintext does support that too. 2 Link to comment Share on other sites More sharing options...
vxda Posted June 6, 2014 Author Share Posted June 6, 2014 A hardcoded parent folder id (1286) isn't the best way to specify it, why ? i found that page names are changing more offten then page id. thats why i use id. Thx for the code Link to comment Share on other sites More sharing options...
horst Posted June 6, 2014 Share Posted June 6, 2014 why ? i found that page names are changing more offten then page id. thats why i use id. Thx for the code ID is ok, but hard coded somewhere in the code isn't that good. Because you display code for the public and at least you should comment that for users that are not familiar with PW or module code (starters?). 1 Link to comment Share on other sites More sharing options...
vxda Posted June 6, 2014 Author Share Posted June 6, 2014 cool get it. well im a module starter myself, i know there is some way to enable settings button on a module and then u could choose all options there but i dunno how . but ye hardcoded stuff is bad for a module. 1 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