activationemail.plugin.php 8.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <?php
  2. /**
  3. * ActivationEmail
  4. *
  5. * Copyright 2011-2019 Bob Ray
  6. *
  7. * @author Bob Ray
  8. * 1/30/11
  9. *
  10. * ActivationEmail is free software; you can redistribute it and/or modify it
  11. * under the terms of the GNU General Public License as published by the Free
  12. * Software Foundation; either version 2 of the License, or (at your option) any
  13. * later version.
  14. *
  15. * ActivationEmail is distributed in the hope that it will be useful, but WITHOUT ANY
  16. * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
  17. * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU General Public License along with
  20. * ActivationEmail; if not, write to the Free Software Foundation, Inc., 59 Temple
  21. * Place, Suite 330, Boston, MA 02111-1307 USA
  22. *
  23. * @package activationemail
  24. */
  25. /**
  26. * MODX ActivationEmail Snippet
  27. *
  28. * Description Sends an email to users on manual activation and
  29. * (optionally) deactivation.
  30. *
  31. * @package activationemail
  32. *
  33. *
  34. * Properties:
  35. *
  36. * @property sendOnActivation (boolean) - Set to '1' to send the activation email; default '1'
  37. * @property sendOnDeactivation (boolean) - Set to '1' to send the deactivation email; default '0'
  38. * @property activationEmailTpl (string) - Tpl chunk for activation email;
  39. * default: ActivationEmailTpl
  40. * @property deactivationEmailTpl (string) - Tpl chunk for deactivation email;
  41. * default: DeactivationEmailTpl
  42. * @property activationURL (string) url to send activated users to;
  43. * defaults to site_url System Setting
  44. * @property deactivationURL (string) url to send deactivated users to;
  45. * defaults to site_url System Setting
  46. * @property sitename (string) site name to use in message; defaults
  47. * to site_name System Setting
  48. * @property useFullname (boolean) - Use full name in msg instead of username.
  49. * @property replyToAddress (string) - reply-to address for emails;
  50. * defaults to emailsender system setting.
  51. * @property activeSubject (string) - subject for activation emails;
  52. * defaults to "Registration Approved".
  53. * @property activeSender(string) - Email Sender for activation emails;
  54. * defaults to emailsender system setting.
  55. * @property activeFrom (string) - Email From for activation emails;
  56. * defaults to emailsender system setting.
  57. * @property activeFromName(string) - Email From Name for activation emails;
  58. * defaults to site_name system setting.
  59. * @property activeReplyTo(string) - Email Reply To for activation emails;
  60. * defaults to emailsender system setting.
  61. *
  62. * @property deActiveSubject (string) - subject for deactivation emails;
  63. * defaults to "Status Changed to Inactive".
  64. * @property deActiveSender(string) - Email Sender for deactivation emails;
  65. * defaults to emailsender system setting.
  66. * @property deActiveFrom (string) - Email From for deactivation emails;
  67. * defaults to emailsender system setting.
  68. * @property deActiveFromName(string) - Email From Name for deactivation emails;
  69. * defaults to site_name system setting.
  70. * @property deActiveReplyTo(string) - Email Reply To for deactivation emails;
  71. * defaults to emailsender system setting.
  72. *
  73. * Placeholders:
  74. * [[+username]]
  75. * [[+sitename]]
  76. * [[+activationURL]]
  77. * [[+deactivationURL]]
  78. * [[+fromName]]
  79. *
  80. */
  81. /* Connect to OnUserBeforeSave (Note: *not* OnBeforeUserFormSave) */
  82. /** @var $scriptProperties array */
  83. /** @var $modx modX $sp */
  84. /** @var $modx->$xpdo xPDO */
  85. $sp = $scriptProperties;
  86. if ($mode != modSystemEvent::MODE_UPD || ! is_object($modx) || !empty($modx->xpdo->config[xPDO::OPT_SETUP])) {
  87. return '';
  88. }
  89. $sendActivation = $modx->getOption('sendOnActivation',$sp,true) ? true : false;
  90. $sendDeactivation = $modx->getOption('sendOnDeactivation',$sp,false) ? true : false;
  91. $activationEmailTpl = $modx->getOption('activationEmailTpl',$sp,'ActivationEmailTpl');
  92. $deactivationEmailTpl = $modx->getOption('deactivationEmailTpl',$sp,'DeactivationEmailTpl');
  93. /* get the system setting */
  94. $emailSender = $modx->getOption('emailsender');
  95. /* This won't be used if activeReplyTo or deActiveReplyTo are set */
  96. $replyTo = $modx->getOption('replyToAddress',$sp, null);
  97. $replyTo = empty($replyTo) ? $emailSender : $replyTo;
  98. /* This won't be used if activeFromName or deActiveFromName are set */
  99. $fromName = $modx->getOption('fromName', $sp, null);
  100. $fromName = empty($fromName) ? $modx->getOption('site_name'): $fromName;
  101. /* If you hard-code these in the email templates, the settings of
  102. * these properties be ignored. Otherwise, the system settings will
  103. * be used unless the properties are set.
  104. */
  105. $siteName = $modx->getOption('sitename',$sp);
  106. $siteName = empty($siteName)? $modx->getOption('site_name') : $siteName;
  107. $activeURL = $modx->getOption('activationURL', $sp,null);
  108. $activeURL = empty($activeURL)? $modx->getOption('site_url') : $activeURL ;
  109. $deActiveURL = $modx->getOption('deactivationURL', $sp,null);
  110. $deActiveURL = empty($deActiveURL)? $modx->getOption('site_url') : $deActiveURL ;
  111. $profile = $user->getOne('Profile');
  112. $email = $profile->get('email');
  113. $useFullName = $modx->getOption('useFullName', $sp, false);
  114. $name = $useFullname ? $profile->get('fullname') : $user->get('username');
  115. $id = $user->get('id');
  116. $dbUser = $modx->getObject('modUser',$id);
  117. $before = $dbUser->get('active');
  118. $after = $user->get('active');
  119. $fullName = $profile->get('fullname');
  120. $fields = array(
  121. 'fullname' => $fullName,
  122. 'username' => $name,
  123. 'sitename' => $siteName,
  124. 'activationURL' => $activeURL,
  125. 'deactivationURL' => $deActiveURL,
  126. );
  127. $send = false;
  128. if ($sendActivation && (empty($before) && $after)) {
  129. /* activation */
  130. $_sender = $modx->getOption('activeSender', $sp, null);
  131. $_sender = empty($_sender)? $emailSender : $_sender;
  132. $_reply = $modx->getOption('activeReplyTo', $sp, null);
  133. $_reply = empty($_reply)? $replyTo : $_reply;
  134. $_from = $modx->getOption('activeFrom', $sp, null);
  135. $_from = empty($_from)? $emailSender : $_from;
  136. $_fromName = $modx->getOption('activeFromName', $sp, null);
  137. $_fromName = empty($_fromName)? $fromName : $_fromName;
  138. $fields['fromName'] = $_fromName;
  139. $subject = $modx->getOption('activeSubject', $sp,null);
  140. $_subject = empty($subject)? 'Registration Approved' : $subject ;
  141. $_msg = $modx->getChunk($activationEmailTpl,$fields);
  142. $send = true;
  143. $eventName = 'activate_user';
  144. if (empty($_msg)) {
  145. $_msg = "<p>Dear " . $name . ",</p>
  146. <p>Thank you for registering at " . $siteName . '.</p>
  147. <p>Your registration has been approved and you may now access the Members area, please login <a href="' . $activeURL . '">here</a>.</p>';
  148. $_msg .= "<p>Kind Regards, <br />Site Administrator</p>";
  149. }
  150. }
  151. if ($sendDeactivation && (empty($after) && $before)) {
  152. /* deactivation */
  153. $_sender = $modx->getOption('deActiveSender', $sp, null);
  154. $_sender = empty($_sender)? $emailSender : $_sender;
  155. $_reply = $modx->getOption('deActiveReplyTo', $sp, null);
  156. $_reply = empty($_reply)? $replyTo : $_reply;
  157. $from = $modx->getOption('deActiveFrom', $sp, null);
  158. $from = empty($from)? $emailSender : $from;
  159. $_fromName = $modx->getOption('deActiveFromName', $sp, null);
  160. $_fromName = empty($_fromName)? $fromName : $_fromName;
  161. $fields['fromName'] = $_fromName;
  162. $subject = $modx->getOption('deActiveSubject', $sp,null);
  163. $_subject = empty($subject)? 'Status Changed to Inactive' : $subject ;
  164. $_msg = $modx->getChunk($deactivationEmailTpl,$fields);
  165. $send = true;
  166. $eventName = 'deactivate_user';
  167. if (empty($_msg)) {
  168. $_msg = "<p>Dear " . $name . ',</p>
  169. <p>Your status at ' . $siteName . ' has been changed to "inactive." '. 'If you believe this is an error please contact the site administrator at <a href="' . $deActiveURL .'">' . $deActiveURL . '</a>.</p>';
  170. $_msg .= "<p>Kind Regards, <br />Site Administrator</p>";
  171. }
  172. }
  173. if ($send ) {
  174. $modx->logManagerAction($eventName,'modUser',$user->get('id'));
  175. $modx->getService('mail', 'mail.modPHPMailer');
  176. $modx->mail->set(modMail::MAIL_BODY, $_msg);
  177. $modx->mail->set(modMail::MAIL_FROM, $_from);
  178. $modx->mail->set(modMail::MAIL_FROM_NAME, $_fromName);
  179. $modx->mail->set(modMail::MAIL_SENDER, $_sender);
  180. $modx->mail->set(modMail::MAIL_SUBJECT, $_subject);
  181. $modx->mail->address('to', $email, $name);
  182. $modx->mail->address('reply-to',$_reply);
  183. $modx->mail->setHTML(true);
  184. $sent = $modx->mail->send();
  185. $modx->mail->reset();
  186. }
  187. return '';