Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Task #182358 feat: Send email while creating user using API #34

Open
wants to merge 1 commit into
base: release-2.1.0
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions src/language/en-GB/en-GB.plg_api_users.ini
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,6 @@ PLG_API_USERS_UNSUPPORTED_METHOD_POST="unsupported method,please use get method"
PLG_API_USERS_USERS="users/"
PLG_API_USERS_IN_DELETE="in delete"
PLG_API_USERS_IN_POST="in post"

PLG_API_USERS_NEW_USER_EMAIL_BODY="Hello %s,\n\n\nYou have been added as a User to %s by an Administrator.\n\nThis email has your username and password to log in to %s\n\nUsername: %s\nPassword: %s\n\n\nPlease do not respond to this message as it is automatically generated and is for information purposes only."
PLG_API_USERS_NEW_USER_EMAIL_SUBJECT="New User Details"
83 changes: 83 additions & 0 deletions src/users/user.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

// No direct access.
defined('_JEXEC') or die();
use Joomla\Registry\Registry;

/**
* User Api.
Expand All @@ -19,6 +20,8 @@
*/
class UsersApiResourceUser extends ApiResource
{
public $shouldSendMail = 1;

/**
* Function to create and edit user record.
*
Expand All @@ -44,6 +47,16 @@ public function post()
return;
}

if (isset($formData['password']))
{
$formData['password_clear'] = $formData['password'];
}

if (isset($formData['sendmail']))
{
$this->shouldSendMail = $formData['sendmail'];
}

// Get current logged in user.
$my = JFactory::getUser();

Expand Down Expand Up @@ -243,6 +256,11 @@ private function storeUser($user, $formData, $isNew = 0)

if ($isNew)
{
if ($this->shouldSendMail)
{
$mail_sent = $this->sendNewUserEmail($formData);
}

$response->message = JText::_('PLG_API_USERS_ACCOUNT_CREATED_SUCCESSFULLY_MESSAGE');
}
else
Expand Down Expand Up @@ -358,4 +376,69 @@ private function retriveUser($xidentifier, $userIdentifier)

return $user;
}

/**
* Send new user create mail
*
* @param array $user this contains user data.
*
* @return object
*/
public function sendNewUserEmail($user)
{
$app = JFactory::getApplication();
$lang = JFactory::getLanguage();
$defaultLocale = $lang->getTag();

/**
* Look for user language. Priority:
* 1. User frontend language
* 2. User backend language
*/
$userParams = new Registry($user['params']);
$userLocale = $userParams->get('language', $userParams->get('admin_language', $defaultLocale));

if ($userLocale !== $defaultLocale)
{
$lang->setLanguage($userLocale);
}

$lang->load('plg_api_users', JPATH_ADMINISTRATOR);

// Compute the mail subject.
$emailSubject = JText::sprintf(
'PLG_API_USERS_NEW_USER_EMAIL_SUBJECT',
$user['name'],
$app->get('sitename')
);

// Compute the mail body.
$emailBody = JText::sprintf(
'PLG_API_USERS_NEW_USER_EMAIL_BODY',
$user['name'],
$app->get('sitename'),
JUri::root(),
$user['username'],
$user['password_clear']
);

$res = JFactory::getMailer()->sendMail(
$app->get('mailfrom'),
$app->get('fromname'),
$user['email'],
$emailSubject,
$emailBody
);

if ($res === false)
{
$app->enqueueMessage(JText::_('JERROR_SENDING_EMAIL'), 'warning');
}

// Set application language back to default if we changed it
if ($userLocale !== $defaultLocale)
{
$lang->setLanguage($defaultLocale);
}
}
}