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 $this->getConf('smtp_allow_insecure') 62 ); 63 if ($this->getConf('auth_user')) { 64 $smtp->setAuth( 65 $this->getConf('auth_user'), 66 $this->getConf('auth_pass') 67 ); 68 } 69 $smtp->setEhlo( 70 helper_plugin_smtp::getEHLO($this->getConf('localdomain')) 71 ); 72 73 74 // send the message 75 try { 76 $smtp->send($message); 77 $ok = true; 78 } catch (Exception $e) { 79 msg('There was an unexpected problem communicating with SMTP: ' . $e->getMessage(), -1); 80 $ok = false; 81 } 82 83 // give debugging help on error 84 if (!$ok && $this->getConf('debug')) { 85 $log = []; 86 foreach ($logger->getLog() as $line) { 87 $log[] = trim($line[1]); 88 } 89 $log = trim(implode("\n", $log)); 90 msg( 91 'SMTP log:<br /><pre>' . hsc($log) . 92 '</pre><b>Above may contain passwords - do not post online!</b>', 93 -1 94 ); 95 } 96 97 // finish event handling 98 $event->preventDefault(); 99 $event->stopPropagation(); 100 $event->result = $ok; 101 $event->data['success'] = $ok; 102 } 103} 104