src/Services/MailerService.php line 30

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.     /**
  20.      * MailerService constructeur
  21.      * 
  22.      * @param MaillerInterface $mailer
  23.      * @param Environement $twig
  24.      */
  25.     public function __construct(MailerInterface $mailerEnvironment $twigParameterBagInterface $paramsLoggerInterface $logger)
  26.     {
  27.         $this->mailer $mailer;
  28.         $this->twig $twig;
  29.         $this->parameters $params;
  30.         $this->logger $logger;
  31.     }
  32.     /**
  33.      * 
  34.      */
  35.     public function send(string $subjectstring $from nullstring $tostring $template, array $parameters)
  36.     {
  37.         $this->logger->info('++++++++ SENDING MAIL ++++++++');
  38.         $this->logger->info('FROM:' $this->parameters->get('mailFrom'));
  39.         $this->logger->info('TO:' $to);
  40.         // Remove possible injections :             
  41.         $to preg_replace("/([^a-zA-Z0-9@._-]+)/","",$to);
  42.         $this->logger->info('TO (safe):' $to);
  43.         $this->logger->info('SUBJECT:' htmlentities($subject));
  44.         $this->logger->info('TEMPLATE: ' $template);
  45.         $parametersForLog array_map('utf8_encode'$parameters);
  46.         $this->logger->info('PARAMETERS: ' json_encode($parametersForLog));
  47.         $email = (new Email())
  48.             //->from($from)
  49.             ->from($this->parameters->get('mailFrom')) //@todo to clean
  50.             ->to($to)
  51.             ->subject($subject)
  52.             ->html(
  53.                 $this->twig->render($template$parameters),
  54.                 charset'text/html'
  55.             );
  56.        
  57.         $this->mailer->send($email);
  58.         $this->logger->info('++++++++ END SENDING MAIL ++++++++');
  59.     }
  60. }