replaced mailer library, added email templates (#1462)
This commit is contained in:
24
src/Configuration/MailConfiguration.php
Normal file
24
src/Configuration/MailConfiguration.php
Normal file
@@ -0,0 +1,24 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Configuration;
|
||||
|
||||
class MailConfiguration
|
||||
{
|
||||
public function getFromAddress(): ?string
|
||||
{
|
||||
$from = getenv('MAILER_FROM');
|
||||
|
||||
if ($from === false || empty($from)) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return $from;
|
||||
}
|
||||
}
|
||||
@@ -214,7 +214,6 @@ class DoctorController extends AbstractController
|
||||
{
|
||||
return [
|
||||
'APP_ENV' => getenv('APP_ENV'),
|
||||
'MAILER_FROM' => getenv('MAILER_FROM'),
|
||||
'CORS_ALLOW_ORIGIN' => getenv('CORS_ALLOW_ORIGIN'),
|
||||
];
|
||||
}
|
||||
|
||||
@@ -16,8 +16,10 @@ use FOS\UserBundle\Model\UserManagerInterface;
|
||||
use Symfony\Component\EventDispatcher\EventSubscriberInterface;
|
||||
|
||||
/**
|
||||
* This class intercepts the registration to make sure the first-ever
|
||||
* registered user will get the SUPER_ADMIN role.
|
||||
* This class intercepts the registration to make sure:
|
||||
*
|
||||
* - the first-ever registered user will get the SUPER_ADMIN role
|
||||
* - the user uses the current request locale as initial language setting
|
||||
*/
|
||||
class RegistrationSubscriber implements EventSubscriberInterface
|
||||
{
|
||||
@@ -49,7 +51,7 @@ class RegistrationSubscriber implements EventSubscriberInterface
|
||||
*/
|
||||
public function onRegistrationSuccess(FormEvent $event)
|
||||
{
|
||||
/** @var \FOS\UserBundle\Model\UserInterface $user */
|
||||
/** @var User $user */
|
||||
$user = $event->getForm()->getData();
|
||||
$roles = [User::ROLE_USER];
|
||||
|
||||
@@ -57,6 +59,7 @@ class RegistrationSubscriber implements EventSubscriberInterface
|
||||
$roles = [User::ROLE_SUPER_ADMIN];
|
||||
}
|
||||
|
||||
$user->setLanguage($event->getRequest()->getLocale());
|
||||
$user->setRoles($roles);
|
||||
}
|
||||
}
|
||||
|
||||
43
src/Mail/KimaiMailer.php
Normal file
43
src/Mail/KimaiMailer.php
Normal file
@@ -0,0 +1,43 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Configuration\MailConfiguration;
|
||||
use Symfony\Component\Mailer\Envelope;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use Symfony\Component\Mime\Email;
|
||||
use Symfony\Component\Mime\RawMessage;
|
||||
|
||||
final class KimaiMailer implements MailerInterface
|
||||
{
|
||||
/**
|
||||
* @var MailerInterface
|
||||
*/
|
||||
private $mailer;
|
||||
/**
|
||||
* @var MailConfiguration
|
||||
*/
|
||||
private $configuration;
|
||||
|
||||
public function __construct(MailConfiguration $configuration, MailerInterface $mailer)
|
||||
{
|
||||
$this->configuration = $configuration;
|
||||
$this->mailer = $mailer;
|
||||
}
|
||||
|
||||
public function send(RawMessage $message, Envelope $envelope = null): void
|
||||
{
|
||||
if ($message instanceof Email) {
|
||||
$message->from($this->configuration->getFromAddress());
|
||||
}
|
||||
|
||||
$this->mailer->send($message);
|
||||
}
|
||||
}
|
||||
97
src/Mail/UserMails.php
Normal file
97
src/Mail/UserMails.php
Normal file
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
/*
|
||||
* This file is part of the Kimai time-tracking app.
|
||||
*
|
||||
* For the full copyright and license information, please view the LICENSE
|
||||
* file that was distributed with this source code.
|
||||
*/
|
||||
|
||||
namespace App\Mail;
|
||||
|
||||
use App\Entity\User;
|
||||
use FOS\UserBundle\Mailer\MailerInterface as FOSMailerInterface;
|
||||
use FOS\UserBundle\Model\UserInterface;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
final class UserMails implements FOSMailerInterface
|
||||
{
|
||||
/**
|
||||
* @var KimaiMailer
|
||||
*/
|
||||
private $mailer;
|
||||
/**
|
||||
* @var UrlGeneratorInterface
|
||||
*/
|
||||
private $router;
|
||||
/**
|
||||
* @var TranslatorInterface
|
||||
*/
|
||||
private $translator;
|
||||
|
||||
public function __construct(KimaiMailer $mailer, UrlGeneratorInterface $router, TranslatorInterface $translator)
|
||||
{
|
||||
$this->mailer = $mailer;
|
||||
$this->router = $router;
|
||||
$this->translator = $translator;
|
||||
}
|
||||
|
||||
public function sendConfirmationEmailMessage(UserInterface $user)
|
||||
{
|
||||
$username = $user->getUsername();
|
||||
$language = 'en';
|
||||
|
||||
if ($user instanceof User) {
|
||||
$username = $user->getDisplayName();
|
||||
$language = $user->getLanguage();
|
||||
}
|
||||
|
||||
$url = $this->router->generate('fos_user_registration_confirm', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
|
||||
$email = (new TemplatedEmail())
|
||||
->to(new Address($user->getEmail()))
|
||||
->subject(
|
||||
$this->translator->trans('registration.subject', ['%username%' => $username], 'email', $language)
|
||||
)
|
||||
->htmlTemplate('emails/confirmation.html.twig')
|
||||
->context([
|
||||
'user' => $user,
|
||||
'username' => $username,
|
||||
'confirmationUrl' => $url,
|
||||
])
|
||||
;
|
||||
|
||||
$this->mailer->send($email);
|
||||
}
|
||||
|
||||
public function sendResettingEmailMessage(UserInterface $user)
|
||||
{
|
||||
$username = $user->getUsername();
|
||||
$language = 'en';
|
||||
|
||||
if ($user instanceof User) {
|
||||
$username = $user->getDisplayName();
|
||||
$language = $user->getLanguage();
|
||||
}
|
||||
|
||||
$url = $this->router->generate('fos_user_resetting_reset', ['token' => $user->getConfirmationToken()], UrlGeneratorInterface::ABSOLUTE_URL);
|
||||
|
||||
$email = (new TemplatedEmail())
|
||||
->to(new Address($user->getEmail()))
|
||||
->subject(
|
||||
$this->translator->trans('reset.subject', ['%username%' => $username], 'email', $language)
|
||||
)
|
||||
->htmlTemplate('emails/password-reset.html.twig')
|
||||
->context([
|
||||
'user' => $user,
|
||||
'username' => $username,
|
||||
'confirmationUrl' => $url,
|
||||
])
|
||||
;
|
||||
|
||||
$this->mailer->send($email);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user