xref: /plugin/recommend/helper/mail.php (revision a2b57a462447c4024f59ed49c1b6a0b5c728daa2)
1<?php
2
3use dokuwiki\Extension\Plugin;
4use dokuwiki\Extension\AuthPlugin;
5
6/**
7 * Mail helper
8 */
9class helper_plugin_recommend_mail extends Plugin
10{
11    /**
12     * @param string $recipient
13     * @param string $sender
14     * @param array $replacements
15     *
16     * @return void
17     */
18    public function sendMail($recipient, $sender, $replacements = [])
19    {
20        global $INPUT;
21
22        $mailtext = file_get_contents($this->localFN('template'));
23
24        /* Limit to two empty lines. */
25        $mailtext = preg_replace('/\n{4,}/', "\n\n\n", $mailtext);
26
27        $mailer = new Mailer();
28        $mailer->bcc($recipient);
29        $mailer->from($sender);
30
31        $subject = $INPUT->str('subject');
32        $mailer->subject($subject);
33        $mailer->setBody($mailtext, $replacements);
34        $mailer->send();
35    }
36
37    /**
38     * Processes recipients from input and returns an array of emails
39     * with user groups resolved to individual users
40     *
41     * @param string $recipients
42     * @return array
43     * @throws Exception
44     */
45    public function resolveRecipients($recipients)
46    {
47        $resolved = [];
48
49        $recipients = explode(',', trim($recipients," ,"));
50
51        foreach ($recipients as $recipient) {
52            $recipient = trim($recipient);
53
54            if ($recipient[0] === '@') {
55                $this->resolveGroup($resolved, $recipient);
56            } elseif (!str_contains($recipient, '@')) {
57                $this->resolveUser($resolved, $recipient);
58            } else {
59                if (!$this->emailIsValid($recipient)) {
60                    throw new \Exception($this->getLang('err_recipient'));
61                }
62                $resolved[] = $recipient;
63            }
64        }
65        return $resolved;
66    }
67
68    /**
69     * @param array $resolved
70     * @param string $recipient
71     * @return void
72     * @throws Exception
73     */
74    protected function resolveGroup(&$resolved, $recipient)
75    {
76        /** @var AuthPlugin $auth */
77        global $auth;
78        if (!$auth->canDo('getUsers')) {
79            throw new \Exception('Auth cannot fetch users by group.');
80        }
81
82        $users = $auth->retrieveUsers(0, 0, ['grps' => substr($recipient, 1)]);
83        foreach ($users as $user) {
84            $resolved[] = $user['mail'];
85        }
86    }
87
88    /**
89     * @param array $resolved
90     * @param string $recipient
91     * @return void
92     */
93    protected function resolveUser(&$resolved, $recipient)
94    {
95        /** @var AuthPlugin $auth */
96        global $auth;
97        $user = $auth->getUserData($recipient);
98        if ($user) $resolved[] = $user['mail'];
99    }
100
101    /**
102     * Checks validity of given mail. With config 'wikionly' set to true
103     * also checks if user with this email is known.
104     *
105     * @param $mail
106     * @return bool
107     * @throws Exception
108     */
109    protected function emailIsValid($mail)
110    {
111        if (!$this->getConf('wikionly')) return mail_isvalid($mail);
112
113        /** @var AuthPlugin $auth */
114        global $auth;
115        if (!$auth->canDo('getUsers')) {
116            throw new \Exception('Auth cannot fetch users by email.');
117        }
118
119        $user = $auth->retrieveUsers(0, 1, ['mail' => $mail]);
120        return (bool)$user;
121    }
122}
123