php-mailer.php 20 KB

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