Files
e-cosplay/src/Service/Mailer/Mailer.php
Serreau Jovann e76650d5f1 ```
 feat(newsletter): Ajoute l'inscription et la désinscription à la newsletter
```
2026-01-11 13:26:10 +01:00

117 lines
3.5 KiB
PHP

<?php
namespace App\Service\Mailer;
use App\Entity\Mail;
use Doctrine\ORM\EntityManagerInterface;
use Exception;
use Symfony\Component\HttpKernel\Profiler\Profiler;
use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mailer\Transport\Smtp\EsmtpTransport;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\IdentificationHeader;
use Symfony\Component\Process\Exception\ProcessFailedException;
use Symfony\Component\Process\Process;
use Symfony\Component\Routing\Generator\UrlGeneratorInterface;
use Twig\Environment;
class Mailer
{
private readonly MailerInterface $mailer;
public function __construct(
MailerInterface $mailer,
private readonly EntityManagerInterface $entityManager,
private readonly UrlGeneratorInterface $urlGenerator,
private readonly ?Profiler $profiler,
private readonly Environment $environment,
) {
$this->mailer = $mailer;
}
private function convertMjmlToHtml(string $mjmlContent): string
{
$process = new Process(['mjml', '--stdin']);
$process->setInput($mjmlContent);
try {
$process->run();
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
} catch (ProcessFailedException|Exception) {
return ''; // Retourne vide en cas d'échec
}
}
/**
* @param string $address
* @param string $addressName
* @param string $subject
* @param string $template
* @param array<string, mixed> $data
* @param array<\Symfony\Component\Mime\Part\DataPart> $files
*/
public function send(
string $address,
string $addressName,
string $subject,
string $template,
array $data,
array $files = [],
bool $isNewsletter = false,
): void {
$dest = new Address($address, $addressName);
$src = new Address("contact@e-cosplay.fr", "E-Cosplay");
$mail = (new Email())
->subject($subject)
->to($dest)
->from($src);
$header = $mail->getHeaders();
if($isNewsletter) {
$unsubscribeUrl = $this->urlGenerator->generate('app_newsletter_validate_remove',['email'=>$address],UrlGeneratorInterface::ABSOLUTE_URL);
$unsubscribeEmail = "mailto:contact@e-cosplay.fr?subject=unsubscribe";
// Ajout de l'en-tête standard
$header->addHeader('List-Unsubscribe', "<$unsubscribeEmail>, <$unsubscribeUrl>");
$header->addHeader('List-Unsubscribe-Post', 'List-Unsubscribe=One-Click');
$data['unsubscribeUrl'] = $unsubscribeUrl;
}
$mail->setHeaders($header);
$mjmlGenerator = $this->environment->render($template, [
'system' => [
'subject' => $subject,
'path' => $_ENV['PATH_URL'],
],
'datas' => $data,
]);
$htmlContent = $this->convertMjmlToHtml($mjmlGenerator);
$txtContent = $this->environment->render('txt-'.$template,[
'system' => [
'subject' => $subject,
'path' => $_ENV['PATH_URL'],
],
'datas' => $data,
]);
foreach ($files as $file) {
$mail->addPart($file);
}
$mail->html($htmlContent);
$mail->text($txtContent);
$this->mailer->send($mail);
}
}