xref: /plugin/recommend/helper/mail.php (revision 5187ba70cd5a5dd1c55421693af3a2580f04e1f3)
1<?php
2
3use dokuwiki\Extension\AuthPlugin;
4
5/**
6 * Mail helper
7 */
8class helper_plugin_recommend_mail extends DokuWiki_Plugin
9{
10    /**
11     * @param string $recipient
12     * @param string $mailtext
13     * @param string $sender
14     * @return void
15     */
16    public function sendMail($recipient, $mailtext, $sender)
17    {
18        global $INPUT;
19
20        $mailer = new Mailer();
21        $mailer->bcc($recipient);
22        $mailer->from($sender);
23
24        $subject = $INPUT->str('subject');
25        $mailer->subject($subject);
26        $mailer->setBody($mailtext);
27        $mailer->send();
28    }
29
30    /**
31     * Processes recipients from input and returns an array of emails
32     * with user groups resolved to individual users
33     *
34     * @param string $recipients
35     * @return array
36     * @throws Exception
37     */
38    public function resolveRecipients($recipients)
39    {
40        $resolved = [];
41
42        $recipients = explode(',', $recipients);
43
44        /** @var AuthPlugin $auth */
45        global $auth;
46
47        foreach ($recipients as $recipient) {
48            $recipient = trim($recipient);
49
50            if ($recipient[0] === '@') {
51                $users = $auth->retrieveUsers(0, 0, ['grps' => substr($recipient, 1)]);
52                foreach ($users as $user) {
53                    $resolved[] = $user['mail'];
54                }
55            } else {
56                if (!mail_isvalid($recipient)) {
57                    throw new \Exception($this->getLang('err_recipient'));
58                }
59                $resolved[] = $recipient;
60            }
61        }
62        return $resolved;
63    }
64}
65