php-mailer.php 14 KB

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