php-mailer.php 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  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. ];
  54. $emailTemplate['body'] = strtr($emailTemplate['body'], $variables);
  55. $emailTemplate['subject'] = strtr($emailTemplate['subject'], $variables);
  56. return $emailTemplate;
  57. }
  58. function phpmBuildEmail($email){
  59. $subject = (isset($email['subject'])) ? $email['subject'] : 'Message from Server';
  60. $body = (isset($email['body'])) ? $email['body'] : 'Message Error Occured';
  61. $type = (isset($email['type'])) ? $email['type'] : 'No Type';
  62. switch ($type) {
  63. case 'invite':
  64. $extra = 'invite';
  65. break;
  66. case 'reset':
  67. $extra = 'reset';
  68. break;
  69. default:
  70. $extra = null;
  71. break;
  72. }
  73. include('misc/emailTemplates/'.$GLOBALS['PHPMAILER-template'].'.php');
  74. return $email;
  75. }
  76. function phpmAdminSendEmail(){
  77. if($GLOBALS['PHPMAILER-enabled']){
  78. $emailTemplate = array(
  79. 'type' => 'admin',
  80. 'body' => $_POST['data']['body'],
  81. 'subject' => $_POST['data']['subject'],
  82. 'user' => null,
  83. 'password' => null,
  84. 'inviteCode' => null,
  85. );
  86. $emailTemplate = phpmEmailTemplate($emailTemplate);
  87. $sendEmail = array(
  88. 'bcc' => $_POST['data']['bcc'],
  89. 'subject' => $emailTemplate['subject'],
  90. 'body' => phpmBuildEmail($emailTemplate),
  91. );
  92. return phpmSendEmail($sendEmail);
  93. }
  94. return false;
  95. }
  96. function phpmSendTestEmail(){
  97. try {
  98. $mail = new PHPMailer\PHPMailer\PHPMailer(true);
  99. $mail->isSMTP();
  100. //$mail->SMTPDebug = 3;
  101. $mail->Host = $GLOBALS['PHPMAILER-smtpHost'];
  102. $mail->Port = $GLOBALS['PHPMAILER-smtpHostPort'];
  103. $mail->SMTPSecure = $GLOBALS['PHPMAILER-smtpHostType'];
  104. $mail->SMTPAuth = $GLOBALS['PHPMAILER-smtpHostAuth'];
  105. $mail->Username = $GLOBALS['PHPMAILER-smtpHostUsername'];
  106. $mail->Password = decrypt($GLOBALS['PHPMAILER-smtpHostPassword']);
  107. $mail->setFrom($GLOBALS['PHPMAILER-smtpHostSenderEmail'], $GLOBALS['PHPMAILER-smtpHostSenderName']);
  108. $mail->addReplyTo($GLOBALS['PHPMAILER-smtpHostSenderEmail'], $GLOBALS['PHPMAILER-smtpHostSenderName']);
  109. $mail->isHTML(true);
  110. $mail->addAddress($GLOBALS['organizrUser']['email'], $GLOBALS['organizrUser']['username']);
  111. $mail->Subject = "Organizr Test E-Mail";
  112. $mail->Body = "This was just a test!";
  113. $mail->send();
  114. writeLog('success', 'Mail Function - E-Mail Test Sent', $GLOBALS['organizrUser']['username']);
  115. return true;
  116. } catch (PHPMailer\PHPMailer\Exception $e) {
  117. writeLog('error', 'Mail Function - E-Mail Test Failed['.$mail->ErrorInfo.']', $GLOBALS['organizrUser']['username']);
  118. return $e->errorMessage();
  119. }
  120. return false;
  121. }
  122. function phpmSendEmail($emailInfo){
  123. $to = isset($emailInfo['to']) ? $emailInfo['to'] : null;
  124. $cc = isset($emailInfo['cc']) ? $emailInfo['cc'] : null;
  125. $bcc = isset($emailInfo['bcc']) ? $emailInfo['bcc'] : null;
  126. $subject = isset($emailInfo['subject']) ? $emailInfo['subject'] : null;
  127. $body = isset($emailInfo['body']) ? $emailInfo['body'] : null;
  128. $username = isset($emailInfo['user']) ? $emailInfo['user'] : 'Organizr User';
  129. try {
  130. $mail = new PHPMailer\PHPMailer\PHPMailer(true);
  131. $mail->isSMTP();
  132. //$mail->SMTPDebug = 3;
  133. $mail->Host = $GLOBALS['PHPMAILER-smtpHost'];
  134. $mail->Port = $GLOBALS['PHPMAILER-smtpHostPort'];
  135. $mail->SMTPSecure = $GLOBALS['PHPMAILER-smtpHostType'];
  136. $mail->SMTPAuth = $GLOBALS['PHPMAILER-smtpHostAuth'];
  137. $mail->Username = $GLOBALS['PHPMAILER-smtpHostUsername'];
  138. $mail->Password = decrypt($GLOBALS['PHPMAILER-smtpHostPassword']);
  139. $mail->setFrom($GLOBALS['PHPMAILER-smtpHostSenderEmail'], $GLOBALS['PHPMAILER-smtpHostSenderName']);
  140. $mail->addReplyTo($GLOBALS['PHPMAILER-smtpHostSenderEmail'], $GLOBALS['PHPMAILER-smtpHostSenderName']);
  141. $mail->isHTML(true);
  142. if($to){ $mail->addAddress($to, $username); }
  143. if($cc){ $mail->addCC($cc); }
  144. if($bcc){
  145. if(strpos($bcc , ',') === false){
  146. $mail->addBCC($bcc);
  147. }else{
  148. $allEmails = explode(",",$bcc);
  149. foreach($allEmails as $gotEmail){
  150. $mail->addBCC($gotEmail);
  151. }
  152. }
  153. }
  154. $mail->Subject = $subject;
  155. $mail->Body = $body;
  156. $mail->send();
  157. //writeLog('success', 'Mail Function - E-Mail Test Sent', $GLOBALS['organizrUser']['username']);
  158. return true;
  159. } catch (PHPMailer\PHPMailer\Exception $e) {
  160. writeLog('error', 'Mail Function - E-Mail Test Failed['.$mail->ErrorInfo.']', $GLOBALS['organizrUser']['username']);
  161. return $e->errorMessage();
  162. }
  163. return false;
  164. }
  165. /* GET PHPMAILER SETTINGS */
  166. function phpmGetSettings(){
  167. return array(
  168. 'Host' => array(
  169. array(
  170. 'type' => 'input',
  171. 'name' => 'PHPMAILER-smtpHost',
  172. 'label' => 'SMTP Host',
  173. 'value' => $GLOBALS['PHPMAILER-smtpHost']
  174. ),
  175. array(
  176. 'type' => 'input',
  177. 'name' => 'PHPMAILER-smtpHostPort',
  178. 'label' => 'SMTP Port',
  179. 'value' => $GLOBALS['PHPMAILER-smtpHostPort']
  180. )
  181. ),
  182. 'Authentication' => array(
  183. array(
  184. 'type' => 'input',
  185. 'name' => 'PHPMAILER-smtpHostUsername',
  186. 'label' => 'Username',
  187. 'value' => $GLOBALS['PHPMAILER-smtpHostUsername']
  188. ),
  189. array(
  190. 'type' => 'password',
  191. 'name' => 'PHPMAILER-smtpHostPassword',
  192. 'label' => 'Password',
  193. 'value' => $GLOBALS['PHPMAILER-smtpHostPassword']
  194. ),
  195. array(
  196. 'type' => 'switch',
  197. 'name' => 'PHPMAILER-smtpHostAuth',
  198. 'label' => 'Authentication',
  199. 'value' => $GLOBALS['PHPMAILER-smtpHostAuth']
  200. ),
  201. array(
  202. 'type' => 'select',
  203. 'name' => 'PHPMAILER-smtpHostType',
  204. 'label' => 'Authentication Type',
  205. 'value' => $GLOBALS['PHPMAILER-smtpHostType'],
  206. 'options' => array(
  207. array(
  208. 'name'=>'tls',
  209. 'value'=>'tls'
  210. ),
  211. array(
  212. 'name'=>'ssl',
  213. 'value'=>'ssl'
  214. ),
  215. array(
  216. 'name'=>'off',
  217. 'value'=>'false'
  218. )
  219. )
  220. )
  221. ),
  222. 'Sender Information' => array(
  223. array(
  224. 'type' => 'input',
  225. 'name' => 'PHPMAILER-smtpHostSenderName',
  226. 'label' => 'Sender Name',
  227. 'value' => $GLOBALS['PHPMAILER-smtpHostSenderName']
  228. ),
  229. array(
  230. 'type' => 'input',
  231. 'name' => 'PHPMAILER-smtpHostSenderEmail',
  232. 'label' => 'Sender Email',
  233. 'value' => $GLOBALS['PHPMAILER-smtpHostSenderEmail'],
  234. 'placeholder' => 'i.e. same as username'
  235. )
  236. ),
  237. 'Test & Options' => array(
  238. array(
  239. 'type' => 'button',
  240. 'label' => 'Send Test',
  241. 'class' => 'phpmSendTestEmail',
  242. 'icon' => 'fa fa-paper-plane',
  243. 'text' => 'Send'
  244. ),
  245. array(
  246. 'type' => 'input',
  247. 'name' => 'PHPMAILER-domain',
  248. 'label' => 'Domain Link Override',
  249. 'value' => $GLOBALS['PHPMAILER-domain'],
  250. 'placeholder' => 'https://domain.com/',
  251. ),
  252. array(
  253. 'type' => 'select',
  254. 'name' => 'theme',
  255. 'label' => 'Theme',
  256. 'class' => 'themeChanger',
  257. 'value' => $GLOBALS['PHPMAILER-template'],
  258. 'options' => getTemplates()
  259. ),
  260. array(
  261. 'type' => 'input',
  262. 'name' => 'PHPMAILER-logo',
  263. 'label' => 'WAN Logo URL',
  264. 'value' => $GLOBALS['PHPMAILER-logo'],
  265. 'placeholder' => 'Full URL',
  266. ),
  267. ),
  268. 'Templates' => array(
  269. array(
  270. 'type' => 'accordion',
  271. 'label' => 'Edit Template',
  272. 'id' => 'customEmailTemplates',
  273. 'override' => 12,
  274. 'options' => array(
  275. array(
  276. 'id' => 'PHPMAILER-emailTemplateRegisterUserForm',
  277. 'header' => 'New Registration',
  278. 'body' => array(
  279. array(
  280. 'type' => 'input',
  281. 'name' => 'PHPMAILER-emailTemplateRegisterUserSubject',
  282. 'smallLabel' => 'Subject',
  283. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUserSubject'],
  284. ),
  285. array(
  286. 'type' => 'textbox',
  287. 'name' => 'PHPMAILER-emailTemplateRegisterUser',
  288. 'smallLabel' => 'Body',
  289. 'value' => $GLOBALS['PHPMAILER-emailTemplateRegisterUser'],
  290. 'attr' => 'rows="10"',
  291. )
  292. )
  293. ),
  294. array(
  295. 'id' => 'PHPMAILER-emailTemplateResetPasswordForm',
  296. 'header' => 'Reset Password',
  297. 'body' => array(
  298. array(
  299. 'type' => 'input',
  300. 'name' => 'PHPMAILER-emailTemplateResetPasswordSubject',
  301. 'smallLabel' => 'Subject',
  302. 'value' => $GLOBALS['PHPMAILER-emailTemplateResetPasswordSubject'],
  303. ),
  304. array(
  305. 'type' => 'textbox',
  306. 'name' => 'PHPMAILER-emailTemplateResetPassword',
  307. 'smallLabel' => 'Body',
  308. 'value' => $GLOBALS['PHPMAILER-emailTemplateResetPassword'],
  309. 'attr' => 'rows="10"',
  310. )
  311. )
  312. ),
  313. array(
  314. 'id' => 'PHPMAILER-emailTemplateInviteUserForm',
  315. 'header' => 'Invite User',
  316. 'body' => array(
  317. array(
  318. 'type' => 'input',
  319. 'name' => 'PHPMAILER-emailTemplateInviteUserSubject',
  320. 'smallLabel' => 'Subject',
  321. 'value' => $GLOBALS['PHPMAILER-emailTemplateInviteUserSubject'],
  322. ),
  323. array(
  324. 'type' => 'textbox',
  325. 'name' => 'PHPMAILER-emailTemplateInviteUser',
  326. 'smallLabel' => 'Body',
  327. 'value' => $GLOBALS['PHPMAILER-emailTemplateInviteUser'],
  328. 'attr' => 'rows="10"',
  329. )
  330. )
  331. ),
  332. )
  333. )
  334. )
  335. );
  336. }