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 foreach ($recipients as $recipient) { 45 $recipient = trim($recipient); 46 47 if ($recipient[0] === '@') { 48 $this->resolveGroup($resolved, $recipient); 49 } elseif (strpos($recipient, '@') === false) { 50 $this->resolveUser($resolved, $recipient); 51 } else { 52 if (!$this->emailIsValid($recipient)) { 53 throw new \Exception($this->getLang('err_recipient')); 54 } 55 $resolved[] = $recipient; 56 } 57 } 58 return $resolved; 59 } 60 61 /** 62 * @param array $resolved 63 * @param string $recipient 64 * @return void 65 * @throws Exception 66 */ 67 protected function resolveGroup(&$resolved, $recipient) 68 { 69 /** @var AuthPlugin $auth */ 70 global $auth; 71 if (!$auth->canDo('getUsers')) { 72 throw new \Exception('Auth cannot fetch users by group.'); 73 } 74 75 $users = $auth->retrieveUsers(0, 0, ['grps' => substr($recipient, 1)]); 76 foreach ($users as $user) { 77 $resolved[] = $user['mail']; 78 } 79 } 80 81 /** 82 * @param array $resolved 83 * @param string $recipient 84 * @return void 85 */ 86 protected function resolveUser(&$resolved, $recipient) 87 { 88 /** @var AuthPlugin $auth */ 89 global $auth; 90 $user = $auth->getUserData($recipient); 91 if ($user) $resolved[] = $user['mail']; 92 } 93 94 /** 95 * Checks validity of given mail. With config 'wikionly' set to true 96 * also checks if user with this email is known. 97 * 98 * @param $mail 99 * @return bool 100 * @throws Exception 101 */ 102 protected function emailIsValid($mail) 103 { 104 if(!$this->getConf('wikionly')) return mail_isvalid($mail); 105 106 /** @var AuthPlugin $auth */ 107 global $auth; 108 if (!$auth->canDo('getUsers')) { 109 throw new \Exception('Auth cannot fetch users by email.'); 110 } 111 112 $user = $auth->retrieveUsers(0, 1, ['mail' => $mail]); 113 return (bool)$user; 114 } 115} 116