replaced mailer library, added email templates (#1462)
This commit is contained in:
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