php-mailer.php 16 KB

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