<?php
/*
* ==============================================================
* Autor : Farid Benjomaa
* Modified by :
* COPYRIGHT (C) 2025, Media-Technologies
* ==============================================================
*/
namespace App\Services;
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Email;
use Twig\Environment;
use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
use Psr\Log\LoggerInterface;
class MailerService
{
/**
* @var MailerInterface
*/
private $mailer;
/**
* @var Environment
*/
private $twig;
private $parameters;
/**
* MailerService constructeur
*
* @param MaillerInterface $mailer
* @param Environement $twig
*/
public function __construct(MailerInterface $mailer, Environment $twig, ParameterBagInterface $params, LoggerInterface $logger)
{
$this->mailer = $mailer;
$this->twig = $twig;
$this->parameters = $params;
$this->logger = $logger;
}
/**
*
*/
public function send(string $subject, string $from = null, string $to, string $template, array $parameters)
{
$this->logger->info('++++++++ SENDING MAIL ++++++++');
$this->logger->info('FROM:' . $this->parameters->get('mailFrom'));
// Remove possible injections :
$to = preg_replace("/([^a-zA-Z0-9@._-]+)/","",$to);
$this->logger->info('TO:' . $to);
$this->logger->info('SUBJECT:' . utf8_encode($subject));
$this->logger->info('TEMPLATE: ' . $template);
$parametersForLog = array_map('utf8_encode', $parameters);
$this->logger->info('PARAMETERS: ' . json_encode($parametersForLog));
$email = (new Email())
//->from($from)
->from($this->parameters->get('mailFrom')) //@todo to clean
->to($to)
->subject($subject)
->html(
$this->twig->render($template, $parameters),
charset: 'text/html'
);
$this->mailer->send($email);
$this->logger->info('++++++++ END SENDING MAIL ++++++++');
}
}