xref: /plugin/smtp/action.php (revision 7ad9344f5bc38cfa8457cb31ea0888694f7856dc)
14dc22474SAndreas Gohr<?php
24dc22474SAndreas Gohr/**
34dc22474SAndreas Gohr * DokuWiki Plugin smtp (Action Component)
44dc22474SAndreas Gohr *
54dc22474SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
64dc22474SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
74dc22474SAndreas Gohr */
84dc22474SAndreas Gohr
94dc22474SAndreas Gohr// must be run within Dokuwiki
104dc22474SAndreas Gohrif(!defined('DOKU_INC')) die();
114dc22474SAndreas Gohr
124dc22474SAndreas Gohrclass action_plugin_smtp extends DokuWiki_Action_Plugin {
134dc22474SAndreas Gohr
144dc22474SAndreas Gohr    /**
154dc22474SAndreas Gohr     * Registers a callback function for a given event
164dc22474SAndreas Gohr     *
174dc22474SAndreas Gohr     * @param Doku_Event_Handler $controller DokuWiki's event controller object
184dc22474SAndreas Gohr     * @return void
194dc22474SAndreas Gohr     */
204dc22474SAndreas Gohr    public function register(Doku_Event_Handler $controller) {
214dc22474SAndreas Gohr
220b7ac7c9SAndreas Gohr       $controller->register_hook('MAIL_MESSAGE_SEND', 'BEFORE', $this, 'handle_mail_message_send');
234dc22474SAndreas Gohr
244dc22474SAndreas Gohr    }
254dc22474SAndreas Gohr
264dc22474SAndreas Gohr    /**
274dc22474SAndreas Gohr     * [Custom event handler which performs action]
284dc22474SAndreas Gohr     *
294dc22474SAndreas Gohr     * @param Doku_Event $event  event object by reference
304dc22474SAndreas Gohr     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
314dc22474SAndreas Gohr     *                           handler was registered]
324dc22474SAndreas Gohr     * @return void
334dc22474SAndreas Gohr     */
344dc22474SAndreas Gohr
354dc22474SAndreas Gohr    public function handle_mail_message_send(Doku_Event &$event, $param) {
360b7ac7c9SAndreas Gohr        require_once __DIR__ . '/loader.php';
370b7ac7c9SAndreas Gohr
386ab179d2SAndreas Gohr        // prepare the message
390b7ac7c9SAndreas Gohr        /** @var Mailer $mailer Our Mailer with all the data */
400b7ac7c9SAndreas Gohr        $mailer = $event->data['mail'];
4125673c62SAndreas Gohr        $rcpt   = $mailer->cleanAddress($event->data['to']) . ',' .
4225673c62SAndreas Gohr                  $mailer->cleanAddress($event->data['cc']) . ',' .
4325673c62SAndreas Gohr                  $mailer->cleanAddress($event->data['bcc']);
440b7ac7c9SAndreas Gohr        $from   = $event->data['from'];
450b7ac7c9SAndreas Gohr        $message = new \splitbrain\dokuwiki\plugin\smtp\Message(
460b7ac7c9SAndreas Gohr            $from,
4725673c62SAndreas Gohr            $rcpt,
480b7ac7c9SAndreas Gohr            $mailer->dump()
490b7ac7c9SAndreas Gohr        );
500b7ac7c9SAndreas Gohr
516ab179d2SAndreas Gohr        // prepare the SMTP communication lib
526ab179d2SAndreas Gohr        $logger = new \splitbrain\dokuwiki\plugin\smtp\Logger();
536ab179d2SAndreas Gohr        $smtp = new \Tx\Mailer\SMTP($logger);
546ab179d2SAndreas Gohr        $smtp->setServer(
556ab179d2SAndreas Gohr            $this->getConf('smtp_host'),
566ab179d2SAndreas Gohr            $this->getConf('smtp_port'),
576ab179d2SAndreas Gohr            $this->getConf('smtp_ssl')
586ab179d2SAndreas Gohr        );
596ab179d2SAndreas Gohr        if($this->getConf('auth_user')){
606ab179d2SAndreas Gohr            $smtp->setAuth(
616ab179d2SAndreas Gohr                $this->getConf('auth_user'),
626ab179d2SAndreas Gohr                $this->getConf('auth_pass')
636ab179d2SAndreas Gohr            );
646ab179d2SAndreas Gohr        }
65*7ad9344fSAndreas Gohr        $smtp->setEhlo(
66*7ad9344fSAndreas Gohr            helper_plugin_smtp::getEHLO($this->getConf('localdomain'))
67*7ad9344fSAndreas Gohr        );
6827827474SAndreas Gohr
6927827474SAndreas Gohr
706ab179d2SAndreas Gohr        // send the message
716ab179d2SAndreas Gohr        try {
720b7ac7c9SAndreas Gohr            $smtp->send($message);
736ab179d2SAndreas Gohr            $ok = true;
746ab179d2SAndreas Gohr        } catch (Exception $e) {
756ab179d2SAndreas Gohr            msg('There was an unexpected problem communicating with SMTP: '.$e->getMessage(), -1);
766ab179d2SAndreas Gohr            $ok = false;
776ab179d2SAndreas Gohr        }
786ab179d2SAndreas Gohr
796ab179d2SAndreas Gohr        // give debugging help on error
806ab179d2SAndreas Gohr        if(!$ok && $this->getConf('debug')) {
816ab179d2SAndreas Gohr            $log = array();
826ab179d2SAndreas Gohr            foreach($logger->getLog() as $line) {
836ab179d2SAndreas Gohr                $log[] = $line[1];
846ab179d2SAndreas Gohr            }
856ab179d2SAndreas Gohr            $log = join("\n", $log);
866ab179d2SAndreas Gohr            msg('SwiftMailer log:<br /><pre>'.hsc($log).'</pre>',-1);
876ab179d2SAndreas Gohr        }
886ab179d2SAndreas Gohr
896ab179d2SAndreas Gohr        // finish event handling
906ab179d2SAndreas Gohr        $event->preventDefault();
916ab179d2SAndreas Gohr        $event->stopPropagation();
926ab179d2SAndreas Gohr        $event->result = $ok;
936ab179d2SAndreas Gohr        $event->data['success'] = $ok;
944dc22474SAndreas Gohr    }
954dc22474SAndreas Gohr
964dc22474SAndreas Gohr}
974dc22474SAndreas Gohr
984dc22474SAndreas Gohr// vim:ts=4:sw=4:et:
99