php-mailer.php 15 KB

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