php-mailer.php 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495
  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' => true,
  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' => true,
  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. ),
  269. 'Sender Information' => array(
  270. array(
  271. 'type' => 'input',
  272. 'name' => 'PHPMAILER-smtpHostSenderName',
  273. 'label' => 'Sender Name',
  274. 'value' => $GLOBALS['PHPMAILER-smtpHostSenderName']
  275. ),
  276. array(
  277. 'type' => 'input',
  278. 'name' => 'PHPMAILER-smtpHostSenderEmail',
  279. 'label' => 'Sender Email',
  280. 'value' => $GLOBALS['PHPMAILER-smtpHostSenderEmail'],
  281. 'placeholder' => 'i.e. same as username'
  282. )
  283. ),
  284. 'Test & Options' => array(
  285. array(
  286. 'type' => 'button',
  287. 'label' => 'Send Test',
  288. 'class' => 'phpmSendTestEmail',
  289. 'icon' => 'fa fa-paper-plane',
  290. 'text' => 'Send'
  291. ),
  292. array(
  293. 'type' => 'input',
  294. 'name' => 'PHPMAILER-domain',
  295. 'label' => 'Domain Link Override',
  296. 'value' => $GLOBALS['PHPMAILER-domain'],
  297. 'placeholder' => 'https://domain.com/',
  298. ),
  299. array(
  300. 'type' => 'select',
  301. 'name' => 'PHPMAILER-template',
  302. 'label' => 'Theme',
  303. 'value' => $GLOBALS['PHPMAILER-template'],
  304. 'options' => getTemplates()
  305. ),
  306. array(
  307. 'type' => 'input',
  308. 'name' => 'PHPMAILER-logo',
  309. 'label' => 'WAN Logo URL',
  310. 'value' => $GLOBALS['PHPMAILER-logo'],
  311. 'placeholder' => 'Full URL',
  312. ),
  313. array(
  314. 'type' => 'switch',
  315. 'name' => 'PHPMAILER-emailTemplateRegisterUserEnabled',
  316. 'label' => 'Send Welcome E-Mail',
  317. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUserEnabled'],
  318. ),
  319. ),
  320. 'Templates' => array(
  321. array(
  322. 'type' => 'accordion',
  323. 'label' => 'Edit Template',
  324. 'id' => 'customEmailTemplates',
  325. 'override' => 12,
  326. 'options' => array(
  327. array(
  328. 'id' => 'PHPMAILER-emailTemplateRegisterUserForm',
  329. 'header' => 'New Registration',
  330. 'body' => array(
  331. array(
  332. 'type' => 'input',
  333. 'name' => 'PHPMAILER-emailTemplateRegisterUserSubject',
  334. 'smallLabel' => 'Subject',
  335. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUserSubject'],
  336. ),
  337. array(
  338. 'type' => 'textbox',
  339. 'name' => 'PHPMAILER-emailTemplateRegisterUser',
  340. 'smallLabel' => 'Body',
  341. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUser'],
  342. 'attr' => 'rows="10"',
  343. )
  344. )
  345. ),
  346. array(
  347. 'id' => 'PHPMAILER-emailTemplateResetPasswordForm',
  348. 'header' => 'Reset Password',
  349. 'body' => array(
  350. array(
  351. 'type' => 'input',
  352. 'name' => 'PHPMAILER-emailTemplateResetPasswordSubject',
  353. 'smallLabel' => 'Subject',
  354. 'value' => $GLOBALS['PHPMAILER-emailTemplateResetPasswordSubject'],
  355. ),
  356. array(
  357. 'type' => 'textbox',
  358. 'name' => 'PHPMAILER-emailTemplateResetPassword',
  359. 'smallLabel' => 'Body',
  360. 'value' => $GLOBALS['PHPMAILER-emailTemplateResetPassword'],
  361. 'attr' => 'rows="10"',
  362. )
  363. )
  364. ),
  365. array(
  366. 'id' => 'PHPMAILER-emailTemplateInviteUserForm',
  367. 'header' => 'Invite User',
  368. 'body' => array(
  369. array(
  370. 'type' => 'input',
  371. 'name' => 'PHPMAILER-emailTemplateInviteUserSubject',
  372. 'smallLabel' => 'Subject',
  373. 'value' => $GLOBALS['PHPMAILER-emailTemplateInviteUserSubject'],
  374. ),
  375. array(
  376. 'type' => 'textbox',
  377. 'name' => 'PHPMAILER-emailTemplateInviteUser',
  378. 'smallLabel' => 'Body',
  379. 'value' => $GLOBALS['PHPMAILER-emailTemplateInviteUser'],
  380. 'attr' => 'rows="10"',
  381. )
  382. )
  383. ),
  384. array(
  385. 'id' => 'PHPMAILER-emailTemplateCustom-include-OneForm',
  386. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneName'],
  387. 'body' => array(
  388. array(
  389. 'type' => 'input',
  390. 'name' => 'PHPMAILER-emailTemplateCustom-include-OneName',
  391. 'smallLabel' => 'Name',
  392. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneName'],
  393. ),
  394. array(
  395. 'type' => 'input',
  396. 'name' => 'PHPMAILER-emailTemplateCustom-include-OneSubject',
  397. 'smallLabel' => 'Subject',
  398. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneSubject'],
  399. ),
  400. array(
  401. 'type' => 'textbox',
  402. 'name' => 'PHPMAILER-emailTemplateCustom-include-One',
  403. 'smallLabel' => 'Body',
  404. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-One'],
  405. 'attr' => 'rows="10"',
  406. )
  407. )
  408. ),
  409. array(
  410. 'id' => 'PHPMAILER-emailTemplateCustom-include-TwoForm',
  411. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoName'],
  412. 'body' => array(
  413. array(
  414. 'type' => 'input',
  415. 'name' => 'PHPMAILER-emailTemplateCustom-include-TwoName',
  416. 'smallLabel' => 'Name',
  417. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoName'],
  418. ),
  419. array(
  420. 'type' => 'input',
  421. 'name' => 'PHPMAILER-emailTemplateCustom-include-TwoSubject',
  422. 'smallLabel' => 'Subject',
  423. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoSubject'],
  424. ),
  425. array(
  426. 'type' => 'textbox',
  427. 'name' => 'PHPMAILER-emailTemplateCustom-include-Two',
  428. 'smallLabel' => 'Body',
  429. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Two'],
  430. 'attr' => 'rows="10"',
  431. )
  432. )
  433. ),
  434. array(
  435. 'id' => 'PHPMAILER-emailTemplateCustom-include-ThreeForm',
  436. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeName'],
  437. 'body' => array(
  438. array(
  439. 'type' => 'input',
  440. 'name' => 'PHPMAILER-emailTemplateCustom-include-ThreeName',
  441. 'smallLabel' => 'Name',
  442. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeName'],
  443. ),
  444. array(
  445. 'type' => 'input',
  446. 'name' => 'PHPMAILER-emailTemplateCustom-include-ThreeSubject',
  447. 'smallLabel' => 'Subject',
  448. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeSubject'],
  449. ),
  450. array(
  451. 'type' => 'textbox',
  452. 'name' => 'PHPMAILER-emailTemplateCustom-include-Three',
  453. 'smallLabel' => 'Body',
  454. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Three'],
  455. 'attr' => 'rows="10"',
  456. )
  457. )
  458. ),
  459. array(
  460. 'id' => 'PHPMAILER-emailTemplateCustom-include-FourForm',
  461. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourName'],
  462. 'body' => array(
  463. array(
  464. 'type' => 'input',
  465. 'name' => 'PHPMAILER-emailTemplateCustom-include-FourName',
  466. 'smallLabel' => 'Name',
  467. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourName'],
  468. ),
  469. array(
  470. 'type' => 'input',
  471. 'name' => 'PHPMAILER-emailTemplateCustom-include-FourSubject',
  472. 'smallLabel' => 'Subject',
  473. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourSubject'],
  474. ),
  475. array(
  476. 'type' => 'textbox',
  477. 'name' => 'PHPMAILER-emailTemplateCustom-include-Four',
  478. 'smallLabel' => 'Body',
  479. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Four'],
  480. 'attr' => 'rows="10"',
  481. )
  482. )
  483. ),
  484. )
  485. )
  486. )
  487. );
  488. }