php-mailer.php 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446
  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. array(
  269. 'type' => 'switch',
  270. 'name' => 'PHPMAILER-emailTemplateRegisterUserEnabled',
  271. 'label' => 'Send Welcome E-Mail',
  272. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUserEnabled'],
  273. ),
  274. ),
  275. 'Templates' => array(
  276. array(
  277. 'type' => 'accordion',
  278. 'label' => 'Edit Template',
  279. 'id' => 'customEmailTemplates',
  280. 'override' => 12,
  281. 'options' => array(
  282. array(
  283. 'id' => 'PHPMAILER-emailTemplateRegisterUserForm',
  284. 'header' => 'New Registration',
  285. 'body' => array(
  286. array(
  287. 'type' => 'input',
  288. 'name' => 'PHPMAILER-emailTemplateRegisterUserSubject',
  289. 'smallLabel' => 'Subject',
  290. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUserSubject'],
  291. ),
  292. array(
  293. 'type' => 'textbox',
  294. 'name' => 'PHPMAILER-emailTemplateRegisterUser',
  295. 'smallLabel' => 'Body',
  296. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUser'],
  297. 'attr' => 'rows="10"',
  298. )
  299. )
  300. ),
  301. array(
  302. 'id' => 'PHPMAILER-emailTemplateResetPasswordForm',
  303. 'header' => 'Reset Password',
  304. 'body' => array(
  305. array(
  306. 'type' => 'input',
  307. 'name' => 'PHPMAILER-emailTemplateResetPasswordSubject',
  308. 'smallLabel' => 'Subject',
  309. 'value' => $GLOBALS['PHPMAILER-emailTemplateResetPasswordSubject'],
  310. ),
  311. array(
  312. 'type' => 'textbox',
  313. 'name' => 'PHPMAILER-emailTemplateResetPassword',
  314. 'smallLabel' => 'Body',
  315. 'value' => $GLOBALS['PHPMAILER-emailTemplateResetPassword'],
  316. 'attr' => 'rows="10"',
  317. )
  318. )
  319. ),
  320. array(
  321. 'id' => 'PHPMAILER-emailTemplateInviteUserForm',
  322. 'header' => 'Invite User',
  323. 'body' => array(
  324. array(
  325. 'type' => 'input',
  326. 'name' => 'PHPMAILER-emailTemplateInviteUserSubject',
  327. 'smallLabel' => 'Subject',
  328. 'value' => $GLOBALS['PHPMAILER-emailTemplateInviteUserSubject'],
  329. ),
  330. array(
  331. 'type' => 'textbox',
  332. 'name' => 'PHPMAILER-emailTemplateInviteUser',
  333. 'smallLabel' => 'Body',
  334. 'value' => $GLOBALS['PHPMAILER-emailTemplateInviteUser'],
  335. 'attr' => 'rows="10"',
  336. )
  337. )
  338. ),
  339. array(
  340. 'id' => 'PHPMAILER-emailTemplateCustom-include-OneForm',
  341. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneName'],
  342. 'body' => array(
  343. array(
  344. 'type' => 'input',
  345. 'name' => 'PHPMAILER-emailTemplateCustom-include-OneName',
  346. 'smallLabel' => 'Name',
  347. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneName'],
  348. ),
  349. array(
  350. 'type' => 'input',
  351. 'name' => 'PHPMAILER-emailTemplateCustom-include-OneSubject',
  352. 'smallLabel' => 'Subject',
  353. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-OneSubject'],
  354. ),
  355. array(
  356. 'type' => 'textbox',
  357. 'name' => 'PHPMAILER-emailTemplateCustom-include-One',
  358. 'smallLabel' => 'Body',
  359. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-One'],
  360. 'attr' => 'rows="10"',
  361. )
  362. )
  363. ),
  364. array(
  365. 'id' => 'PHPMAILER-emailTemplateCustom-include-TwoForm',
  366. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoName'],
  367. 'body' => array(
  368. array(
  369. 'type' => 'input',
  370. 'name' => 'PHPMAILER-emailTemplateCustom-include-TwoName',
  371. 'smallLabel' => 'Name',
  372. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoName'],
  373. ),
  374. array(
  375. 'type' => 'input',
  376. 'name' => 'PHPMAILER-emailTemplateCustom-include-TwoSubject',
  377. 'smallLabel' => 'Subject',
  378. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-TwoSubject'],
  379. ),
  380. array(
  381. 'type' => 'textbox',
  382. 'name' => 'PHPMAILER-emailTemplateCustom-include-Two',
  383. 'smallLabel' => 'Body',
  384. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Two'],
  385. 'attr' => 'rows="10"',
  386. )
  387. )
  388. ),
  389. array(
  390. 'id' => 'PHPMAILER-emailTemplateCustom-include-ThreeForm',
  391. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeName'],
  392. 'body' => array(
  393. array(
  394. 'type' => 'input',
  395. 'name' => 'PHPMAILER-emailTemplateCustom-include-ThreeName',
  396. 'smallLabel' => 'Name',
  397. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeName'],
  398. ),
  399. array(
  400. 'type' => 'input',
  401. 'name' => 'PHPMAILER-emailTemplateCustom-include-ThreeSubject',
  402. 'smallLabel' => 'Subject',
  403. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-ThreeSubject'],
  404. ),
  405. array(
  406. 'type' => 'textbox',
  407. 'name' => 'PHPMAILER-emailTemplateCustom-include-Three',
  408. 'smallLabel' => 'Body',
  409. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Three'],
  410. 'attr' => 'rows="10"',
  411. )
  412. )
  413. ),
  414. array(
  415. 'id' => 'PHPMAILER-emailTemplateCustom-include-FourForm',
  416. 'header' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourName'],
  417. 'body' => array(
  418. array(
  419. 'type' => 'input',
  420. 'name' => 'PHPMAILER-emailTemplateCustom-include-FourName',
  421. 'smallLabel' => 'Name',
  422. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourName'],
  423. ),
  424. array(
  425. 'type' => 'input',
  426. 'name' => 'PHPMAILER-emailTemplateCustom-include-FourSubject',
  427. 'smallLabel' => 'Subject',
  428. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-FourSubject'],
  429. ),
  430. array(
  431. 'type' => 'textbox',
  432. 'name' => 'PHPMAILER-emailTemplateCustom-include-Four',
  433. 'smallLabel' => 'Body',
  434. 'value' => $GLOBALS['PHPMAILER-emailTemplateCustom-include-Four'],
  435. 'attr' => 'rows="10"',
  436. )
  437. )
  438. ),
  439. )
  440. )
  441. )
  442. );
  443. }