src/Services/MailerService.php line 64

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