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