php-mailer.php 9.6 KB

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