Skip to content

Commit

Permalink
Add capability to send activation emails. Resolves issue #2.
Browse files Browse the repository at this point in the history
  • Loading branch information
Simon Champion committed Jan 30, 2017
1 parent 904387d commit 786417f
Show file tree
Hide file tree
Showing 4 changed files with 94 additions and 2 deletions.
16 changes: 16 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,15 @@ 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.


Version History
----------------
* 1.0.0 2016-07-03: Initial release.
* 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.


Installation
Expand All @@ -57,16 +60,28 @@ 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.

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.


Limitations
----------------
The plugin is obviously only going to be useful if your Joomla site requires user accounts to be activated. If you set the site to not require account activation, the plugin will still function but will produce erroneous user notes.

The feature to send an activation email to the user is only available for activation events that are triggered by the admin. In addition, some of these events may trigger an email to the user already, depending on your site config. It is also possible that future Joomla updates or other plugins may add similar functionality which could also result in emails being sent twice. It is up to the site administrator to ensure that your site is configured to only send one email for each event.


Motivation
----------------
This plugin was written from scratch after attempting to resolve a user query over account activation. The account had been activated but the user claimed not to have done so himself and none of the site admins had done it either. We were unable to answer the question. Having the functionality in this plugin would have helped to resolve this.


To Do
-----

Expand All @@ -79,6 +94,7 @@ Caveats
* When upgrading, if the note text does not appear, or appears incorrectly, try going to the plugin config page and hitting 'Save'.
* With regard to the note generated when an admin user clicks an email link to activate an account: The plugin cannot know which admin user does this, so it can't specify them in the note text as it does for other events done by an admin user.


License
----------------
As with all Joomla extensions, this plugin is licensed under the GPLv2. The full license document should have been included with the source code.
12 changes: 12 additions & 0 deletions language/en-GB/en-GB.plg_user_notifyactivation.sys.ini
Original file line number Diff line number Diff line change
Expand Up @@ -26,3 +26,15 @@ PLG_USER_NOTIFYACTIVATION_ADMIN_VIA_PANEL_DESC = "Note text for accounts that ar

PLG_USER_NOTIFYACTIVATION_SUBJECT_DESC="The subject text for generated notes."
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_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"
PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_ACTIVATE_LABEL="When admin activates account"
PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_ACTIVATE_DESC="Send email to user when an admin clicks the link in the activation email."
PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_VIA_PANEL_LABEL="On activation via admin panel"
PLG_USER_NOTIFYACTIVATION_EMAIL_ON_ADMIN_VIA_PANEL_DESC="Send email to user when admin activates their account via the admin panel."
52 changes: 51 additions & 1 deletion notifyactivation.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,11 @@ public function onUserBeforeSave($oldUser, $isNew, $newUser)
$isBeingVerified = ($oldActivationKey && $newActivationKey) && ($oldActivationKey != $newActivationKey);

if($isBeingActivated) {
return $this->createActivationNote($newUser['id'], $this->getActivationMode($oldUser['params']));
$activationMode = $this->getActivationMode($oldUser['params']);
$this->sendActivationEmail($newUser, $activationMode);
return $this->createActivationNote($newUser['id'], $activationMode);
} elseif($isBeingVerified) {
$this->sendActivationEmail($newUser, self::MODE_USER_EMAIL_VERIFICATION);
return $this->createActivationNote($newUser['id'], self::MODE_USER_EMAIL_VERIFICATION);
}
}
Expand All @@ -44,6 +47,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);
return $this->createActivationNote($newUser['id'], $mode);
}
}
Expand Down Expand Up @@ -101,4 +105,50 @@ private function getAdminMessage($activationMode, $adminUser)
$userLink = "<a href='{$userLinkURL}' target='_blank'>{$adminUser->name}</a>";
return sprintf($this->params->get($activationMode, ''), $userLink);
}

protected function sendActivationEmail($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);

//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.
$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')
);

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

//make sure email content exists. Unlikely to fail, but we *really* don't want people getting blank emails.
if(!$emailContent) {
return;
}

$mailer = JFactory::getMailer();
$mailer->setSender([$config->get('mailfrom'), $config->get('fromname')]);
$mailer->addRecipient([$newUser['email']]);
$mailer->setSubject($emailSubject);
$mailer->setBody($emailContent);

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

protected function getEmailSender()
{
$config = JFactory::getConfig();
return [
$config->get( 'mailfrom' ),
$config->get( 'fromname' )
];
}
}
16 changes: 15 additions & 1 deletion notifyactivation.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<extension version="3.0" type="plugin" group="user" method="upgrade">
<name>PLG_USER_NOTIFYACTIVATION_NAME</name>
<version>2.0.0</version>
<version>2.1.0</version>
<creationDate>December 2016</creationDate>
<author>Simon Champion</author>
<authorEmail>[email protected]</authorEmail>
Expand Down Expand Up @@ -40,6 +40,20 @@
<field type="text" name="subject" default="User activation"
label="PLG_USER_NOTIFYACTIVATION_SUBJECT_LABEL" description="PLG_USER_NOTIFYACTIVATION_SUBJECT_DESC" />
</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>
</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>
</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>
</field>
</fieldset>
</fields>
</config>
</extension>

0 comments on commit 786417f

Please sign in to comment.