---
title: "Let Users Log In with Their Email Address in Joomla - Code Snippets"
description: "Let your users log in with their email address or their username, so nobody gets stuck at your Joomla login form, using one PHP snippet."
url: "https://www.tassos.gr/docs/code-snippets/howto/access-control-security/login-with-email"
date: "2026-10-23T00:40:23+00:00"
language: "en-GB"
---

[ Home ](https://www.tassos.gr/docs) / [ Code Snippets ](https://www.tassos.gr/docs/code-snippets) / [ How-to Guides ](https://www.tassos.gr/docs/code-snippets/howto) / [ Access Control & Security ](https://www.tassos.gr/docs/code-snippets/howto/access-control-security)

#  Let Users Log In with Their Email Address in Joomla

##  Let your users log in with their email address or their username, so nobody gets stuck at your Joomla login form, using one PHP snippet.

 ![Let Users Log In with Their Email Address in Joomla](https://www.tassos.gr/images/2026/10/code-snippets-login-with-email-card.png) Heads up! This article contains PHP code and is intended for developers. We offer this code as a courtesy, but don't provide support for code customizations or 3rd party development.

Joomla's login form only accepts a username. Most people remember their email address instead, so they type that, get "Username and password do not match", and email you for help. You could install an authentication plugin to fix it, or you can add one PHP snippet with Code Snippets.

## [The PHP snippet code](#the-php-snippet-code)

Set **Code Type** to `PHP`, then use this snippet. When someone submits the login form with an email address, it looks up the account that owns that email and hands its username to Joomla. Joomla's own login then runs as usual, so it still checks the password and every other login rule.

It also covers the last step of a password reset. Joomla emails a verification code, then asks for the **username** along with that code. With this snippet, users can type their email there too.

```
use Joomla\CMS\Factory;
use Joomla\Database\DatabaseInterface;

$app   = Factory::getApplication();
$input = $app->getInput();

if ($input->getMethod() !== 'POST' || $input->getCmd('option') !== 'com_users')
{
    return;
}

$task = $input->getCmd('task');

if (!in_array($task, ['user.login', 'reset.confirm'], true))
{
    return;
}

// Login posts "username", the reset confirm form posts "jform[username]"
$jform = $input->get('jform', [], 'array');
$login = $task === 'user.login'
    ? $input->post->getString('username')
    : (string) ($jform['username'] ?? '');
$login = trim($login);

if (!filter_var($login, FILTER_VALIDATE_EMAIL))
{
    return;
}

$db = Factory::getContainer()->get(DatabaseInterface::class);

// Someone's username is literally this email, so leave it alone
$query = $db->createQuery()
    ->select('COUNT(*)')
    ->from($db->quoteName('#__users'))

    ->where($db->quoteName('username') . ' = :login')
    ->bind(':login', $login);

if ((int) $db->setQuery($query)->loadResult() > 0)
{
    return;
}

// Find the username that owns this email
$query = $db->createQuery()
    ->select($db->quoteName('username'))
    ->from($db->quoteName('#__users'))

    ->where($db->quoteName('email') . ' = :email')
    ->bind(':email', $login);

$usernames = $db->setQuery($query, 0, 2)->loadColumn();

// Swap only when exactly one account matches
if (count($usernames) !== 1)
{
    return;
}

if ($task === 'user.login')
{
    $input->post->set('username', $usernames[0]);
}
else
{
    $jform['username'] = $usernames[0];
    $input->set('jform', $jform);
}
```

A few things worth knowing:

- Usernames still work. The snippet only acts when the value is a valid email address.
- A wrong email or password shows Joomla's usual error, so the form doesn't reveal which emails have an account.
- If an account's username is itself an email address, that account comes first and the snippet leaves the value alone.
- If 2 accounts share the same email, the snippet doesn't guess. It leaves the value alone.
- It works with both the login page and the Login module.

## [How to add this snippet](#how-to-add-this-snippet)

1. Install [Code Snippets](https://www.tassos.gr/docs/code-snippets/getting-started/how-to-install) if it isn't installed already.
2. Log into your Joomla backend.
3. Go to **Components** → **Tassos Code Snippets**.
4. Click **New**.
5. Enter a title, for example *Login with email*.
6. Set **Code Type** to **PHP**.
7. Set **Insertion Method** to **Auto-Insert Locations**.
8. Set **Location** to **On Page Load**.
9. Paste the code above into the editor.
10. Click **Save & Close**.

Leave the **Conditional Logic** tab empty. The snippet checks the request itself and stops right away on every other page.

 PHP snippets run on the frontend only, so your Joomla Administrator login still needs a username.

## [Change the "Username" label](#change-the-username-label)

The login form still says *Username*, so tell your users they can type their email too. Joomla's language overrides change that text without touching any files. You need 3 of them: one for the login page, one for the Login module, and one for the password reset step.

1. Log into your Joomla backend.
2. Go to **System** → **Manage** → **Language Overrides**.
3. In the language filter, select your site language, for example **English (en-GB) - Site**.
4. Click **New**.
5. Enter `COM_USERS_LOGIN_USERNAME_LABEL` in **Language Constant**.
6. Enter `Username or Email` in **Text**.
7. Click **Save & New**.
8. Repeat steps 5 to 7 for `MOD_LOGIN_VALUE_USERNAME` and `COM_USERS_FIELD_RESET_CONFIRM_USERNAME_LABEL`.

That's it. Your users can now log in with their username or their email address, and you didn't have to install another plugin.

To send each user group to its own page after login, see [Redirect Users After Login by User Group in Joomla](https://www.tassos.gr/docs/code-snippets/howto/access-control-security/redirect-after-login). The 2 snippets work together. To learn more about how PHP snippets work, read the [PHP snippet docs](https://www.tassos.gr/docs/code-snippets/types/php).

 Last updated on Oct 15th 2026 18:10
