WIP: Add email notifications
This commit is contained in:
@@ -0,0 +1,97 @@
|
||||
<?php
|
||||
|
||||
namespace App\Email;
|
||||
|
||||
use App\Model\EmailAttachmentDto;
|
||||
use Psr\Log\LoggerInterface;
|
||||
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
|
||||
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
|
||||
use Symfony\Component\Mailer\MailerInterface;
|
||||
use Symfony\Component\Mime\Address;
|
||||
use Symfony\Component\OptionsResolver\OptionsResolver;
|
||||
use Symfony\Contracts\Translation\TranslatorInterface;
|
||||
|
||||
class Mailer
|
||||
{
|
||||
public function __construct(
|
||||
private readonly MailerInterface $mailer,
|
||||
private readonly TranslatorInterface $translator,
|
||||
private readonly array $defaults,
|
||||
private readonly LoggerInterface $logger
|
||||
) {
|
||||
}
|
||||
|
||||
public function createAndSendEmail(array $context, array $options): void
|
||||
{
|
||||
$config = $this->resolveConfig($options);
|
||||
$email = $this->create($context, $config);
|
||||
|
||||
$recipients = (array) $config['to'];
|
||||
|
||||
foreach ($recipients as $recipient) {
|
||||
$email->to($recipient);
|
||||
$this->send($email);
|
||||
}
|
||||
}
|
||||
|
||||
public function create(array $context, array $config): TemplatedEmail
|
||||
{
|
||||
$email = (new TemplatedEmail())
|
||||
->from($config['from'])
|
||||
->subject($this->translator->trans($config['subject'], $config['subject_parameters']))
|
||||
->htmlTemplate($config['template'])
|
||||
->context($context)
|
||||
;
|
||||
|
||||
foreach ($config['attachments'] as $attachment) {
|
||||
/* @var EmailAttachmentDto $attachment */
|
||||
$email->attach($attachment->getContent(), $attachment->getName(), $attachment->getMimeType());
|
||||
}
|
||||
|
||||
return $email;
|
||||
}
|
||||
|
||||
public function send(TemplatedEmail $email): void
|
||||
{
|
||||
$recipients = array_map(fn (Address $address) => $address->toString(), $email->getTo());
|
||||
|
||||
try {
|
||||
$this->mailer->send($email);
|
||||
$this->logger->info('Send email', [
|
||||
'to' => $recipients,
|
||||
'subject' => $email->getSubject(),
|
||||
]);
|
||||
} catch (TransportExceptionInterface $e) {
|
||||
$this->logger->error('Email could not be sent', [
|
||||
'to' => $recipients,
|
||||
'subject' => $email->getSubject(),
|
||||
'error' => $e->getMessage(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
private function resolveConfig(array $options): array
|
||||
{
|
||||
$resolver = new OptionsResolver();
|
||||
|
||||
$resolver
|
||||
->setDefaults([
|
||||
'from' => $this->defaults['from'],
|
||||
'to' => $this->defaults['to'],
|
||||
'subject_parameters' => [],
|
||||
'attachments' => [],
|
||||
])
|
||||
->setRequired([
|
||||
'template',
|
||||
'subject',
|
||||
])
|
||||
->setAllowedTypes('to', ['string', 'array'])
|
||||
->setAllowedTypes('template', 'string')
|
||||
->setAllowedTypes('subject', 'string')
|
||||
->setAllowedTypes('subject_parameters', 'array')
|
||||
->setAllowedTypes('attachments', 'array')
|
||||
;
|
||||
|
||||
return $resolver->resolve($options);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
<?php
|
||||
|
||||
namespace App\Email;
|
||||
|
||||
use App\Entity\User;
|
||||
use Psr\Log\LoggerInterface;
|
||||
|
||||
class WelcomeEmailMailer
|
||||
{
|
||||
public function __construct(private readonly Mailer $mailer, private readonly LoggerInterface $logger)
|
||||
{}
|
||||
|
||||
public function send(User $user): void
|
||||
{
|
||||
$this->mailer->createAndSendEmail([
|
||||
'user' => $user,
|
||||
], [
|
||||
'to' => $user->getEmail(),
|
||||
'subject' => 'Willkommen bei MyE&P Team',
|
||||
'template' => 'email/welcome.html.twig',
|
||||
]);
|
||||
|
||||
$this->logger->info('Send welcome email', [
|
||||
'user_id' => $user->getId(),
|
||||
'user_email' => $user->getEmail(),
|
||||
]);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,25 @@
|
||||
<?php
|
||||
|
||||
namespace App\Model;
|
||||
|
||||
class EmailAttachmentDto
|
||||
{
|
||||
public function __construct(private string $name, private string $content, private string $mimeType)
|
||||
{
|
||||
}
|
||||
|
||||
public function getName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
public function getContent(): string
|
||||
{
|
||||
return $this->content;
|
||||
}
|
||||
|
||||
public function getMimeType(): string
|
||||
{
|
||||
return $this->mimeType;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user