php-mailer.php 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538
  1. <?php
  2. // PLUGIN INFORMATION
  3. $GLOBALS['plugins'][]['PHP Mailer'] = array( // Plugin Name
  4. 'name' => 'PHP Mailer', // Plugin Name
  5. 'author' => 'PHP Mailer', // Who wrote the plugin
  6. 'category' => 'Mail', // One to Two Word Description
  7. 'link' => 'https://github.com/PHPMailer/PHPMailer', // Link to plugin info
  8. 'license' => 'personal,business', // License Type use , for multiple
  9. 'idPrefix' => 'PHPMAILER', // html element id prefix
  10. 'configPrefix' => 'PHPMAILER', // config file prefix for array items without the hyphen
  11. 'version' => '1.0.0', // SemVer of plugin
  12. 'image' => 'plugins/images/php-mailer.png', // 1:1 non transparent image for plugin
  13. 'settings' => true, // does plugin need a settings page? true or false
  14. 'homepage' => false // Is plugin for use on homepage? true or false
  15. );
  16. class PhpMailer extends Organizr
  17. {
  18. public function _phpMailerPluginGetEmails()
  19. {
  20. $type = null;
  21. if ($this->config['authBackend']) {
  22. if ($this->config['authBackend'] == 'plex') {
  23. $type = 'plex';
  24. }
  25. }
  26. if ($type == 'plex') {
  27. $emails = array_merge($this->userList('plex')['both'], $this->_phpMailerPluginGetOrgUsers());
  28. } elseif ($type == 'emby') {
  29. $emails = $this->_phpMailerPluginGetOrgUsers();
  30. } else {
  31. $emails = $this->_phpMailerPluginGetOrgUsers();
  32. }
  33. return $emails;
  34. }
  35. public function _phpMailerPluginGetOrgUsers()
  36. {
  37. $return = null;
  38. $result = $this->getAllUsers(true);
  39. if (is_array($result) || is_object($result)) {
  40. foreach ($result['users'] as $k => $v) {
  41. $return[$v['username']] = $v['email'];
  42. }
  43. return ($return) ?? false;
  44. }
  45. }
  46. public function _phpMailerPluginGetTemplates()
  47. {
  48. foreach (glob(dirname(__DIR__, 2) . DIRECTORY_SEPARATOR . 'api' . DIRECTORY_SEPARATOR . 'plugins' . DIRECTORY_SEPARATOR . 'misc' . DIRECTORY_SEPARATOR . 'emailTemplates' . DIRECTORY_SEPARATOR . "*.php") as $filename) {
  49. $templates[] = array(
  50. 'name' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename)),
  51. 'value' => preg_replace('/\\.[^.\\s]{3,4}$/', '', basename($filename))
  52. );
  53. }
  54. return $templates;
  55. }
  56. public function _phpMailerPluginEmailTemplate($emailTemplate)
  57. {
  58. $variables = [
  59. '{user}' => $emailTemplate['user'],
  60. '{domain}' => $this->getServerPath(true),
  61. '{password}' => $emailTemplate['password'],
  62. '{inviteCode}' => $emailTemplate['inviteCode'],
  63. '{fullDomain}' => $this->getServerPath(true),
  64. '{title}' => $this->config['title'],
  65. ];
  66. $emailTemplate['body'] = strtr($emailTemplate['body'], $variables);
  67. $emailTemplate['subject'] = strtr($emailTemplate['subject'], $variables);
  68. return $emailTemplate;
  69. }
  70. public function _phpMailerPluginBuildEmail($email)
  71. {
  72. /** @noinspection PhpUnusedLocalVariableInspection */
  73. $subject = (isset($email['subject'])) ? $email['subject'] : 'Message from Server';
  74. $body = (isset($email['body'])) ? $email['body'] : 'Message Error Occurred';
  75. $type = (isset($email['type'])) ? $email['type'] : 'No Type';
  76. switch ($type) {
  77. case 'invite':
  78. $extra = 'invite';
  79. break;
  80. case 'reset':
  81. $extra = 'reset';
  82. break;
  83. default:
  84. $extra = null;
  85. break;
  86. }
  87. include('misc/emailTemplates/' . $this->config['PHPMAILER-template'] . '.php');
  88. return $email;
  89. }
  90. public function _phpMailerPluginAdminSendEmail($array)
  91. {
  92. if ($this->config['PHPMAILER-enabled']) {
  93. $emailTemplate = array(
  94. 'type' => 'admin',
  95. 'body' => $array['body'],
  96. 'subject' => $array['subject'],
  97. 'user' => null,
  98. 'password' => null,
  99. 'inviteCode' => null,
  100. );
  101. $emailTemplate = $this->_phpMailerPluginEmailTemplate($emailTemplate);
  102. $sendEmail = array(
  103. 'bcc' => $array['bcc'],
  104. 'subject' => $emailTemplate['subject'],
  105. 'body' => $this->_phpMailerPluginBuildEmail($emailTemplate),
  106. );
  107. $response = $this->_phpMailerPluginSendEmail($sendEmail);
  108. if ($response == true) {
  109. $msg = ($this->config['PHPMAILER-debugTesting']) ? $this->config['phpmOriginalDebug'] : 'Email sent';
  110. $this->setAPIResponse('success', $msg, 200);
  111. } else {
  112. $this->setAPIResponse('error', $response, 409);
  113. return false;
  114. }
  115. return true;
  116. } else {
  117. $this->setAPIResponse('error', 'PHP-Mailer is not enabled', 409);
  118. return false;
  119. }
  120. return false;
  121. }
  122. public function _phpMailerPluginGetDebug($str, $level)
  123. {
  124. $this->config['phpmOriginalDebug'] = $this->config['phpmOriginalDebug'] . $str;
  125. return $this->config['phpmOriginalDebug'];
  126. }
  127. public function _phpMailerPluginSendTestEmail()
  128. {
  129. $emailTemplate = array(
  130. 'type' => 'test',
  131. 'body' => 'This is just a test email.',
  132. 'subject' => 'Test E-Mail',
  133. 'user' => null,
  134. 'password' => null,
  135. 'inviteCode' => null,
  136. );
  137. $emailTemplate = $this->_phpMailerPluginEmailTemplate($emailTemplate);
  138. $this->config['phpmOriginalDebug'] = '|||DEBUG|||';
  139. try {
  140. $mail = new PHPMailer\PHPMailer\PHPMailer(true);
  141. $mail->SMTPDebug = 2;
  142. $mail->isSMTP();
  143. $mail->Debugoutput = function ($str, $level) {
  144. $this->_phpMailerPluginGetDebug($str, $level);
  145. };
  146. $mail->Host = $this->config['PHPMAILER-smtpHost'];
  147. $mail->Port = $this->config['PHPMAILER-smtpHostPort'];
  148. if ($this->config['PHPMAILER-smtpHostType'] !== 'n/a') {
  149. $mail->SMTPSecure = $this->config['PHPMAILER-smtpHostType'];
  150. }
  151. $mail->SMTPAuth = $this->config['PHPMAILER-smtpHostAuth'];
  152. $mail->Username = $this->config['PHPMAILER-smtpHostUsername'];
  153. $mail->Password = $this->decrypt($this->config['PHPMAILER-smtpHostPassword']);
  154. $mail->SMTPOptions = array(
  155. 'ssl' => [
  156. 'verify_peer' => $this->config['PHPMAILER-verifyCert'],
  157. 'verify_depth' => 3,
  158. 'allow_self_signed' => true,
  159. 'peer_name' => $this->config['PHPMAILER-smtpHost'],
  160. 'cafile' => $this->getCert(),
  161. ],
  162. );
  163. $mail->setFrom($this->config['PHPMAILER-smtpHostSenderEmail'], $this->config['PHPMAILER-smtpHostSenderName']);
  164. $mail->addReplyTo($this->config['PHPMAILER-smtpHostSenderEmail'], $this->config['PHPMAILER-smtpHostSenderName']);
  165. $mail->isHTML(true);
  166. $mail->addAddress($this->user['email'], $this->user['username']);
  167. $mail->Subject = $emailTemplate['subject'];
  168. $mail->Body = $this->_phpMailerPluginBuildEmail($emailTemplate);
  169. $mail->send();
  170. $this->writeLog('success', 'Mail Function - E-Mail Test Sent', $this->user['username']);
  171. $msg = ($this->config['PHPMAILER-debugTesting']) ? $this->config['phpmOriginalDebug'] : 'Email sent';
  172. $this->setAPIResponse('success', $msg, 200);
  173. return true;
  174. } catch (PHPMailer\PHPMailer\Exception $e) {
  175. $this->writeLog('error', 'Mail Function - E-Mail Test Failed[' . $mail->ErrorInfo . ']', $this->user['username']);
  176. $this->setAPIResponse('error', $e->getMessage(), 500);
  177. return false;
  178. }
  179. return false;
  180. }
  181. public function _phpMailerPluginSendEmail($emailInfo)
  182. {
  183. $to = isset($emailInfo['to']) ? $emailInfo['to'] : null;
  184. $cc = isset($emailInfo['cc']) ? $emailInfo['cc'] : null;
  185. $bcc = isset($emailInfo['bcc']) ? $emailInfo['bcc'] : null;
  186. $subject = isset($emailInfo['subject']) ? $emailInfo['subject'] : null;
  187. $body = isset($emailInfo['body']) ? $emailInfo['body'] : null;
  188. $username = isset($emailInfo['user']) ? $emailInfo['user'] : 'Organizr User';
  189. try {
  190. $mail = new PHPMailer\PHPMailer\PHPMailer(true);
  191. $mail->isSMTP();
  192. //$mail->SMTPDebug = 3;
  193. $mail->Host = $this->config['PHPMAILER-smtpHost'];
  194. $mail->Port = $this->config['PHPMAILER-smtpHostPort'];
  195. if ($this->config['PHPMAILER-smtpHostType'] !== 'n/a') {
  196. $mail->SMTPSecure = $this->config['PHPMAILER-smtpHostType'];
  197. }
  198. $mail->SMTPAuth = $this->config['PHPMAILER-smtpHostAuth'];
  199. $mail->Username = $this->config['PHPMAILER-smtpHostUsername'];
  200. $mail->Password = $this->decrypt($this->config['PHPMAILER-smtpHostPassword']);
  201. $mail->SMTPOptions = array(
  202. 'ssl' => [
  203. 'verify_peer' => $this->config['PHPMAILER-verifyCert'],
  204. 'verify_depth' => 3,
  205. 'allow_self_signed' => true,
  206. 'peer_name' => $this->config['PHPMAILER-smtpHost'],
  207. 'cafile' => $this->getCert(),
  208. ],
  209. );
  210. $mail->setFrom($this->config['PHPMAILER-smtpHostSenderEmail'], $this->config['PHPMAILER-smtpHostSenderName']);
  211. $mail->addReplyTo($this->config['PHPMAILER-smtpHostSenderEmail'], $this->config['PHPMAILER-smtpHostSenderName']);
  212. $mail->isHTML(true);
  213. if ($to) {
  214. $mail->addAddress($to, $username);
  215. }
  216. if ($cc) {
  217. $mail->addCC($cc);
  218. }
  219. if ($bcc) {
  220. if (strpos($bcc, ',') === false) {
  221. $mail->addBCC($bcc);
  222. } else {
  223. $allEmails = explode(",", $bcc);
  224. foreach ($allEmails as $gotEmail) {
  225. $mail->addBCC($gotEmail);
  226. }
  227. }
  228. }
  229. $mail->Subject = $subject;
  230. $mail->Body = $body;
  231. $mail->send();
  232. return true;
  233. } catch (PHPMailer\PHPMailer\Exception $e) {
  234. $this->writeLog('error', 'Mail Function - E-Mail Test Failed[' . $mail->ErrorInfo . ']', $this->user['username']);
  235. return $e->errorMessage();
  236. }
  237. }
  238. /* GET PHPMAILER SETTINGS */
  239. public function _phpMailerPluginGetSettings()
  240. {
  241. return array(
  242. 'Host' => array(
  243. array(
  244. 'type' => 'input',
  245. 'name' => 'PHPMAILER-smtpHost',
  246. 'label' => 'SMTP Host',
  247. 'value' => $this->config['PHPMAILER-smtpHost']
  248. ),
  249. array(
  250. 'type' => 'input',
  251. 'name' => 'PHPMAILER-smtpHostPort',
  252. 'label' => 'SMTP Port',
  253. 'value' => $this->config['PHPMAILER-smtpHostPort']
  254. )
  255. ),
  256. 'Authentication' => array(
  257. array(
  258. 'type' => 'input',
  259. 'name' => 'PHPMAILER-smtpHostUsername',
  260. 'label' => 'Username',
  261. 'value' => $this->config['PHPMAILER-smtpHostUsername']
  262. ),
  263. array(
  264. 'type' => 'password',
  265. 'name' => 'PHPMAILER-smtpHostPassword',
  266. 'label' => 'Password',
  267. 'value' => $this->config['PHPMAILER-smtpHostPassword']
  268. ),
  269. array(
  270. 'type' => 'switch',
  271. 'name' => 'PHPMAILER-smtpHostAuth',
  272. 'label' => 'Authentication',
  273. 'value' => $this->config['PHPMAILER-smtpHostAuth']
  274. ),
  275. array(
  276. 'type' => 'select',
  277. 'name' => 'PHPMAILER-smtpHostType',
  278. 'label' => 'Authentication Type',
  279. 'value' => $this->config['PHPMAILER-smtpHostType'],
  280. 'options' => array(
  281. array(
  282. 'name' => 'tls',
  283. 'value' => 'tls'
  284. ),
  285. array(
  286. 'name' => 'ssl',
  287. 'value' => 'ssl'
  288. ),
  289. array(
  290. 'name' => 'off',
  291. 'value' => 'n/a'
  292. )
  293. )
  294. ),
  295. array(
  296. 'type' => 'switch',
  297. 'name' => 'PHPMAILER-verifyCert',
  298. 'label' => 'Verify Certificate',
  299. 'value' => $this->config['PHPMAILER-verifyCert']
  300. ),
  301. ),
  302. 'Sender Information' => array(
  303. array(
  304. 'type' => 'input',
  305. 'name' => 'PHPMAILER-smtpHostSenderName',
  306. 'label' => 'Sender Name',
  307. 'value' => $this->config['PHPMAILER-smtpHostSenderName']
  308. ),
  309. array(
  310. 'type' => 'input',
  311. 'name' => 'PHPMAILER-smtpHostSenderEmail',
  312. 'label' => 'Sender Email',
  313. 'value' => $this->config['PHPMAILER-smtpHostSenderEmail'],
  314. 'placeholder' => 'i.e. same as username'
  315. )
  316. ),
  317. 'Test & Options' => array(
  318. array(
  319. 'type' => 'button',
  320. 'label' => 'Send Test',
  321. 'class' => 'phpmSendTestEmail',
  322. 'icon' => 'fa fa-paper-plane',
  323. 'text' => 'Send'
  324. ),
  325. array(
  326. 'type' => 'switch',
  327. 'name' => 'PHPMAILER-debugTesting',
  328. 'label' => 'Enable Debug Output on Email Test',
  329. 'value' => $this->config['PHPMAILER-debugTesting'],
  330. ),
  331. array(
  332. 'type' => 'input',
  333. 'name' => 'PHPMAILER-domain',
  334. 'label' => 'Domain Link Override',
  335. 'value' => $this->config['PHPMAILER-domain'],
  336. 'placeholder' => 'https://domain.com/',
  337. ),
  338. array(
  339. 'type' => 'select',
  340. 'name' => 'PHPMAILER-template',
  341. 'label' => 'Theme',
  342. 'value' => $this->config['PHPMAILER-template'],
  343. 'options' => $this->_phpMailerPluginGetTemplates()
  344. ),
  345. array(
  346. 'type' => 'input',
  347. 'name' => 'PHPMAILER-logo',
  348. 'label' => 'WAN Logo URL',
  349. 'value' => $this->config['PHPMAILER-logo'],
  350. 'placeholder' => 'Full URL',
  351. ),
  352. array(
  353. 'type' => 'switch',
  354. 'name' => 'PHPMAILER-emailTemplateRegisterUserEnabled',
  355. 'label' => 'Send Welcome E-Mail',
  356. 'value' => $this->config['PHPMAILER-emailTemplateRegisterUserEnabled'],
  357. ),
  358. ),
  359. 'Templates' => array(
  360. array(
  361. 'type' => 'accordion',
  362. 'label' => 'Edit Template',
  363. 'id' => 'customEmailTemplates',
  364. 'override' => 12,
  365. 'options' => array(
  366. array(
  367. 'id' => 'PHPMAILER-emailTemplateRegisterUserForm',
  368. 'header' => 'New Registration',
  369. 'body' => array(
  370. array(
  371. 'type' => 'input',
  372. 'name' => 'PHPMAILER-emailTemplateRegisterUserSubject',
  373. 'smallLabel' => 'Subject',
  374. 'value' => $this->config['PHPMAILER-emailTemplateRegisterUserSubject'],
  375. ),
  376. array(
  377. 'type' => 'textbox',
  378. 'name' => 'PHPMAILER-emailTemplateRegisterUser',
  379. 'smallLabel' => 'Body',
  380. 'value' => $this->config['PHPMAILER-emailTemplateRegisterUser'],
  381. 'attr' => 'rows="10"',
  382. )
  383. )
  384. ),
  385. array(
  386. 'id' => 'PHPMAILER-emailTemplateResetPasswordForm',
  387. 'header' => 'Reset Password',
  388. 'body' => array(
  389. array(
  390. 'type' => 'input',
  391. 'name' => 'PHPMAILER-emailTemplateResetSubject',
  392. 'smallLabel' => 'Subject',
  393. 'value' => $this->config['PHPMAILER-emailTemplateResetSubject'],
  394. ),
  395. array(
  396. 'type' => 'textbox',
  397. 'name' => 'PHPMAILER-emailTemplateReset',
  398. 'smallLabel' => 'Body',
  399. 'value' => $this->config['PHPMAILER-emailTemplateReset'],
  400. 'attr' => 'rows="10"',
  401. )
  402. )
  403. ),
  404. array(
  405. 'id' => 'PHPMAILER-emailTemplateInviteUserForm',
  406. 'header' => 'Invite User',
  407. 'body' => array(
  408. array(
  409. 'type' => 'input',
  410. 'name' => 'PHPMAILER-emailTemplateInviteUserSubject',
  411. 'smallLabel' => 'Subject',
  412. 'value' => $this->config['PHPMAILER-emailTemplateInviteUserSubject'],
  413. ),
  414. array(
  415. 'type' => 'textbox',
  416. 'name' => 'PHPMAILER-emailTemplateInviteUser',
  417. 'smallLabel' => 'Body',
  418. 'value' => $this->config['PHPMAILER-emailTemplateInviteUser'],
  419. 'attr' => 'rows="10"',
  420. )
  421. )
  422. ),
  423. array(
  424. 'id' => 'PHPMAILER-emailTemplateCustom-include-OneForm',
  425. 'header' => $this->config['PHPMAILER-emailTemplateCustom-include-OneName'],
  426. 'body' => array(
  427. array(
  428. 'type' => 'input',
  429. 'name' => 'PHPMAILER-emailTemplateCustom-include-OneName',
  430. 'smallLabel' => 'Name',
  431. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-OneName'],
  432. ),
  433. array(
  434. 'type' => 'input',
  435. 'name' => 'PHPMAILER-emailTemplateCustom-include-OneSubject',
  436. 'smallLabel' => 'Subject',
  437. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-OneSubject'],
  438. ),
  439. array(
  440. 'type' => 'textbox',
  441. 'name' => 'PHPMAILER-emailTemplateCustom-include-One',
  442. 'smallLabel' => 'Body',
  443. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-One'],
  444. 'attr' => 'rows="10"',
  445. )
  446. )
  447. ),
  448. array(
  449. 'id' => 'PHPMAILER-emailTemplateCustom-include-TwoForm',
  450. 'header' => $this->config['PHPMAILER-emailTemplateCustom-include-TwoName'],
  451. 'body' => array(
  452. array(
  453. 'type' => 'input',
  454. 'name' => 'PHPMAILER-emailTemplateCustom-include-TwoName',
  455. 'smallLabel' => 'Name',
  456. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-TwoName'],
  457. ),
  458. array(
  459. 'type' => 'input',
  460. 'name' => 'PHPMAILER-emailTemplateCustom-include-TwoSubject',
  461. 'smallLabel' => 'Subject',
  462. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-TwoSubject'],
  463. ),
  464. array(
  465. 'type' => 'textbox',
  466. 'name' => 'PHPMAILER-emailTemplateCustom-include-Two',
  467. 'smallLabel' => 'Body',
  468. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-Two'],
  469. 'attr' => 'rows="10"',
  470. )
  471. )
  472. ),
  473. array(
  474. 'id' => 'PHPMAILER-emailTemplateCustom-include-ThreeForm',
  475. 'header' => $this->config['PHPMAILER-emailTemplateCustom-include-ThreeName'],
  476. 'body' => array(
  477. array(
  478. 'type' => 'input',
  479. 'name' => 'PHPMAILER-emailTemplateCustom-include-ThreeName',
  480. 'smallLabel' => 'Name',
  481. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-ThreeName'],
  482. ),
  483. array(
  484. 'type' => 'input',
  485. 'name' => 'PHPMAILER-emailTemplateCustom-include-ThreeSubject',
  486. 'smallLabel' => 'Subject',
  487. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-ThreeSubject'],
  488. ),
  489. array(
  490. 'type' => 'textbox',
  491. 'name' => 'PHPMAILER-emailTemplateCustom-include-Three',
  492. 'smallLabel' => 'Body',
  493. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-Three'],
  494. 'attr' => 'rows="10"',
  495. )
  496. )
  497. ),
  498. array(
  499. 'id' => 'PHPMAILER-emailTemplateCustom-include-FourForm',
  500. 'header' => $this->config['PHPMAILER-emailTemplateCustom-include-FourName'],
  501. 'body' => array(
  502. array(
  503. 'type' => 'input',
  504. 'name' => 'PHPMAILER-emailTemplateCustom-include-FourName',
  505. 'smallLabel' => 'Name',
  506. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-FourName'],
  507. ),
  508. array(
  509. 'type' => 'input',
  510. 'name' => 'PHPMAILER-emailTemplateCustom-include-FourSubject',
  511. 'smallLabel' => 'Subject',
  512. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-FourSubject'],
  513. ),
  514. array(
  515. 'type' => 'textbox',
  516. 'name' => 'PHPMAILER-emailTemplateCustom-include-Four',
  517. 'smallLabel' => 'Body',
  518. 'value' => $this->config['PHPMAILER-emailTemplateCustom-include-Four'],
  519. 'attr' => 'rows="10"',
  520. )
  521. )
  522. ),
  523. )
  524. )
  525. )
  526. );
  527. }
  528. }