Deprecated: Constant E_STRICT is deprecated in /var/www/PixelForce/vendor/symfony/error-handler/ErrorHandler.php on line 58

Deprecated: Constant E_STRICT is deprecated in /var/www/PixelForce/vendor/symfony/error-handler/ErrorHandler.php on line 76
Symfony Profiler

src/Services/MailerService.php line 47

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\DevisCompany;
  4. use Twig\Environment as Twig_Environment;
  5. use App\Entity\DocumentRecipient;
  6. use App\Entity\Formation;
  7. use App\Entity\Order;
  8. use App\Entity\OrderAroma;
  9. use App\Entity\OrderDigital;
  10. use App\Entity\OrderPack;
  11. use App\Entity\OrderSecu;
  12. use App\Entity\User;
  13. use App\Repository\UserRepository;
  14. use Nucleos\DompdfBundle\Wrapper\DompdfWrapperInterface;
  15. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  16. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  17. use Symfony\Component\Mailer\Exception\TransportExceptionInterface;
  18. use Symfony\Component\Mailer\MailerInterface;
  19. use Symfony\Component\Mime\Address;
  20. use Symfony\Component\Mime\Email;
  21. class MailerService
  22. {
  23.     /**
  24.      * @var Mailer
  25.      */
  26.     private $mailer;
  27.     private $from;
  28.     private $from_name;
  29.     private $parameterBag;
  30.     private $twig;
  31.     private $wrapper;
  32.     private $fileHandlser;
  33.     public function __construct(private UserRepository $userRepoMailerInterface $mailerParameterBagInterface $parameterBagTwig_Environment $twigDompdfWrapperInterface $wrapperFileHandler $fileHandler)
  34.     {
  35.         $this->mailer $mailer;
  36.         $this->from =  $_ENV['MAILER_SEND_FROM'];
  37.         $this->from_name $_ENV['MAILER_SEND_FROM_NAME'];
  38.         $this->parameterBag $parameterBag;
  39.         $this->twig $twig;
  40.         $this->wrapper $wrapper;
  41.         $this->fileHandler $fileHandler;
  42.     }
  43.     public function sendMailInscriptionUser($email)
  44.     {
  45.         $this->sendMail([
  46.             'subject' => 'Code de vérification',
  47.             'to' => [
  48.                 $email
  49.             ],
  50.             'template' => 'inscription/lien_page_inscription.html.twig',
  51.             'template_vars' => [
  52.                 'encodedMail' => base64_encode($email),
  53.             ]
  54.         ]);
  55.     }
  56.     public function sendMailRegenerationCode(User $user)
  57.     {
  58.         $this->sendMail([
  59.             'subject' => 'Code de vérification',
  60.             'to' => [
  61.                 $user->getEmail()
  62.             ],
  63.             'template' => 'security/sixDigitKey.html.twig',
  64.             'template_vars' => [
  65.                 'sixDigitKey' => $user->getSixDigitCode()
  66.             ]
  67.         ]);
  68.     }
  69.     public function sendMailAfterDoneFormation(User $senderUser $recipientFormation $formation)
  70.     {
  71.         $this->sendMail([
  72.             'subject' => 'Formation terminée',
  73.             //'from' => $sender->getEmail(),
  74.             //'from_name' => $sender->getNom(),
  75.             'to' => [
  76.                 $recipient->getEmail()
  77.             ],
  78.             'template' => 'formation/formation_done.html.twig',
  79.             'template_vars' => [
  80.                 'formation' => $formation,
  81.                 'agent' => $sender
  82.             ]
  83.         ]);
  84.     }
  85.     public function sendVerifCodeToClient($recipient$code$dateExpiration)
  86.     {
  87.         $this->sendMail([
  88.             'subject' => 'Validation du nouveau compte',
  89.             'to' => [
  90.                 $recipient->getEmail()
  91.             ],
  92.             'template' => 'inscription/code_verification_client.html.twig',
  93.             'template_vars' => [
  94.                 'code' => $code,
  95.                 'dateExpiration' => $dateExpiration
  96.             ]
  97.         ]);       
  98.     }
  99.     public function SendDevisToCompany($recipient$devisCompany$pj_pathname)
  100.     {
  101.         $email = (new TemplatedEmail())
  102.         ->from(new Address($this->from$this->from_name))
  103.         ->to($recipient)
  104.         ->subject('Devis')
  105.         ->htmlTemplate('emails/devis/devis_entreprise.html.twig')
  106.         ->context([
  107.             'devisCompany' => $devisCompany
  108.         ])
  109.         ->attachFromPath($this->parameterBag->get('kernel.project_dir')."/public/files/".$pj_pathnamenull);
  110.         $this->mailer->send($email);
  111.     }
  112.     public function sendDocument(DocumentRecipient $rec$link$recipient)
  113.     {
  114.         $email = (new TemplatedEmail())
  115.             ->from(new Address($this->from$this->from_name))
  116.             ->to($recipient->getEmail())
  117.             ->subject("Signature du document << ".$rec->getDocument()->getNom()." >>")
  118.             ->htmlTemplate('emails/document.html.twig')
  119.             ->context([
  120.                 'link' => $link,
  121.                 'conseiller' => $rec->getConseiller()
  122.             ])
  123.             ->embedFromPath($this->parameterBag->get('kernel.project_dir').'/public/assets/img/securitas.png''logoSecuritas''image/png');
  124.         $this->mailer->send($email);
  125.     }
  126.     public function sendMail($parameters)
  127.     {
  128.         $email = (new TemplatedEmail())
  129.             //->from(new Address($parameters['from'], isset($parameters['from_name']) ? $parameters['from_name'] : ''))
  130.             ->from(new Address($this->from$this->from_name))
  131.             ->subject($parameters['subject'])
  132.         ;
  133.         // email des recepteurs
  134.         foreach($parameters['to'] as $to) {
  135.            $email $email->addTo($to);
  136.         }
  137.         // eamil en copie
  138.         if(isset($parameters['cc'])){
  139.             foreach($parameters['cc'] as $cc) {
  140.                 $email $email->addcc($cc);
  141.             }
  142.         }
  143.         // template
  144.         $email $email->htmlTemplate('emails/'.$parameters['template']);
  145.         // passez les variables
  146.         $email $email->context(isset($parameters['template_vars']) ? $parameters['template_vars'] : []);
  147.         // piece_jointe
  148.         // $email = $email->attachFromPath(  $this->parameterBag->get('kernel.project_dir')."/public/files/piece-jointe/livret_d_accueil_formation_officielle_entreprendre_en_2022.pdf", null)  
  149.         //try{
  150.             $this->mailer->send($email);
  151.         //} catch (TransportExceptionInterface $e) {
  152.             // some error prevented the email sending; display an
  153.             // error message or try to resend the message
  154.         //     echo 'erreur : '.$e->getMessage();
  155.         //}
  156.     }
  157.     //    private $exempleParametre = [
  158. //        'from' => 'xx@gmail.com',
  159. //        'from_name' => 'nomena',
  160. //        'to' => [
  161. //            'qdsf@dsf',
  162. //            'qdsk@qsdf'
  163. //        ],
  164. //        'cc' => [
  165. //            'qsmldkfj@qdsf'
  166. //        ],
  167. //        'template' => 'qkdjfmdsjf.html.twig',
  168. //        'template_vars' => [
  169. //            'variable1' => 'valeur'
  170. //        ]
  171. //
  172. //    ];
  173.     public function mySendMail(array $mail, array $attachmentsPath = [], ?string $to null, array $embeddedImages = [])
  174.     {
  175.         $email = (new Email())
  176.             ->from(new Address($this->from$this->from_name))
  177.             ->subject($mail['subject'])
  178.             ->html($mail['body']);
  179.         $to $to === null $mail['to'] : $to;    
  180.         if(gettype($to) == "array"){
  181.             $email $email->to(...$to);
  182.         } else {
  183.             $email $email->to($to);
  184.         }
  185.         foreach($attachmentsPath as $path){
  186.             $email->attachFromPath($this->parameterBag->get('kernel.project_dir')."/public/files/".$path);
  187.         }
  188.         foreach($embeddedImages as $key => $value){
  189.             $email->embedFromPath($this->parameterBag->get('kernel.project_dir')."/public/".$value$key);
  190.         }
  191.         $this->mailer->send($email);
  192.     }
  193.     public function renderTwig($filePath$options = []){
  194.         return $this->twig->render($filePath$options);
  195.     }
  196.     public function sendFactureProduit(Order $order){
  197.         
  198.         $body $this->renderTwig('emails/commande.html.twig', [
  199.             'nomClient' => $order->getAddress()->getNom(),
  200.             'prenomClient' => $order->getAddress()->getPrenom(),
  201.             'order' => $order
  202.         ]);
  203.         $attachmentsPath = [$order->getInvoicePath()];
  204.         $embeddedImages = ['logo' => 'assets/img/logo/greenlife/greenlife.png'];
  205.         $this->mySendMail([
  206.             'subject' => 'Confirmation de commande '.$order->getId(),
  207.             'to' => $order->getAddress()->getEmail(),
  208.             'body' => $body
  209.         ], $attachmentsPathnull$embeddedImages);
  210.         $bodyMailToAdmin $this->renderTwig('emails/commande_details.html.twig', [
  211.             'order' => $order
  212.         ]);
  213.         $MailToAdmin = [
  214.             'body' => $bodyMailToAdmin,
  215.             'subject' => "Commande coffret  n°{$order->getId()}",
  216.             'to' => $order->getAgent()->getEmail()
  217.         ];
  218.         $this->mySendMail($MailToAdmin$attachmentsPathnull$embeddedImages);
  219.     }
  220.     public function sendFactureSecu(OrderSecu $order){
  221.         
  222.         $body $this->renderTwig('emails/commande_secu.html.twig', [
  223.             'nomClient' => $order->getClient()->getNom(),
  224.             'prenomClient' => $order->getClient()->getPrenom(),
  225.             'order' => $order
  226.         ]);
  227.         $attachmentsPath = [$order->getInvoicePath(), $order->getContratSigned()];
  228.         $embeddedImages = ['logo' => 'assets/img/logo/greenlife/greenlife.png'];
  229.         $this->mySendMail([
  230.             'subject' => 'Confirmation de commande '.$order->getId(),
  231.             'to' => $order->getClient()->getEmail(),
  232.             'body' => $body
  233.         ], $attachmentsPathnull$embeddedImages);
  234.         $bodyMailToAdmin $this->renderTwig('emails/commande_details_secu.html.twig', [
  235.             'order' => $order
  236.         ]);
  237.         $MailToAdmin = [
  238.             'body' => $bodyMailToAdmin,
  239.             'subject' => "Commande n°{$order->getId()}",
  240.             'to' => $order->getAgent()->getEmail()
  241.         ];
  242.         $this->mySendMail($MailToAdmin$attachmentsPathnull$embeddedImages);
  243.     }
  244.     public function sendFactureAroma(OrderAroma $order){
  245.         
  246.         $body $this->renderTwig('emails/commande_aroma.html.twig', [
  247.             'nomClient' => $order->getAddress()->getLastname(),
  248.             'prenomClient' => $order->getAddress()->getFirstname(),
  249.             'order' => $order
  250.         ]);
  251.         $attachmentsPath = [$order->getInvoicePath()];
  252.         $embeddedImages = ['logo' => 'assets/img/home/af_logo_d2-min.png'];
  253.         $this->mySendMail([
  254.             'subject' => 'Confirmation de commande '.$order->getId(),
  255.             'to' => $order->getAddress()->getMail(),
  256.             'body' => $body
  257.         ], $attachmentsPathnull$embeddedImages);
  258.         $bodyMailToAdmin $this->renderTwig('emails/commande_details_aroma.html.twig', [
  259.             'order' => $order
  260.         ]);
  261.         $MailToAdmin = [
  262.             'body' => $bodyMailToAdmin,
  263.             'subject' => "Commande n°{$order->getId()}",
  264.             'to' => $order->getAgent()->getEmail()
  265.         ];
  266.         $this->mySendMail($MailToAdmin$attachmentsPathnull$embeddedImages);
  267.     }
  268.     public function sendDeliveryOrder(OrderAroma $order){
  269.         $body $this->renderTwig('emails/livraison_aroma.html.twig', [
  270.             'nomClient' => $order->getAddress()->getLastname(),
  271.             'prenomClient' => $order->getAddress()->getFirstname(),
  272.             'order' => $order
  273.         ]);
  274.         $attachmentsPath = [$order->getDeliveryOrderPath()];
  275.         $embeddedImages = ['logo' => 'assets/img/home/af_logo_d2-min.png'];
  276.         $this->mySendMail([
  277.             'subject' => 'Livraison de commande '.$order->getId(),
  278.             'to' => $order->getAddress()->getMail(),
  279.             'body' => $body
  280.         ], $attachmentsPathnull$embeddedImages);
  281.     }
  282.     public function sendFactureDevisCompanyDigital(DevisCompany $devisCompany){
  283.         
  284.         $body $this->renderTwig('emails/commande_devis_company_digital.html.twig', [
  285.             'prenomClient' => $devisCompany->getClientLastname(),
  286.             'devisCompany' => $devisCompany
  287.         ]);
  288.         $attachmentsPath = [$devisCompany->getPjFilename()];
  289.         $embeddedImages = ['logo' => 'assets/img/logo/greenlife/greenlife.png'];
  290.         $this->mySendMail([
  291.             'subject' => 'Confirmation du devis '.$devisCompany->getId(),
  292.             'to' => $devisCompany->getClientMail(),
  293.             'body' => $body
  294.         ], $attachmentsPathnull$embeddedImages);
  295.         $bodyMailToAdmin $this->renderTwig('emails/commande_details_devis_company_digital.html.twig', [
  296.             'devisCompany' => $devisCompany
  297.         ]);
  298.         $MailToAdmin = [
  299.             'body' => $bodyMailToAdmin,
  300.             'subject' => "Devis n°{$devisCompany->getId()}",
  301.             'to' => $devisCompany->getAgent()->getEmail()
  302.         ];
  303.         $this->mySendMail($MailToAdmin$attachmentsPathnull$embeddedImages);
  304.     }
  305.     public function sendFactureOrderDigital(OrderDigital $order){
  306.         
  307.         $body $this->renderTwig('emails/commande_digital.html.twig', [
  308.             'prenomClient' => $order->getDevis()->getDemandeDevis()->getPrenom(),
  309.             'order' => $order
  310.         ]);
  311.         $attachmentsPath = [$order->getInvoicePath(), $order->getDevis()->getContratFileName()];
  312.         $embeddedImages = ['logo' => 'assets/img/logo/greenlife/greenlife.png'];
  313.         $this->mySendMail([
  314.             'subject' => 'Confirmation de la commande '.$order->getId(),
  315.             'to' => $order->getDevis()->getDemandeDevis()->getEmail(),
  316.             'body' => $body
  317.         ], $attachmentsPathnull$embeddedImages);
  318.         $bodyMailToAdmin $this->renderTwig('emails/commande_details_digital.html.twig', [
  319.             'order' => $order
  320.         ]);
  321.         $MailToAdmin = [
  322.             'body' => $bodyMailToAdmin,
  323.             'subject' => "Commande n°{$order->getId()}",
  324.             'to' => $order->getAgent()->getEmail()
  325.         ];
  326.         $this->mySendMail($MailToAdmin$attachmentsPathnull$embeddedImages);
  327.     }
  328.     public function sendFactureOrderPack(OrderPack $order){
  329.         
  330.         $body $this->renderTwig('emails/commande_pack.html.twig', [
  331.             'prenomClient' => $order->getAgent()->getPrenom(),
  332.             'order' => $order
  333.         ]);
  334.         $attachmentsPath = [$order->getInvoicePath()];
  335.         $embeddedImages = ['logo' => 'assets/img/logo/greenlife/greenlife.png'];
  336.         $this->mySendMail([
  337.             'subject' => 'Confirmation de la commande '.$order->getId(),
  338.             'to' => $order->getAgent()->getEmail(),
  339.             'body' => $body
  340.         ], $attachmentsPathnull$embeddedImages);
  341.         
  342.         $admins $this->userRepo->findAllAdmin();
  343.         $emailMap = function (User $user){
  344.             return $user->getEmail();
  345.         };
  346.         $adminMailList array_map($emailMap$admins);
  347.         // $adminMails = join(",", $adminMailList);
  348.         $bodyMailToAdmin $this->renderTwig('emails/commande_details_pack.html.twig', [
  349.             'prenomClient' => $order->getAgent()->getPrenom(),
  350.             'order' => $order
  351.         ]);
  352.         $MailToAdmin = [
  353.             'body' => $bodyMailToAdmin,
  354.             'subject' => "Commande n°{$order->getId()}",
  355.             'to' => $adminMailList // We don*t know Admin Email list
  356.         ];
  357.         $this->mySendMail($MailToAdmin$attachmentsPathnull$embeddedImages);
  358.     }
  359. }