1<?php
2/**
3 * DokuWiki Plugin smtp (Action Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Gohr <andi@splitbrain.org>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12class action_plugin_smtp extends DokuWiki_Action_Plugin {
13
14    /**
15     * Registers a callback function for a given event
16     *
17     * @param Doku_Event_Handler $controller DokuWiki's event controller object
18     * @return void
19     */
20    public function register(Doku_Event_Handler $controller) {
21
22       $controller->register_hook('MAIL_MESSAGE_SEND', 'BEFORE', $this, 'handle_mail_message_send');
23
24    }
25
26    /**
27     * [Custom event handler which performs action]
28     *
29     * @param Doku_Event $event  event object by reference
30     * @param mixed      $param  [the parameters passed as fifth argument to register_hook() when this
31     *                           handler was registered]
32     * @return void
33     */
34
35    public function handle_mail_message_send(Doku_Event &$event, $param) {
36        require_once __DIR__ . '/loader.php';
37
38        // prepare the message
39        /** @var Mailer $mailer Our Mailer with all the data */
40        $mailer = $event->data['mail'];
41        $body = $mailer->dump();  // this also prepares all internal variables of the mailer
42        $rcpt   = $event->data['to'] . ',' .
43                  $event->data['cc'] . ',' .
44                  $event->data['bcc'];
45        $from   = $event->data['from'];
46        $message = new \splitbrain\dokuwiki\plugin\smtp\Message(
47            $from,
48            $rcpt,
49            $body
50        );
51
52        // prepare the SMTP communication lib
53        $logger = new \splitbrain\dokuwiki\plugin\smtp\Logger();
54        $smtp = new \Tx\Mailer\SMTP($logger);
55        $smtp->setServer(
56            $this->getConf('smtp_host'),
57            $this->getConf('smtp_port'),
58            $this->getConf('smtp_ssl')
59        );
60        if($this->getConf('auth_user')){
61            $smtp->setAuth(
62                $this->getConf('auth_user'),
63                $this->getConf('auth_pass')
64            );
65        }
66        $smtp->setEhlo(
67            helper_plugin_smtp::getEHLO($this->getConf('localdomain'))
68        );
69
70
71        // send the message
72        try {
73            $smtp->send($message);
74            $ok = true;
75        } catch (Exception $e) {
76            msg('There was an unexpected problem communicating with SMTP: '.$e->getMessage(), -1);
77            $ok = false;
78        }
79
80        // give debugging help on error
81        if(!$ok && $this->getConf('debug')) {
82            $log = array();
83            foreach($logger->getLog() as $line) {
84                $log[] = trim($line[1]);
85            }
86            $log = trim(join("\n", $log));
87            msg('SMTP log:<br /><pre>'.hsc($log).'</pre><b>Above may contain passwords - do not post online!</b>',-1);
88        }
89
90        // finish event handling
91        $event->preventDefault();
92        $event->stopPropagation();
93        $event->result = $ok;
94        $event->data['success'] = $ok;
95    }
96
97}
98
99// vim:ts=4:sw=4:et:
100