daddl3 / email_with_login_link_and_change_password
Symfony Bundle for automatic E-Mail sending with Login Link
Package info
gitlab.com/daddl3/email-with-login-link-and-change-password
Type:symfony-bundle
pkg:composer/daddl3/email_with_login_link_and_change_password
Requires
- php: ^8.5
- doctrine/doctrine-bundle: ^3
- doctrine/doctrine-migrations-bundle: ^4
- doctrine/orm: ^3
- symfony/config: ^8
- symfony/dependency-injection: ^8
- symfony/form: ^8
- symfony/framework-bundle: ^8
- symfony/mailer: ^8
- symfony/messenger: ^8
- symfony/security-bundle: ^8
- symfony/translation: ^8
- symfony/twig-bundle: ^8
- symfony/validator: ^8
- symfony/yaml: ^8
Requires (Dev)
- roave/security-advisories: dev-latest
README
Email Login When Creating A User And Change Password
Send an E-Mail with a Login Link via Symfony Messenger.
Getting started
This bundle dispatches an EmailOnCreationEvent which is converted into a
SendUserCreationEmailMessage and processed asynchronously by a Messenger
worker. The handler creates a login link for the new user and sends the
welcome E-Mail.
Installation
$ composer require daddl3/email_with_login_link_and_change_password
Requirements
- PHP
^8.5 - Symfony
^8
Configuration
Security firewall
Add login_link to the firewall that protects your application:
security:
firewalls:
main:
login_link:
check_route: login_check
lifetime: 86400
signature_properties: ['id', 'email']
signature_properties must reference properties on your user entity. The
fields email and id are required by this bundle, fullname is optional.
The bundle exposes its own check route as daddl3_login_check. Either point
check_route to that name, or declare a login_check route in your app that
the login_link authenticator intercepts:
#[Route('/login_check', name: 'login_check')]
public function loginCheck(): never
{
throw new LogicException('Intercepted by the login_link authenticator');
}
Messenger
Route the message to an asynchronous transport so E-Mail sending happens outside the request cycle:
framework:
messenger:
transports:
email:
dsn: '%env(MESSENGER_TRANSPORT_DSN)%'
options:
queue_name: 'email'
table_name: 'email'
routing:
daddl3\EmailWithLoginLinkAndChangePasswordBundle\Message\SendUserCreationEmailMessage: email
Then run a worker:
$ bin/console messenger:consume email
User entity
Implement daddl3\EmailWithLoginLinkAndChangePasswordBundle\Contract\EmailUserInterface
on your user entity. The interface adds these methods on top of
UserInterface and PasswordAuthenticatedUserInterface:
public function getEmail(): ?string;
public function setPassword(string $password): void;
public function setSetOwnPassword(bool $value): void;
public function isOwnPasswordIsAlreadySet(): bool;
public function getFullname(): ?string;
Dispatching the event
use daddl3\EmailWithLoginLinkAndChangePasswordBundle\Event\EmailOnCreationEvent;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
$this->eventDispatcher->dispatch(new EmailOnCreationEvent(
emailUser: $user,
sender: new Address('email@example.de', 'Mr. Daddl3'),
subject: 'Welcome',
cc: 'cc@example.de',
bcc: 'bcc@example.de',
replyTo: new Address('reply@example.de'),
priority: Email::PRIORITY_HIGH,
));
Overriding the E-Mail template
Place your override at:
templates/bundles/Daddl3EmailWithLoginLinkAndChangePasswordBundle/email.html.twig
The template receives loginLink (string), user (your EmailUserInterface
instance) and message (the dispatched SendUserCreationEmailMessage).