src/Services/MailerService.php line 42

Open in your IDE?
  1. <?php
  2. /*
  3. * ==============================================================
  4. *     Autor            :  Farid Benjomaa
  5. *     Modified by        :  
  6. *    COPYRIGHT (C) 2025, Media-Technologies
  7. * ==============================================================
  8. */
  9. namespace App\Services;
  10. use Symfony\Component\Mailer\MailerInterface;
  11. use Symfony\Component\Mime\Email;
  12. use Twig\Environment;
  13. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  14. use Psr\Log\LoggerInterface;
  15. class MailerService
  16. {
  17.     /**
  18.      * @var MailerInterface
  19.      */
  20.     private $mailer;
  21.     /**
  22.      * @var Environment
  23.      */
  24.     private $twig;
  25.     private $parameters;
  26.     /**
  27.      * MailerService constructeur
  28.      * 
  29.      * @param MaillerInterface $mailer
  30.      * @param Environement $twig
  31.      */
  32.     public function __construct(MailerInterface $mailerEnvironment $twigParameterBagInterface $paramsLoggerInterface $logger)
  33.     {
  34.         $this->mailer $mailer;
  35.         $this->twig $twig;
  36.         $this->parameters $params;
  37.         $this->logger $logger;
  38.     }
  39.     /**
  40.      * 
  41.      */
  42.     public function send(string $subjectstring $from nullstring $tostring $template, array $parameters)
  43.     {
  44.         $this->logger->info('++++++++ SENDING MAIL ++++++++');
  45.         $this->logger->info('FROM:' $this->parameters->get('mailFrom'));
  46.         // Remove possible injections :
  47.         $to preg_replace("/([^a-zA-Z0-9@._-]+)/","",$to);
  48.         $this->logger->info('TO:' $to);
  49.         $this->logger->info('SUBJECT:' utf8_encode($subject));
  50.         $this->logger->info('TEMPLATE: ' $template);
  51.         $parametersForLog array_map('utf8_encode'$parameters);
  52.         $this->logger->info('PARAMETERS: ' json_encode($parametersForLog));
  53.         $email = (new Email())
  54.             //->from($from)
  55.             ->from($this->parameters->get('mailFrom')) //@todo to clean
  56.             ->to($to)
  57.             ->subject($subject)
  58.             ->html(
  59.                 $this->twig->render($template$parameters),
  60.                 charset'text/html'
  61.             );
  62.        
  63.         $this->mailer->send($email);
  64.         $this->logger->info('++++++++ END SENDING MAIL ++++++++');
  65.     }
  66. }