PHPackages                             danielburger1337/2fa-email - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Authentication &amp; Authorization](/categories/authentication)
4. /
5. danielburger1337/2fa-email

ActiveSymfony-bundle[Authentication &amp; Authorization](/categories/authentication)

danielburger1337/2fa-email
==========================

Extends scheb/2fa-bundle with two-factor authentication via email

v2.0.1(2mo ago)537.8k↑22.9%1MITPHPPHP ^8.4

Since Feb 5Pushed 2mo ago1 watchersCompare

[ Source](https://github.com/danielburger1337/scheb-2fa-email)[ Packagist](https://packagist.org/packages/danielburger1337/2fa-email)[ Docs](https://github.com/danielburger1337/scheb-2fa-email)[ RSS](/packages/danielburger1337-2fa-email/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (4)Dependencies (16)Versions (5)Used By (0)

[![PHPCSFixer](https://github.com/danielburger1337/scheb-2fa-email/actions/workflows/phpcsfixer.yml/badge.svg)](https://github.com/danielburger1337/scheb-2fa-email/actions/workflows/phpcsfixer.yml)[![PHPStan](https://github.com/danielburger1337/scheb-2fa-email/actions/workflows/phpstan.yml/badge.svg)](https://github.com/danielburger1337/scheb-2fa-email/actions/workflows/phpstan.yml)[![PHPUnit](https://github.com/danielburger1337/scheb-2fa-email/actions/workflows/phpunit.yml/badge.svg)](https://github.com/danielburger1337/scheb-2fa-email/actions/workflows/phpunit.yml)[![Packagist Version](https://camo.githubusercontent.com/8f0fffaf8dd8a9df48c5c1ab6c6fe55295a1c4bbf44c7591ecb72d21cb64d7ae/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64616e69656c627572676572313333372f3266612d656d61696c3f6c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b6167657325324664616e69656c627572676572313333372532463266612d656d61696c)](https://camo.githubusercontent.com/8f0fffaf8dd8a9df48c5c1ab6c6fe55295a1c4bbf44c7591ecb72d21cb64d7ae/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f64616e69656c627572676572313333372f3266612d656d61696c3f6c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b6167657325324664616e69656c627572676572313333372532463266612d656d61696c)[![Packagist Downloads](https://camo.githubusercontent.com/5450440c5df995171942819f64ef062c74793fe8ae96ea239d980b0fffb68038/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64616e69656c627572676572313333372f3266612d656d61696c3f6c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b6167657325324664616e69656c627572676572313333372532463266612d656d61696c)](https://camo.githubusercontent.com/5450440c5df995171942819f64ef062c74793fe8ae96ea239d980b0fffb68038/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f64616e69656c627572676572313333372f3266612d656d61696c3f6c696e6b3d68747470732533412532462532467061636b61676973742e6f72672532467061636b6167657325324664616e69656c627572676572313333372532463266612d656d61696c)

danielburger1337/2fa-email
==========================

[](#danielburger13372fa-email)

This bundle is an extension of [scheb/2fa-bundle](https://github.com/scheb/2fa-bundle) that provides a more advanced email two-factor provider than the default [scheb/2fa-email](https://github.com/scheb/2fa-email) provider.

It adds the ability to let an authentication code expire (by default 15 minutes) and makes the customization of the generated email message a bit more developer friendly.

Installation
------------

[](#installation)

```
composer install danielburger1337/2fa-email
```

```
// config/bundles.php
return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Scheb\TwoFactorBundle\SchebTwoFactorBundle::class => ['all' => true],
    danielburger1337\SchebTwoFactorBundle\TwoFactorEmailBundle::class => ['all' => true],
];
```

---

Customize the email
-------------------

[](#customize-the-email)

There are multiple ways you can customize the creation and sending of the authentication code message.

By default, the bundle generates a [bare bones](src/Mailer/SymfonyAuthCodeEmailGenerator.php) email. You can customize the subject and text body by setting the `email_subject`, `email_body`, `sender_email` and `sender_name` parameters. The "{{AUTH\_CODE}}" string in the `email_body` template will be replaced with the actual auth code when the email is sent.

You can change the symfony/mime email message generation (e.g. create a twig TemplatedEmail) by creating a service that implements the [AuthCodeEmailGeneratorInterface](src/Mailer/AuthCodeEmailGeneratorInterface.php) and setting the `email_generator` parameter to that service id.

The generated message is then sent via symfony/mailer and the `mailer.mailer` service by default. If you want to use a different symfony/mailer service to send the messages, simply set the service id to the `symfony_mailer` parameter.

Lastly, if you dont want to use symfony/mailer at all, you can create a service that implements [AuthCodeMailerInterface](src/Mailer/AuthCodeMailerInterface.php) that handles the message generation and sending completly on its own. To use this service, all you have to do is set the `mailer` parameter to that services id.

---

Resend an authentication code
-----------------------------

[](#resend-an-authentication-code)

If you want to resend the authentication message (maybe the message got lost in transit), the easiest way is to use a `RequestEvent::class` event listener and inject te [AuthCodeMailerInterface](src/Mailer/AuthCodeMailerInterface.php) service and call the "sendAuthCode" method.

Using a "normal" route doesn't work by default because scheb/2fa-bundle will always redirect that route to the 2fa endpoint.

```
declare(strict_types=1);

use danielburger1337\SchebTwoFactorBundle\Mailer\AuthCodeMailerInterface;
use danielburger1337\SchebTwoFactorBundle\Model\TwoFactorEmailInterface;
use Scheb\TwoFactorBundle\Security\Authentication\Token\TwoFactorTokenInterface;
use Symfony\Component\EventDispatcher\Attribute\AsEventListener;
use Symfony\Component\HttpKernel\Event\RequestEvent;
use Symfony\Component\RateLimiter\RateLimiterFactory;
use Symfony\Component\Security\Core\Authentication\Token\Storage\TokenStorageInterface;

#[AsEventListener(RequestEvent::class)]
class ResendEmailAuthCodeEventListener
{

    public function __construct(
        private readonly RateLimiterFactory $rateLimiterFactory,
        private readonly AuthCodeMailerInterface $authCodeMailer,
        private readonly TokenStorageInterface $tokenStorage,
    ) {
    }

    public function __invoke(RequestEvent $event): void
    {
        $request = $event->getRequest();

        if ($request->attributes->get('_route') !== '2fa_login') {
            return;
        }

        $token = $this->tokenStorage->getToken();
        $user = $token?->getUser();

        if (!$token instanceof TwoFactorTokenInterface || !$user instanceof TwoFactorEmailInterface) {
            return;
        }

        // somehow determine that you want to resend the email
        if ($request->request->get('resendAuthCode') === 'true') {
            // If you use rate limiting, make sure to also use the auth code as key,
            // otherwise the user might get throttled when their code has expired and a new one should be sent.
            $rateLimiter = $this->rateLimiterFactory->create(
                'tfa_email_'.\hash('xxh128', $user->getEmailAuthCode().$user->getEmailAuthRecipient())
            );

            if ($rateLimiter->consume(1)->isAccepted()) {
                // mail the auth code
                $this->authCodeMailer->sendAuthCode($user);
            }
        }
    }

}
```

---

Configuration Reference
-----------------------

[](#configuration-reference)

The listed values are the default values. Every value is optional.

```
# config/packages/two_factor_email.yaml
two_factor_email:
    # A custom service to manage the auth code
    # It must implement AuthCodeProviderInterface
    auth_code_provider: null
    # This option is only used when the default `auth_code_provider` is used:
    # A \DateInterval compatible value that sets
    # how long an auth code is considered valid.
    # `null` disables expiration.
    expires_after: PT15M

    # ---------------------------------------------

    # A custom service that creates the auth code
    # It must implement AuthCodeGeneratorInterface
    code_generator: null
    # This option is only used when the default `code_generator` is used:
    # The length of the generated auth code
    digits: 6

    # A custom service that sends the auth code to the user
    # It must implement AuthCodeMailerInterface
    # The default implementation has a hard dependency on symonfy/mailer,
    # so make sure that you have the package installed.
    mailer: null
    # A custom symfony/mailer service to send the emails with.
    # "mailer.mailer" is the default symfony/mailer service.
    symfony_mailer: mailer.mailer
    # A custom service that generates the mime email messsage to send
    # It must implement AuthCodeEmailGeneratorInterface.
    email_generator: null
    # Subject of the generated email
    email_subject: Authentication Code
    # Text message body of the generated email
    # "{{AUTH_CODE}}" is a template string that will be replaced with the actual auth code.
    email_body: "{{AUTH_CODE}}"
    # "From" header address
    sender_email: null
    # "From" header name
    sender_name: null

    # A custom form renderer service that renders the 2fa form.
    # It must implement TwoFactorFormRendererInterface.
    form_renderer: null
    # The twig template to render when no custom form renderer was defined.
    template: "@SchebTwoFactor/Authentication/form.html.twig"
```

---

License
-------

[](#license)

This software is available under the [MIT](LICENSE) license.

###  Health Score

52

—

FairBetter than 96% of packages

Maintenance86

Actively maintained with recent releases

Popularity34

Limited adoption so far

Community10

Small or concentrated contributor base

Maturity62

Established project with proven stability

 Bus Factor1

Top contributor holds 95.7% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~253 days

Total

4

Last Release

73d ago

Major Versions

v1.1.0 → v2.0.02025-12-05

PHP version history (2 changes)v1.0.0PHP ^8.2

v2.0.0PHP ^8.4

### Community

Maintainers

![](https://www.gravatar.com/avatar/b54ebcf5961630f5ac30dc9d15d304a286fe22d14ebe78470c9ca9ad12e285db?d=identicon)[danielburger1337](/maintainers/danielburger1337)

---

Top Contributors

[![danielburger1337](https://avatars.githubusercontent.com/u/48986191?v=4)](https://github.com/danielburger1337 "danielburger1337 (44 commits)")[![craigh](https://avatars.githubusercontent.com/u/350048?v=4)](https://github.com/craigh "craigh (2 commits)")

---

Tags

symfonyAuthenticationemail2fatwo-factortwo-stepscheb-2fa

###  Code Quality

TestsPHPUnit

Static AnalysisPHPStan

Code StylePHP CS Fixer

Type Coverage Yes

### Embed Badge

![Health badge](/badges/danielburger1337-2fa-email/health.svg)

```
[![Health](https://phpackages.com/badges/danielburger1337-2fa-email/health.svg)](https://phpackages.com/packages/danielburger1337-2fa-email)
```

###  Alternatives

[scheb/2fa-email

Extends scheb/2fa-bundle with two-factor authentication via email

354.3M16](/packages/scheb-2fa-email)[scheb/2fa-bundle

A generic interface to implement two-factor authentication in Symfony applications

7014.0M62](/packages/scheb-2fa-bundle)[scheb/2fa-google-authenticator

Extends scheb/2fa-bundle with two-factor authentication using Google Authenticator

298.2M30](/packages/scheb-2fa-google-authenticator)[scheb/2fa-trusted-device

Extends scheb/2fa-bundle with trusted devices support

355.1M16](/packages/scheb-2fa-trusted-device)[scheb/2fa

Two-factor authentication for Symfony applications (please use scheb/2fa-bundle to install)

578630.7k1](/packages/scheb-2fa)[scheb/2fa-totp

Extends scheb/2fa-bundle with two-factor authentication using TOTP

292.7M22](/packages/scheb-2fa-totp)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
