php-mailer.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  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 phpmSendTestEmail()
  106. {
  107. $emailTemplate = array(
  108. 'type' => 'test',
  109. 'body' => 'This is just a test email.',
  110. 'subject' => 'Test E-Mail',
  111. 'user' => null,
  112. 'password' => null,
  113. 'inviteCode' => null,
  114. );
  115. $emailTemplate = phpmEmailTemplate($emailTemplate);
  116. try {
  117. $mail = new PHPMailer\PHPMailer\PHPMailer(true);
  118. $mail->isSMTP();
  119. //$mail->SMTPDebug = 3;
  120. $mail->Host = $GLOBALS['PHPMAILER-smtpHost'];
  121. $mail->Port = $GLOBALS['PHPMAILER-smtpHostPort'];
  122. if ($GLOBALS['PHPMAILER-smtpHostType'] !== 'n/a') {
  123. $mail->SMTPSecure = $GLOBALS['PHPMAILER-smtpHostType'];
  124. }
  125. $mail->SMTPAuth = $GLOBALS['PHPMAILER-smtpHostAuth'];
  126. $mail->Username = $GLOBALS['PHPMAILER-smtpHostUsername'];
  127. $mail->Password = decrypt($GLOBALS['PHPMAILER-smtpHostPassword']);
  128. $mail->SMTPOptions = array(
  129. 'ssl' => [
  130. 'verify_peer' => $GLOBALS['PHPMAILER-verifyCert'],
  131. 'verify_depth' => 3,
  132. 'allow_self_signed' => true,
  133. 'peer_name' => $GLOBALS['PHPMAILER-smtpHost'],
  134. 'cafile' => getCert(),
  135. ],
  136. );
  137. $mail->setFrom($GLOBALS['PHPMAILER-smtpHostSenderEmail'], $GLOBALS['PHPMAILER-smtpHostSenderName']);
  138. $mail->addReplyTo($GLOBALS['PHPMAILER-smtpHostSenderEmail'], $GLOBALS['PHPMAILER-smtpHostSenderName']);
  139. $mail->isHTML(true);
  140. $mail->addAddress($GLOBALS['organizrUser']['email'], $GLOBALS['organizrUser']['username']);
  141. $mail->Subject = $emailTemplate['subject'];
  142. $mail->Body = phpmBuildEmail($emailTemplate);
  143. $mail->send();
  144. writeLog('success', 'Mail Function - E-Mail Test Sent', $GLOBALS['organizrUser']['username']);
  145. return true;
  146. } catch (PHPMailer\PHPMailer\Exception $e) {
  147. writeLog('error', 'Mail Function - E-Mail Test Failed[' . $mail->ErrorInfo . ']', $GLOBALS['organizrUser']['username']);
  148. return $e->errorMessage();
  149. }
  150. return false;
  151. }
  152. function phpmSendEmail($emailInfo)
  153. {
  154. $to = isset($emailInfo['to']) ? $emailInfo['to'] : null;
  155. $cc = isset($emailInfo['cc']) ? $emailInfo['cc'] : null;
  156. $bcc = isset($emailInfo['bcc']) ? $emailInfo['bcc'] : null;
  157. $subject = isset($emailInfo['subject']) ? $emailInfo['subject'] : null;
  158. $body = isset($emailInfo['body']) ? $emailInfo['body'] : null;
  159. $username = isset($emailInfo['user']) ? $emailInfo['user'] : 'Organizr User';
  160. try {
  161. $mail = new PHPMailer\PHPMailer\PHPMailer(true);
  162. $mail->isSMTP();
  163. //$mail->SMTPDebug = 3;
  164. $mail->Host = $GLOBALS['PHPMAILER-smtpHost'];
  165. $mail->Port = $GLOBALS['PHPMAILER-smtpHostPort'];
  166. if ($GLOBALS['PHPMAILER-smtpHostType'] !== 'n/a') {
  167. $mail->SMTPSecure = $GLOBALS['PHPMAILER-smtpHostType'];
  168. }
  169. $mail->SMTPAuth = $GLOBALS['PHPMAILER-smtpHostAuth'];
  170. $mail->Username = $GLOBALS['PHPMAILER-smtpHostUsername'];
  171. $mail->Password = decrypt($GLOBALS['PHPMAILER-smtpHostPassword']);
  172. $mail->SMTPOptions = array(
  173. 'ssl' => [
  174. 'verify_peer' => $GLOBALS['PHPMAILER-verifyCert'],
  175. 'verify_depth' => 3,
  176. 'allow_self_signed' => true,
  177. 'peer_name' => $GLOBALS['PHPMAILER-smtpHost'],
  178. 'cafile' => getCert(),
  179. ],
  180. );
  181. $mail->setFrom($GLOBALS['PHPMAILER-smtpHostSenderEmail'], $GLOBALS['PHPMAILER-smtpHostSenderName']);
  182. $mail->addReplyTo($GLOBALS['PHPMAILER-smtpHostSenderEmail'], $GLOBALS['PHPMAILER-smtpHostSenderName']);
  183. $mail->isHTML(true);
  184. if ($to) {
  185. $mail->addAddress($to, $username);
  186. }
  187. if ($cc) {
  188. $mail->addCC($cc);
  189. }
  190. if ($bcc) {
  191. if (strpos($bcc, ',') === false) {
  192. $mail->addBCC($bcc);
  193. } else {
  194. $allEmails = explode(",", $bcc);
  195. foreach ($allEmails as $gotEmail) {
  196. $mail->addBCC($gotEmail);
  197. }
  198. }
  199. }
  200. $mail->Subject = $subject;
  201. $mail->Body = $body;
  202. $mail->send();
  203. //writeLog('success', 'Mail Function - E-Mail Test Sent', $GLOBALS['organizrUser']['username']);
  204. return true;
  205. } catch (PHPMailer\PHPMailer\Exception $e) {
  206. writeLog('error', 'Mail Function - E-Mail Test Failed[' . $mail->ErrorInfo . ']', $GLOBALS['organizrUser']['username']);
  207. return $e->errorMessage();
  208. }
  209. return false;
  210. }
  211. /* GET PHPMAILER SETTINGS */
  212. function phpmGetSettings()
  213. {
  214. return array(
  215. 'Host' => array(
  216. array(
  217. 'type' => 'input',
  218. 'name' => 'PHPMAILER-smtpHost',
  219. 'label' => 'SMTP Host',
  220. 'value' => $GLOBALS['PHPMAILER-smtpHost']
  221. ),
  222. array(
  223. 'type' => 'input',
  224. 'name' => 'PHPMAILER-smtpHostPort',
  225. 'label' => 'SMTP Port',
  226. 'value' => $GLOBALS['PHPMAILER-smtpHostPort']
  227. )
  228. ),
  229. 'Authentication' => array(
  230. array(
  231. 'type' => 'input',
  232. 'name' => 'PHPMAILER-smtpHostUsername',
  233. 'label' => 'Username',
  234. 'value' => $GLOBALS['PHPMAILER-smtpHostUsername']
  235. ),
  236. array(
  237. 'type' => 'password',
  238. 'name' => 'PHPMAILER-smtpHostPassword',
  239. 'label' => 'Password',
  240. 'value' => $GLOBALS['PHPMAILER-smtpHostPassword']
  241. ),
  242. array(
  243. 'type' => 'switch',
  244. 'name' => 'PHPMAILER-smtpHostAuth',
  245. 'label' => 'Authentication',
  246. 'value' => $GLOBALS['PHPMAILER-smtpHostAuth']
  247. ),
  248. array(
  249. 'type' => 'select',
  250. 'name' => 'PHPMAILER-smtpHostType',
  251. 'label' => 'Authentication Type',
  252. 'value' => $GLOBALS['PHPMAILER-smtpHostType'],
  253. 'options' => array(
  254. array(
  255. 'name' => 'tls',
  256. 'value' => 'tls'
  257. ),
  258. array(
  259. 'name' => 'ssl',
  260. 'value' => 'ssl'
  261. ),
  262. array(
  263. 'name' => 'off',
  264. 'value' => 'n/a'
  265. )
  266. )
  267. ),
  268. array(
  269. 'type' => 'switch',
  270. 'name' => 'PHPMAILER-verifyCert',
  271. 'label' => 'Verify Certificate',
  272. 'value' => $GLOBALS['PHPMAILER-verifyCert']
  273. ),
  274. ),
  275. 'Sender Information' => array(
  276. array(
  277. 'type' => 'input',
  278. 'name' => 'PHPMAILER-smtpHostSenderName',
  279. 'label' => 'Sender Name',
  280. 'value' => $GLOBALS['PHPMAILER-smtpHostSenderName']
  281. ),
  282. array(
  283. 'type' => 'input',
  284. 'name' => 'PHPMAILER-smtpHostSenderEmail',
  285. 'label' => 'Sender Email',
  286. 'value' => $GLOBALS['PHPMAILER-smtpHostSenderEmail'],
  287. 'placeholder' => 'i.e. same as username'
  288. )
  289. ),
  290. 'Test & Options' => array(
  291. array(
  292. 'type' => 'button',
  293. 'label' => 'Send Test',
  294. 'class' => 'phpmSendTestEmail',
  295. 'icon' => 'fa fa-paper-plane',
  296. 'text' => 'Send'
  297. ),
  298. array(
  299. 'type' => 'input',
  300. 'name' => 'PHPMAILER-domain',
  301. 'label' => 'Domain Link Override',
  302. 'value' => $GLOBALS['PHPMAILER-domain'],
  303. 'placeholder' => 'https://domain.com/',
  304. ),
  305. array(
  306. 'type' => 'select',
  307. 'name' => 'PHPMAILER-template',
  308. 'label' => 'Theme',
  309. 'value' => $GLOBALS['PHPMAILER-template'],
  310. 'options' => getTemplates()
  311. ),
  312. array(
  313. 'type' => 'input',
  314. 'name' => 'PHPMAILER-logo',
  315. 'label' => 'WAN Logo URL',
  316. 'value' => $GLOBALS['PHPMAILER-logo'],
  317. 'placeholder' => 'Full URL',
  318. ),
  319. array(
  320. 'type' => 'switch',
  321. 'name' => 'PHPMAILER-emailTemplateRegisterUserEnabled',
  322. 'label' => 'Send Welcome E-Mail',
  323. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUserEnabled'],
  324. ),
  325. ),
  326. 'Templates' => array(
  327. array(
  328. 'type' => 'accordion',
  329. 'label' => 'Edit Template',
  330. 'id' => 'customEmailTemplates',
  331. 'override' => 12,
  332. 'options' => array(
  333. array(
  334. 'id' => 'PHPMAILER-emailTemplateRegisterUserForm',
  335. 'header' => 'New Registration',
  336. 'body' => array(
  337. array(
  338. 'type' => 'input',
  339. 'name' => 'PHPMAILER-emailTemplateRegisterUserSubject',
  340. 'smallLabel' => 'Subject',
  341. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUserSubject'],
  342. ),
  343. array(
  344. 'type' => 'textbox',
  345. 'name' => 'PHPMAILER-emailTemplateRegisterUser',
  346. 'smallLabel' => 'Body',
  347. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUser'],
  348. 'attr' => 'rows="10"',
  349. )
  350. )
  351. ),
  352. array(
  353. 'id' => 'PHPMAILER-emailTemplateResetPasswordForm',
  354. 'header' => 'Reset Password',
  355. 'body' => array(
  356. array(
  357. 'type' => 'input',
  358. 'name' => 'PHPMAILER-emailTemplateResetPasswordSubject',
  359. 'smallLabel' => 'Subject',
  360. 'value' => $GLOBALS['PHPMAILER-emailTemplateResetPasswordSubject'],
  361. ),
  362. array(
  363. 'type' => 'textbox',
  364. 'name' => 'PHPMAILER-emailTemplateResetPassword',
  365. 'smallLabel' => 'Body',
  366. 'value' => $GLOBALS['PHPMAILER-emailTemplateResetPassword'],
  367. 'attr' => 'rows="10"',
  368. )
  369. )
  370. ),
  371. array(
  372. 'id' => 'PHPMAILER-emailTemplateInviteUserForm',
  373. 'header' => 'Invite User',
  374. 'body' => array(
  375. array(
  376. 'type' => 'input',
  377. 'name' => 'PHPMAILER-emailTemplateInviteUserSubject',
  378. 'smallLabel' => 'Subject',
  379. 'value' => $GLOBALS['PHPMAILER-emailTemplateInviteUserSubject'],
  380. ),
  381. array(
  382. 'type' => 'textbox',
  383. 'name' => 'PHPMAILER-emailTemplateInviteUser',
  384. 'smallLabel' => 'Body',
  385. 'value' => $GLOBALS['PHPMAILER-emailTemplateInviteUser'],
  386. 'attr' => 'rows="10"',
  387. )
  388. )
  389. ),
  390. array(
  391. 'id' => 'PHPMAILER-emailTemplateCustom-include-OneForm',
  392. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneName'],
  393. 'body' => array(
  394. array(
  395. 'type' => 'input',
  396. 'name' => 'PHPMAILER-emailTemplateCustom-include-OneName',
  397. 'smallLabel' => 'Name',
  398. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneName'],
  399. ),
  400. array(
  401. 'type' => 'input',
  402. 'name' => 'PHPMAILER-emailTemplateCustom-include-OneSubject',
  403. 'smallLabel' => 'Subject',
  404. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneSubject'],
  405. ),
  406. array(
  407. 'type' => 'textbox',
  408. 'name' => 'PHPMAILER-emailTemplateCustom-include-One',
  409. 'smallLabel' => 'Body',
  410. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-One'],
  411. 'attr' => 'rows="10"',
  412. )
  413. )
  414. ),
  415. array(
  416. 'id' => 'PHPMAILER-emailTemplateCustom-include-TwoForm',
  417. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoName'],
  418. 'body' => array(
  419. array(
  420. 'type' => 'input',
  421. 'name' => 'PHPMAILER-emailTemplateCustom-include-TwoName',
  422. 'smallLabel' => 'Name',
  423. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoName'],
  424. ),
  425. array(
  426. 'type' => 'input',
  427. 'name' => 'PHPMAILER-emailTemplateCustom-include-TwoSubject',
  428. 'smallLabel' => 'Subject',
  429. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoSubject'],
  430. ),
  431. array(
  432. 'type' => 'textbox',
  433. 'name' => 'PHPMAILER-emailTemplateCustom-include-Two',
  434. 'smallLabel' => 'Body',
  435. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Two'],
  436. 'attr' => 'rows="10"',
  437. )
  438. )
  439. ),
  440. array(
  441. 'id' => 'PHPMAILER-emailTemplateCustom-include-ThreeForm',
  442. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeName'],
  443. 'body' => array(
  444. array(
  445. 'type' => 'input',
  446. 'name' => 'PHPMAILER-emailTemplateCustom-include-ThreeName',
  447. 'smallLabel' => 'Name',
  448. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeName'],
  449. ),
  450. array(
  451. 'type' => 'input',
  452. 'name' => 'PHPMAILER-emailTemplateCustom-include-ThreeSubject',
  453. 'smallLabel' => 'Subject',
  454. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeSubject'],
  455. ),
  456. array(
  457. 'type' => 'textbox',
  458. 'name' => 'PHPMAILER-emailTemplateCustom-include-Three',
  459. 'smallLabel' => 'Body',
  460. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Three'],
  461. 'attr' => 'rows="10"',
  462. )
  463. )
  464. ),
  465. array(
  466. 'id' => 'PHPMAILER-emailTemplateCustom-include-FourForm',
  467. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourName'],
  468. 'body' => array(
  469. array(
  470. 'type' => 'input',
  471. 'name' => 'PHPMAILER-emailTemplateCustom-include-FourName',
  472. 'smallLabel' => 'Name',
  473. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourName'],
  474. ),
  475. array(
  476. 'type' => 'input',
  477. 'name' => 'PHPMAILER-emailTemplateCustom-include-FourSubject',
  478. 'smallLabel' => 'Subject',
  479. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourSubject'],
  480. ),
  481. array(
  482. 'type' => 'textbox',
  483. 'name' => 'PHPMAILER-emailTemplateCustom-include-Four',
  484. 'smallLabel' => 'Body',
  485. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Four'],
  486. 'attr' => 'rows="10"',
  487. )
  488. )
  489. ),
  490. )
  491. )
  492. )
  493. );
  494. }