Skip to content

Commit

Permalink
v2.2: Can now send notification emails to admin users.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Champion committed Feb 6, 2017
1 parent 786417f commit 5fc0446
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 38 deletions.
11 changes: 5 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ All six of these events are recorded by the plugin, giving different messages in

Where the action is carried out by someone other than the account holder, the notes try to include information about who that person was. This applies to the actions done by admin users. In the case of the two actions done on the admin panel, the system can work this out by getting the user details for the logged in admin user. However in the case of the admin activation link, the admin user does not need to be logged in for it to work. All admin users are sent the same link, so for this event we cannot tell which admin user completed the activation; just that it was done by one of them. The other events done by admin users do require the admin user to be logged in, and thus the plugin can report who did them.

In addition, as of version 2.1, the plugin can also send users an email notifying them of their account being created or activated by an administrator.
In addition, as of version 2.1, the plugin can also send users an email notifying them of their account being created or activated by an administrator. And in 2.2, the site administrators can be notified by email as well.


Version History
Expand All @@ -43,6 +43,7 @@ Version History
* 1.0.1 2016-07-14: Bug fixes: Now works in all three scenarios.
* 2.0.0 2016-12-13: Made it work with the "Administrator" option for new account activation.
* 2.1.0 2017-01-30: Added ability to send activation emails.
* 2.2.0 2017-02-06: Activation emails can now go to either the end user or to admin users (or both).


Installation
Expand All @@ -60,14 +61,11 @@ The plugin has several parameters:

* Subject Text - this is the text that will be used for the for the subject text on the user notes.

There is also a second configuration tab, for emails. This tab has three toggle switches that allow you to set the plugin to send emails to the user when their account is activated by the administrator, either by the administator creating the account manually, activating the account via an email link, or directly in the admin panel.
There is also a second configuration tab, for emails. This tab has three toggle switches that allow you to set the plugin to send emails to the end user when their account is activated by the administrator, either by the administator creating the account manually, activating the account via an email link, or directly in the admin panel. You can also use these switches to send notification emails to all the admin users of the system.

Note: You should only enable these switches if your system does not already send emails to notify the user of account activation in these three cases. Depending on your Joomla config and other plugins, you may not need all of these switches enabled.

The email that is sent to users in response to these three events uses the same text as Joomla's built-in account activation notification. This text is taken from the standard language translation files provided with Joomla. If you wish to override it, the relevant translation IDs are:

* COM_USERS_EMAIL_ACTIVATED_BY_ADMIN_ACTIVATION_SUBJECT for the email subject line.
* COM_USERS_EMAIL_ACTIVATED_BY_ADMIN_ACTIVATION_BODY for the email body text.
The email that is sent to users in response to these three events uses the same text as Joomla's built-in account activation notification. This default text is copied from the standard language translation files provided with Joomla, but independant translations are provided by the plugin. If you wish to override it, the relevant translation IDs can be found in the en-GB.plg_user_notifyactivation.ini file provided with this plugin.


Limitations
Expand All @@ -88,6 +86,7 @@ To Do
* Move the message strings into a language translation file rather than having them as config settings.
* Add text to the end of the notes stating that they were generated by the plugin.


Caveats
-------

Expand Down
13 changes: 13 additions & 0 deletions language/en-GB/en-GB.plg_user_notifyactivation.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
; @package NotifyActivation
; @language English
; @version 1.0.0
; @author Simon Champion
; @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html

PLG_USER_NOTIFYACTIVATION_NAME="User - NotifyActivation"

PLG_USER_NOTIFYACTIVATION_EMAIL_ACTIVATION_NOTIFICATION_TO_ADMIN_BODY="Hello administrator,\n\nThis is to notify you of an account activation at %s.\nThe account in question is now fully activated and can be used to log into the site.\nThis email contains their details:\n\n Name : %s \n email: %s \n Username: %s \n\nThis is a notification email only; you do not need to do any further action.\n"
PLG_USER_NOTIFYACTIVATION_EMAIL_ACTIVATION_NOTIFICATION_TO_ADMIN_SUBJECT="Account activation notification of %s at %s"

PLG_USER_NOTIFYACTIVATION_EMAIL_ACTIVATION_NOTIFICATION_TO_USER_BODY="Hello %s,\n\nYour account has been activated by an administrator. You can now login at %s using the username %s and the password you chose while registering."
PLG_USER_NOTIFYACTIVATION_EMAIL_ACTIVATION_NOTIFICATION_TO_USER_SUBJECT="Account activated for %s at %s"
6 changes: 4 additions & 2 deletions language/en-GB/en-GB.plg_user_notifyactivation.sys.ini
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
; @license GNU/GPLv3 http://www.gnu.org/licenses/gpl-3.0.html

PLG_USER_NOTIFYACTIVATION_NAME="User - NotifyActivation"
PLG_USER_NOTIFYACTIVATION_DESCRIPTION="This is a simple plugin that adds a user note when an account is activated, stating who activated it and how. This information is not stored by Joomla, but is useful, eg for keeping track of accounts that needed admin intervention."
PLG_USER_NOTIFYACTIVATION_DESCRIPTION="This is a simple plugin that adds a user note when an account is activated, stating who activated it and how. This information is not normally stored by Joomla, but is useful, eg for keeping track of accounts that needed admin intervention."
PLG_USER_NOTIFYACTIVATION="User - NotifyActivation"

PLG_USER_NOTIFYACTIVATION_CATEGORY_DESC="Specify a user notes category for the generated notes"
Expand All @@ -29,8 +29,10 @@ PLG_USER_NOTIFYACTIVATION_SUBJECT_LABEL="Subject Text"

PLG_USER_NOTIFYACTIVATION_FIELDSET_EMAIL="Email Sending"

PLG_USER_NOTIFYACTIVATION_EMAIL_YES="Send email to user"
PLG_USER_NOTIFYACTIVATION_EMAIL_NO="Do NOT send email"
PLG_USER_NOTIFYACTIVATION_EMAIL_YES_USER="Send email to user"
PLG_USER_NOTIFYACTIVATION_EMAIL_YES_ADMIN="Send email to admins"
PLG_USER_NOTIFYACTIVATION_EMAIL_YES_BOTH="Send email to user and admins"

PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_INSTANT_LABEL="When created by admin"
PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_INSTANT_DESC="Send email to user when account is created by admin and activated immediately"
Expand Down
108 changes: 81 additions & 27 deletions notifyactivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,19 @@ class plgUserNotifyActivation extends JPlugin
const MODE_ADMIN_EMAIL_ACTIVATION = 'admin_email_activation';
const MODE_ADMIN_PANEL_ACTIVATION = 'admin_panel_activation';

const SEND_TO_NONE = 0;
const SEND_TO_USER = 1;
const SEND_TO_ADMIN = 2;
const SEND_TO_BOTH = 3;

/**
* Load the language file on instantiation.
*
* @var boolean
* @since 3.1
*/
protected $autoloadLanguage = true;

public function onUserBeforeSave($oldUser, $isNew, $newUser)
{
$oldActivationKey = isset($oldUser['activation']) ? $oldUser['activation'] : '';
Expand All @@ -32,10 +45,10 @@ public function onUserBeforeSave($oldUser, $isNew, $newUser)

if($isBeingActivated) {
$activationMode = $this->getActivationMode($oldUser['params']);
$this->sendActivationEmail($newUser, $activationMode);
$this->sendActivationEmails($newUser, $activationMode);
return $this->createActivationNote($newUser['id'], $activationMode);
} elseif($isBeingVerified) {
$this->sendActivationEmail($newUser, self::MODE_USER_EMAIL_VERIFICATION);
$this->sendActivationEmails($newUser, self::MODE_USER_EMAIL_VERIFICATION);
return $this->createActivationNote($newUser['id'], self::MODE_USER_EMAIL_VERIFICATION);
}
}
Expand All @@ -47,7 +60,7 @@ public function onUserAfterSave($newUser, $isNew, $success)
if($success && $isNew && !$newUser['activation']) {
$loggedInUser = JFactory::getUser();
$mode = ($loggedInUser->id > 0) ? self::MODE_ADMIN_INSTANT_ACTIVATION : self::MODE_USER_INSTANT_ACTIVATION;
$this->sendActivationEmail($newUser, $mode);
$this->sendActivationEmails($newUser, $mode);
return $this->createActivationNote($newUser['id'], $mode);
}
}
Expand Down Expand Up @@ -106,49 +119,90 @@ private function getAdminMessage($activationMode, $adminUser)
return sprintf($this->params->get($activationMode, ''), $userLink);
}

protected function sendActivationEmail($newUser, $activationMode)
protected function sendActivationEmails($newUser, $activationMode)
{
//we're sharing com_user's language file so we can send the same email text, so make sure it's loaded.
//(it should be loaded already as this plugin is called from a com_users event, but good to be certain)
$lang = JFactory::getLanguage();
$lang->load('com_users', JPATH_ROOT, null, true);
$lang->load('plg_notifyactivation', JPATH_ROOT, null, true);

//note: the lang strings use for the emails are for admin activation. Only $activationMode types that are
//relevant for this email content will have activation email options in config. Other types will return 0.
//Only $activationMode types that are relevant will have activation email options in config.
//Other types will always return 0 as the default.
$shouldSendEmail = (int)$this->params->get('email_on_'.$activationMode, 0);
if(!$shouldSendEmail) {
return;
}

$config = JFactory::getConfig();
$emailSubject = JText::sprintf('COM_USERS_EMAIL_ACTIVATED_BY_ADMIN_ACTIVATION_SUBJECT',
$newUser['name'], $config->get('sitename')
if ($this->shouldSendToUser($shouldSendEmail)) {
$this->sendUserEmail($newUser, $activationMode, $config);
}
if ($this->shouldSendToAdmin($shouldSendEmail)) {
$this->sendAdminEmail($newUser, $activationMode, $config);
}
}

protected function sendUserEmail($newUser, $activationMode, $config)
{
$siteName = $config->get('sitename');
$emailSubject = JText::sprintf('PLG_USER_NOTIFYACTIVATION_EMAIL_ACTIVATION_NOTIFICATION_TO_USER_SUBJECT',
$newUser['name'], $siteName
);

$emailBody = JText::sprintf('COM_USERS_EMAIL_ACTIVATED_BY_ADMIN_ACTIVATION_BODY',
$newUser['name'], $config->get('sitename'), $newUser['username']
$emailBody = JText::sprintf('PLG_USER_NOTIFYACTIVATION_EMAIL_ACTIVATION_NOTIFICATION_TO_USER_BODY',
$newUser['name'], $siteName, $newUser['username']
);

//make sure email content exists. Unlikely to fail, but we *really* don't want people getting blank emails.
if(!$emailContent) {
return;
if($emailBody) {
$this->sendEmail($config, [$newUser['email']], $emailSubject, $emailBody);
}
}

protected function sendAdminEmail($newUser, $activationMode, $config)
{
$siteName = $config->get('sitename');
$emailSubject = JText::sprintf('PLG_USER_NOTIFYACTIVATION_EMAIL_ACTIVATION_NOTIFICATION_TO_ADMIN_SUBJECT',
$newUser['name'], $siteName
);

$emailBody = JText::sprintf('PLG_USER_NOTIFYACTIVATION_EMAIL_ACTIVATION_NOTIFICATION_TO_ADMIN_BODY',
$siteName, $newUser['name'], $newUser['email'], $newUser['username']
);

//make sure email content exists. Unlikely to fail, but we *really* don't want people getting blank emails.
if($emailBody) {
$recipients = $this->loadSysAdminEmailAddresses();
$this->sendEmail($config, $recipients, $emailSubject, $emailBody);
}

}

protected function sendEmail($config, $recipients, $subject, $body)
{
$mailer = JFactory::getMailer();
$mailer->setSender([$config->get('mailfrom'), $config->get('fromname')]);
$mailer->addRecipient([$newUser['email']]);
$mailer->setSubject($emailSubject);
$mailer->setBody($emailContent);
$mailer->addRecipient($recipients);
$mailer->setSubject($subject);
$mailer->setBody($body);

$send = $mailer->Send();
}

protected function getEmailSender()
protected function shouldSendToUser($flag)
{
$config = JFactory::getConfig();
return [
$config->get( 'mailfrom' ),
$config->get( 'fromname' )
];
return ($flag === (int)self::SEND_TO_USER || $flag === (int)self::SEND_TO_BOTH);
}

protected function shouldSendToAdmin($flag)
{
return ($flag === (int)self::SEND_TO_ADMIN || $flag === (int)self::SEND_TO_BOTH);
}

protected function loadSysAdminEmailAddresses()
{
$db = JFactory::getDBO();
$query = $db->getQuery(true)
->select($db->quoteName(['email']))
->from($db->quoteName('#__users'))
->where($db->quoteName('block') . ' = 0')
->where($db->quoteName('sendEmail') . ' = 1');
$db->setQuery($query);
return $db->loadColumn();
}
}
13 changes: 10 additions & 3 deletions notifyactivation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

<languages>
<language tag="en-GB">language/en-GB/en-GB.plg_user_notifyactivation.sys.ini</language>
<language tag="en-GB">language/en-GB/en-GB.plg_user_notifyactivation.ini</language>
</languages>

<config>
Expand All @@ -42,16 +43,22 @@
</fieldset>
<fieldset name="email" label="PLG_USER_NOTIFYACTIVATION_FIELDSET_EMAIL">
<field name="email_on_admin_instant_activation" type="radio" label="PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_INSTANT_LABEL" description="PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_INSTANT_DESC" class="btn-group btn-group-yesno" default="0">
<option value="1">PLG_USER_NOTIFYACTIVATION_EMAIL_YES</option>
<option value="0">PLG_USER_NOTIFYACTIVATION_EMAIL_NO</option>
<option value="1">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_USER</option>
<option value="2">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_ADMIN</option>
<option value="3">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_BOTH</option>
</field>
<field name="email_on_admin_email_activation" type="radio" label="PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_ACTIVATE_LABEL" description="PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_ACTIVATE_DESC" class="btn-group btn-group-yesno" default="0">
<option value="1">PLG_USER_NOTIFYACTIVATION_EMAIL_YES</option>
<option value="0">PLG_USER_NOTIFYACTIVATION_EMAIL_NO</option>
<option value="1">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_USER</option>
<option value="2">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_ADMIN</option>
<option value="3">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_BOTH</option>
</field>
<field name="email_on_admin_panel_activation" type="radio" label="PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_VIA_PANEL_LABEL" description="PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_VIA_PANEL_DESC" class="btn-group btn-group-yesno" default="0">
<option value="1">PLG_USER_NOTIFYACTIVATION_EMAIL_YES</option>
<option value="0">PLG_USER_NOTIFYACTIVATION_EMAIL_NO</option>
<option value="1">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_USER</option>
<option value="2">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_ADMIN</option>
<option value="3">PLG_USER_NOTIFYACTIVATION_EMAIL_YES_BOTH</option>
</field>
</fieldset>
</fields>
Expand Down

0 comments on commit 5fc0446

Please sign in to comment.