xref: /plugin/smtp/action.php (revision 8052a8f638c8166f8bce9343fa25892001340bf5)
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'),
606ab179d2SAndreas Gohr            $this->getConf('smtp_ssl')
616ab179d2SAndreas Gohr        );
626ab179d2SAndreas Gohr        if ($this->getConf('auth_user')) {
636ab179d2SAndreas Gohr            $smtp->setAuth(
646ab179d2SAndreas Gohr                $this->getConf('auth_user'),
656ab179d2SAndreas Gohr                $this->getConf('auth_pass')
666ab179d2SAndreas Gohr            );
676ab179d2SAndreas Gohr        }
687ad9344fSAndreas Gohr        $smtp->setEhlo(
697ad9344fSAndreas Gohr            helper_plugin_smtp::getEHLO($this->getConf('localdomain'))
707ad9344fSAndreas Gohr        );
7127827474SAndreas Gohr
7227827474SAndreas Gohr
736ab179d2SAndreas Gohr        // send the message
746ab179d2SAndreas Gohr        try {
750b7ac7c9SAndreas Gohr            $smtp->send($message);
766ab179d2SAndreas Gohr            $ok = true;
776ab179d2SAndreas Gohr        } catch (Exception $e) {
786ab179d2SAndreas Gohr            msg('There was an unexpected problem communicating with SMTP: ' . $e->getMessage(), -1);
796ab179d2SAndreas Gohr            $ok = false;
806ab179d2SAndreas Gohr        }
816ab179d2SAndreas Gohr
826ab179d2SAndreas Gohr        // give debugging help on error
836ab179d2SAndreas Gohr        if (!$ok && $this->getConf('debug')) {
84ee5c0205SAndreas Gohr            $log = [];
856ab179d2SAndreas Gohr            foreach ($logger->getLog() as $line) {
86e6fd30d8SAndreas Gohr                $log[] = trim($line[1]);
876ab179d2SAndreas Gohr            }
88ee5c0205SAndreas Gohr            $log = trim(implode("\n", $log));
89*8052a8f6SAndreas Gohr            msg(
90*8052a8f6SAndreas Gohr                'SMTP log:<br /><pre>' . hsc($log) .
91*8052a8f6SAndreas Gohr                '</pre><b>Above may contain passwords - do not post online!</b>',
92*8052a8f6SAndreas Gohr                -1
93*8052a8f6SAndreas Gohr            );
946ab179d2SAndreas Gohr        }
956ab179d2SAndreas Gohr
966ab179d2SAndreas Gohr        // finish event handling
976ab179d2SAndreas Gohr        $event->preventDefault();
986ab179d2SAndreas Gohr        $event->stopPropagation();
996ab179d2SAndreas Gohr        $event->result = $ok;
1006ab179d2SAndreas Gohr        $event->data['success'] = $ok;
1014dc22474SAndreas Gohr    }
1024dc22474SAndreas Gohr}
103