xref: /plugin/smtp/action.php (revision 30b0aec1b43572b8f5091314125a38d68397b736)
14dc22474SAndreas Gohr<?php
2ee5c0205SAndreas Gohr
3ee5c0205SAndreas Gohruse dokuwiki\Extension\ActionPlugin;
4ee5c0205SAndreas Gohruse dokuwiki\Extension\EventHandler;
5ee5c0205SAndreas Gohruse dokuwiki\Extension\Event;
6ee5c0205SAndreas Gohruse splitbrain\dokuwiki\plugin\smtp\Message;
7ee5c0205SAndreas Gohruse splitbrain\dokuwiki\plugin\smtp\Logger;
8ee5c0205SAndreas Gohruse Tx\Mailer\SMTP;
9ee5c0205SAndreas Gohr
104dc22474SAndreas Gohr/**
114dc22474SAndreas Gohr * DokuWiki Plugin smtp (Action Component)
124dc22474SAndreas Gohr *
134dc22474SAndreas Gohr * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
144dc22474SAndreas Gohr * @author  Andreas Gohr <andi@splitbrain.org>
154dc22474SAndreas Gohr */
16ee5c0205SAndreas Gohrclass action_plugin_smtp extends ActionPlugin
17ee5c0205SAndreas Gohr{
184dc22474SAndreas Gohr    /**
194dc22474SAndreas Gohr     * Registers a callback function for a given event
204dc22474SAndreas Gohr     *
21ee5c0205SAndreas Gohr     * @param EventHandler $controller DokuWiki's event controller object
224dc22474SAndreas Gohr     * @return void
234dc22474SAndreas Gohr     */
24ee5c0205SAndreas Gohr    public function register(EventHandler $controller)
25ee5c0205SAndreas Gohr    {
264dc22474SAndreas Gohr
27ee5c0205SAndreas Gohr        $controller->register_hook('MAIL_MESSAGE_SEND', 'BEFORE', $this, 'handleMailMessageSend');
284dc22474SAndreas Gohr    }
294dc22474SAndreas Gohr
304dc22474SAndreas Gohr    /**
314dc22474SAndreas Gohr     * [Custom event handler which performs action]
324dc22474SAndreas Gohr     *
33ee5c0205SAndreas Gohr     * @param Event $event event object by reference
344dc22474SAndreas Gohr     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
354dc22474SAndreas Gohr     *                           handler was registered]
364dc22474SAndreas Gohr     * @return void
374dc22474SAndreas Gohr     */
38ee5c0205SAndreas Gohr    public function handleMailMessageSend(Event $event, $param)
39ee5c0205SAndreas Gohr    {
406ab179d2SAndreas Gohr        // prepare the message
410b7ac7c9SAndreas Gohr        /** @var Mailer $mailer Our Mailer with all the data */
420b7ac7c9SAndreas Gohr        $mailer = $event->data['mail'];
43677cce93SAndreas Gohr        $body = $mailer->dump();  // this also prepares all internal variables of the mailer
44677cce93SAndreas Gohr        $rcpt   = $event->data['to'] . ',' .
45677cce93SAndreas Gohr                  $event->data['cc'] . ',' .
46677cce93SAndreas Gohr                  $event->data['bcc'];
470b7ac7c9SAndreas Gohr        $from   = $event->data['from'];
48ee5c0205SAndreas Gohr        $message = new Message(
490b7ac7c9SAndreas Gohr            $from,
5025673c62SAndreas Gohr            $rcpt,
51677cce93SAndreas Gohr            $body
520b7ac7c9SAndreas Gohr        );
530b7ac7c9SAndreas Gohr
546ab179d2SAndreas Gohr        // prepare the SMTP communication lib
55ee5c0205SAndreas Gohr        $logger = new Logger();
56ee5c0205SAndreas Gohr        $smtp = new SMTP($logger);
576ab179d2SAndreas Gohr        $smtp->setServer(
586ab179d2SAndreas Gohr            $this->getConf('smtp_host'),
596ab179d2SAndreas Gohr            $this->getConf('smtp_port'),
60*30b0aec1SAndreas Gohr            $this->getConf('smtp_ssl'),
61*30b0aec1SAndreas Gohr            $this->getConf('smtp_allow_insecure')
626ab179d2SAndreas Gohr        );
636ab179d2SAndreas Gohr        if ($this->getConf('auth_user')) {
646ab179d2SAndreas Gohr            $smtp->setAuth(
656ab179d2SAndreas Gohr                $this->getConf('auth_user'),
666ab179d2SAndreas Gohr                $this->getConf('auth_pass')
676ab179d2SAndreas Gohr            );
686ab179d2SAndreas Gohr        }
697ad9344fSAndreas Gohr        $smtp->setEhlo(
707ad9344fSAndreas Gohr            helper_plugin_smtp::getEHLO($this->getConf('localdomain'))
717ad9344fSAndreas Gohr        );
7227827474SAndreas Gohr
7327827474SAndreas Gohr
746ab179d2SAndreas Gohr        // send the message
756ab179d2SAndreas Gohr        try {
760b7ac7c9SAndreas Gohr            $smtp->send($message);
776ab179d2SAndreas Gohr            $ok = true;
786ab179d2SAndreas Gohr        } catch (Exception $e) {
796ab179d2SAndreas Gohr            msg('There was an unexpected problem communicating with SMTP: ' . $e->getMessage(), -1);
806ab179d2SAndreas Gohr            $ok = false;
816ab179d2SAndreas Gohr        }
826ab179d2SAndreas Gohr
836ab179d2SAndreas Gohr        // give debugging help on error
846ab179d2SAndreas Gohr        if (!$ok && $this->getConf('debug')) {
85ee5c0205SAndreas Gohr            $log = [];
866ab179d2SAndreas Gohr            foreach ($logger->getLog() as $line) {
87e6fd30d8SAndreas Gohr                $log[] = trim($line[1]);
886ab179d2SAndreas Gohr            }
89ee5c0205SAndreas Gohr            $log = trim(implode("\n", $log));
908052a8f6SAndreas Gohr            msg(
918052a8f6SAndreas Gohr                'SMTP log:<br /><pre>' . hsc($log) .
928052a8f6SAndreas Gohr                '</pre><b>Above may contain passwords - do not post online!</b>',
938052a8f6SAndreas Gohr                -1
948052a8f6SAndreas Gohr            );
956ab179d2SAndreas Gohr        }
966ab179d2SAndreas Gohr
976ab179d2SAndreas Gohr        // finish event handling
986ab179d2SAndreas Gohr        $event->preventDefault();
996ab179d2SAndreas Gohr        $event->stopPropagation();
1006ab179d2SAndreas Gohr        $event->result = $ok;
1016ab179d2SAndreas Gohr        $event->data['success'] = $ok;
1024dc22474SAndreas Gohr    }
1034dc22474SAndreas Gohr}
104