1<?php 2 3use dokuwiki\Extension\ActionPlugin; 4use dokuwiki\Extension\EventHandler; 5use dokuwiki\Extension\Event; 6use splitbrain\dokuwiki\plugin\smtp\Message; 7use splitbrain\dokuwiki\plugin\smtp\Logger; 8use Tx\Mailer\SMTP; 9 10/** 11 * DokuWiki Plugin smtp (Action Component) 12 * 13 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html 14 * @author Andreas Gohr <andi@splitbrain.org> 15 */ 16class action_plugin_smtp extends ActionPlugin 17{ 18 /** 19 * Registers a callback function for a given event 20 * 21 * @param EventHandler $controller DokuWiki's event controller object 22 * @return void 23 */ 24 public function register(EventHandler $controller) 25 { 26 27 $controller->register_hook('MAIL_MESSAGE_SEND', 'BEFORE', $this, 'handleMailMessageSend'); 28 } 29 30 /** 31 * [Custom event handler which performs action] 32 * 33 * @param Event $event event object by reference 34 * @param mixed $param [the parameters passed as fifth argument to register_hook() when this 35 * handler was registered] 36 * @return void 37 */ 38 public function handleMailMessageSend(Event $event, $param) 39 { 40 // prepare the message 41 /** @var Mailer $mailer Our Mailer with all the data */ 42 $mailer = $event->data['mail']; 43 $body = $mailer->dump(); // this also prepares all internal variables of the mailer 44 $rcpt = $event->data['to'] . ',' . 45 $event->data['cc'] . ',' . 46 $event->data['bcc']; 47 $from = $event->data['from']; 48 $message = new Message( 49 $from, 50 $rcpt, 51 $body 52 ); 53 54 // prepare the SMTP communication lib 55 $logger = new Logger(); 56 $smtp = new SMTP($logger); 57 $smtp->setServer( 58 $this->getConf('smtp_host'), 59 $this->getConf('smtp_port'), 60 $this->getConf('smtp_ssl') 61 ); 62 if ($this->getConf('auth_user')) { 63 $smtp->setAuth( 64 $this->getConf('auth_user'), 65 $this->getConf('auth_pass') 66 ); 67 } 68 $smtp->setEhlo( 69 helper_plugin_smtp::getEHLO($this->getConf('localdomain')) 70 ); 71 72 73 // send the message 74 try { 75 $smtp->send($message); 76 $ok = true; 77 } catch (Exception $e) { 78 msg('There was an unexpected problem communicating with SMTP: ' . $e->getMessage(), -1); 79 $ok = false; 80 } 81 82 // give debugging help on error 83 if (!$ok && $this->getConf('debug')) { 84 $log = []; 85 foreach ($logger->getLog() as $line) { 86 $log[] = trim($line[1]); 87 } 88 $log = trim(implode("\n", $log)); 89 msg( 90 'SMTP log:<br /><pre>' . hsc($log) . 91 '</pre><b>Above may contain passwords - do not post online!</b>', 92 -1 93 ); 94 } 95 96 // finish event handling 97 $event->preventDefault(); 98 $event->stopPropagation(); 99 $event->result = $ok; 100 $event->data['success'] = $ok; 101 } 102} 103