xref: /plugin/smtp/action.php (revision 6ab179d20eb5805745647ce412329c70cd97c74b)
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
38*6ab179d2SAndreas 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
51*6ab179d2SAndreas Gohr        // prepare the SMTP communication lib
52*6ab179d2SAndreas Gohr        $logger = new \splitbrain\dokuwiki\plugin\smtp\Logger();
53*6ab179d2SAndreas Gohr        $smtp = new \Tx\Mailer\SMTP($logger);
54*6ab179d2SAndreas Gohr        $smtp->setServer(
55*6ab179d2SAndreas Gohr            $this->getConf('smtp_host'),
56*6ab179d2SAndreas Gohr            $this->getConf('smtp_port'),
57*6ab179d2SAndreas Gohr            $this->getConf('smtp_ssl')
58*6ab179d2SAndreas Gohr        );
59*6ab179d2SAndreas Gohr        if($this->getConf('auth_user')){
60*6ab179d2SAndreas Gohr            $smtp->setAuth(
61*6ab179d2SAndreas Gohr                $this->getConf('auth_user'),
62*6ab179d2SAndreas Gohr                $this->getConf('auth_pass')
63*6ab179d2SAndreas Gohr            );
64*6ab179d2SAndreas Gohr        }
650b7ac7c9SAndreas Gohr
66*6ab179d2SAndreas Gohr        // send the message
67*6ab179d2SAndreas Gohr        try {
680b7ac7c9SAndreas Gohr            $smtp->send($message);
69*6ab179d2SAndreas Gohr            $ok = true;
70*6ab179d2SAndreas Gohr        } catch (Exception $e) {
71*6ab179d2SAndreas Gohr            msg('There was an unexpected problem communicating with SMTP: '.$e->getMessage(), -1);
72*6ab179d2SAndreas Gohr            $ok = false;
73*6ab179d2SAndreas Gohr        }
74*6ab179d2SAndreas Gohr
75*6ab179d2SAndreas Gohr        // give debugging help on error
76*6ab179d2SAndreas Gohr        if(!$ok && $this->getConf('debug')) {
77*6ab179d2SAndreas Gohr            $log = array();
78*6ab179d2SAndreas Gohr            foreach($logger->getLog() as $line) {
79*6ab179d2SAndreas Gohr                $log[] = $line[1];
80*6ab179d2SAndreas Gohr            }
81*6ab179d2SAndreas Gohr            $log = join("\n", $log);
82*6ab179d2SAndreas Gohr            msg('SwiftMailer log:<br /><pre>'.hsc($log).'</pre>',-1);
83*6ab179d2SAndreas Gohr        }
84*6ab179d2SAndreas Gohr
85*6ab179d2SAndreas Gohr        // finish event handling
86*6ab179d2SAndreas Gohr        $event->preventDefault();
87*6ab179d2SAndreas Gohr        $event->stopPropagation();
88*6ab179d2SAndreas Gohr        $event->result = $ok;
89*6ab179d2SAndreas Gohr        $event->data['success'] = $ok;
904dc22474SAndreas Gohr    }
914dc22474SAndreas Gohr
924dc22474SAndreas Gohr}
934dc22474SAndreas Gohr
944dc22474SAndreas Gohr// vim:ts=4:sw=4:et:
95