src/Services/Mail.php line 88

Open in your IDE?
  1. <?php
  2. namespace App\Services;
  3. use App\Entity\User;
  4. use Swift_Attachment;
  5. use Twig\Environment;
  6. use App\Entity\Command;
  7. use App\Entity\Company;
  8. use App\Entity\Location;
  9. use App\Model\ContactDTO;
  10. use App\Entity\Abonnement;
  11. use PhpParser\Builder\Param;
  12. use App\Entity\ContactProduct;
  13. use Symfony\Component\Mime\Email;
  14. use Symfony\Bundle\MakerBundle\Str;
  15. use Symfony\Component\Mime\Address;
  16. use App\Repository\CautionRepository;
  17. use App\Repository\CommandRepository;
  18. use Doctrine\ORM\EntityManagerInterface;
  19. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  20. use Symfony\Component\Mailer\MailerInterface;
  21. use Symfony\Component\DependencyInjection\EnvVarLoaderInterface;
  22. use Symfony\Component\Translation\Exception\NotFoundResourceException;
  23. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  24. use Symfony\Component\Mime\Crypto\DkimSigner;
  25. class Mail
  26. {
  27.     /**
  28.      * @var \Swift_Mailer
  29.      */
  30.     private $swift_Mailer;
  31.     /**
  32.      * @var Environment
  33.      */
  34.     private $environment;
  35.     private $commandrepo;
  36.     private $em;
  37.     /**
  38.      * @var CautionRepository
  39.      */
  40.     private $caution;
  41.     // private $domainName;
  42.     private $parameterBag;
  43.     /**
  44.      * @var MailerInterface
  45.      */
  46.     private $mailer;
  47.     public function __construct(
  48.         \Swift_Mailer $swift_Mailer,
  49.         Environment $environment,
  50.         CommandRepository $commandrepo,
  51.         EntityManagerInterface $em,
  52.         CautionRepository $caution,
  53.         // EnvVarLoaderInterface $domainName
  54.         ParameterBagInterface $parameterBag,
  55.         MailerInterface $mailer
  56.     ) {
  57.         $this->swift_Mailer = $swift_Mailer;
  58.         $this->environment = $environment;
  59.         $this->commandrepo = $commandrepo;
  60.         $this->em = $em;
  61.         $this->caution = $caution;
  62.         // $this->domainName = $domainName;
  63.         $this->parameterBag = $parameterBag;
  64.         $this->mailer = $mailer;
  65.     }
  66.     public function contact(ContactDTO $contactDTO)
  67.     {
  68.         $message = (new \Swift_Message('Contact ' . ucfirst($_ENV['PLATEFORM_NAME'])))
  69.             ->setFrom($contactDTO->getEmail())
  70.             ->setTo('contact@providerstravel.fr')
  71.             ->setBody(
  72.                 $this->environment->render(
  73.                     'front/email/contact.html.twig',
  74.                     ['contact' => $contactDTO]
  75.                 ),
  76.                 'text/html'
  77.             );
  78.         $this->swift_Mailer->send($message);
  79.     }
  80.     public function contactProduct(ContactProduct $contactProduct, string $message)
  81.     {
  82.         $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Réponse à votre demande'))
  83.             ->setFrom($_ENV['MAILER_USERNAME'])
  84.             ->setTo($contactProduct->getEmail())
  85.             ->setBody(
  86.                 $this->environment->render(
  87.                     'front/email/contact_product.html.twig',
  88.                     ['contactProduct' => $contactProduct, 'message' => $message]
  89.                 ),
  90.                 'text/html'
  91.             );
  92.         $this->swift_Mailer->send($message);
  93.     }
  94.     public function contactPlatform(Company $company, string $subject, string $message)
  95.     {
  96.         $message = (new \Swift_Message('Contact ' . ucfirst($_ENV['PLATEFORM_NAME'])))
  97.             ->setFrom($company->getEmail())
  98.             ->setTo('contact@providerstravel.fr')
  99.             ->setBody(
  100.                 $this->environment->render(
  101.                     'front/email/contact_platform.html.twig',
  102.                     ['company' => $company, 'subject' => $subject, 'message' => $message]
  103.                 ),
  104.                 'text/html'
  105.             );
  106.         $this->swift_Mailer->send($message);
  107.     }
  108.     public function contactProvider(Company $company, ContactProduct $contactProduct)
  109.     {
  110.         $message = (new \Swift_Message('Contact ' . ucfirst($_ENV['PLATEFORM_NAME'])))
  111.             ->setFrom($_ENV['MAILER_USERNAME'])
  112.             ->setTo($company->getEmail())
  113.             ->setBody(
  114.                 $this->environment->render(
  115.                     'front/email/contact_provider.html.twig',
  116.                     ['company' => $company, 'contactProduct' => $contactProduct]
  117.                 ),
  118.                 'text/html'
  119.             );
  120.         $this->swift_Mailer->send($message);
  121.     }
  122.     public function ActivationCompte(Company $company)
  123.     {
  124.         $message = (new \Swift_Message('Activation compte ' . ucfirst($_ENV['PLATEFORM_NAME'])))
  125.             ->setFrom($_ENV['MAILER_USERNAME'])
  126.             ->setTo($company->getEmail())
  127.             // ->setTo('nyainatester@gmail.com')
  128.         ;
  129.         // $imgActive = $message->embed(\Swift_Image::fromPath('images/mail/activation_img.jpg'));
  130.         $message->setBody(
  131.             $this->environment->render(
  132.                 'front/email/activation_compte.html.twig',
  133.                 ['company' => $company]
  134.             ),
  135.             'text/html'
  136.         );
  137.         $this->swift_Mailer->send($message);
  138.     }
  139.     public function ActivationCompteUser(User $user)
  140.     {
  141.         $domainName = $_ENV['EXECUTION_ENV'] === 'local' ?
  142.             $this->parameterBag->get('kernel.project_dir') . '\public' :
  143.             $_ENV['DOMAIN_NAME'];
  144.         $message = (new \Swift_Message('Activation compte ' . ucfirst($_ENV['PLATEFORM_NAME'])))
  145.             ->setFrom($_ENV['MAILER_USERNAME'])
  146.             ->setTo($user->getEmail())
  147.             ->setBody(
  148.                 $this->environment->render(
  149.                     'front/email/activation_compte_client.html.twig',
  150.                     ['user' => $user]
  151.                 ),
  152.                 'text/html'
  153.             )
  154.             ->attach(
  155.                 \Swift_Attachment::fromPath($domainName . '/images/recap_souscription.pdf')->setFilename('recapitulatif_souscription.pdf')
  156.             );
  157.         $this->swift_Mailer->send($message);
  158.     }
  159.     public function ConfirmationClient(User $user)
  160.     {
  161.         $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - confirmation inscription'))
  162.             ->setFrom($_ENV['MAILER_USERNAME'])
  163.             ->setTo($user->getEmail());
  164.        
  165.         $message->setBody(
  166.             $this->environment->render(
  167.                 'front/email/confirmation_compte_client.html.twig',
  168.                 ['user' => $user],
  169.             ),
  170.             'text/html'
  171.         );
  172.         $this->swift_Mailer->send($message);
  173.     }
  174.     public function ConfirmationNewAdmin(User $user, string $password)
  175.     {
  176.         $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - confirmation inscription'))
  177.             ->setFrom($_ENV['MAILER_USERNAME'])
  178.             ->setTo($user->getEmail())
  179.             ->setBody(
  180.                 $this->environment->render(
  181.                     'front/email/confirmation_new_admin.html.twig',
  182.                     ['user' => $user, 'password' => $password]
  183.                 ),
  184.                 'text/html'
  185.             );
  186.         $this->swift_Mailer->send($message);
  187.     }
  188.     public function ConfirmationPaiementClient(User $user, Command $command, string $path)
  189.     {
  190.         $message = (new \Swift_Message($_ENV['PLATEFORM_NAME'] . ' - confirmation d’achat'))
  191.             ->setFrom($_ENV['MAILER_USERNAME'])
  192.             ->setTo($user->getEmail())
  193.             // ->setTo('nyainatester@gmail.com')
  194.         ;
  195.         // $img1 = $message->embed(\Swift_Image::fromPath('images/img-mail-1.jpeg')) ?? "";
  196.         // $img2 = $message->embed(\Swift_Image::fromPath('images/img-mail-2.jpeg')) ?? "";
  197.         $message->setBody(
  198.             $this->environment->render(
  199.                 'front/email/confirmation_paiement.html.twig',
  200.                 compact('user', 'command')
  201.             ),
  202.             'text/html'
  203.         )
  204.             ->attach(Swift_Attachment::fromPath($path));
  205.         $this->swift_Mailer->send($message);
  206.     }
  207.     /**
  208.      * method to call after a company's subscription is finished
  209.      */
  210.     public function confirmCompanySSubscription(Company $company, Abonnement $abonnement, string $path)
  211.     {
  212.         $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - confirmation d’achat'))
  213.             ->setFrom($_ENV['MAILER_USERNAME'])
  214.             // ->setTo('contact@providerstravel.fr');
  215.             ->setTo($company->getEmail());
  216.         //$img1 = $message->embed(\Swift_Image::fromPath('images/img-mail-1.jpeg')) ?? "";
  217.         //$img2 = $message->embed(\Swift_Image::fromPath('images/img-mail-2.jpeg')) ?? "";
  218.         $message->setBody(
  219.             $this->environment->render(
  220.                 'company_space/subscription/billing_email_template.html.twig',
  221.                 compact('company', 'abonnement')
  222.             ),
  223.             'text/html'
  224.         )
  225.             ->attach(Swift_Attachment::fromPath($path));
  226.         $this->swift_Mailer->send($message);
  227.     }
  228.     public function confirmationCommandCompany(array $companies)
  229.     {
  230.         /** @var Company $company */
  231.         foreach ($companies as $company) {
  232.             $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Commande effectuée'))
  233.                 ->setFrom($_ENV['MAILER_USERNAME'])
  234.                 ->setTo($company->getEmail())
  235.                 ->setBody(
  236.                     $this->environment->render(
  237.                         'front/email/confirmation_paiement_company.html.twig'
  238.                     ),
  239.                     'text/html'
  240.                 );
  241.             $this->swift_Mailer->send($message);
  242.         }
  243.     }
  244.     public function subscriptionConfirm(Company $company)
  245.     {
  246.         $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - confirmation inscription'))
  247.             ->setFrom($_ENV['MAILER_USERNAME'])
  248.             ->setTo($company->getEmail())
  249.             ->setBody(
  250.                 $this->environment->render(
  251.                     'front/email/subscription_confirm.html.twig',
  252.                     ['company' => $company]
  253.                 ),
  254.                 'text/html'
  255.             );
  256.         $this->swift_Mailer->send($message);
  257.     }
  258.     public function subscriptionConfirmRoAdmin()
  259.     {
  260.         $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Inscription prestataire'))
  261.             ->setFrom($_ENV['MAILER_USERNAME'])
  262.             ->setTo($_ENV['GOOGLE_EMAIL_ADMIN_'])
  263.             ->setBody(
  264.                 $this->environment->render(
  265.                     'front/email/subscription_confirm_admin.html.twig'
  266.                 ),
  267.                 'text/html'
  268.             );
  269.         $this->swift_Mailer->send($message);
  270.     }
  271.     public function forgotPassword(string $newPassword, $user)
  272.     {
  273.         if ($user instanceof User) {
  274.             $template = 'front/email/forgot_password_user.html.twig';
  275.         } elseif ($user instanceof Company) {
  276.             $template = 'front/email/forgot_password_company.html.twig';
  277.         } else {
  278.             throw new NotFoundResourceException('Template not found');
  279.         }
  280.         $email = (new TemplatedEmail())
  281.             ->from($_ENV['MAILER_USERNAME'])
  282.             ->to(new Address($user->getEmail()))
  283.             ->subject('Réinitialisation de mot de passe!')
  284.             // path of the Twig template to render
  285.             ->htmlTemplate($template)
  286.             // pass variables (name => value) to the template
  287.             ->context([
  288.                         'password' => $newPassword, 
  289.                         'user' => $user
  290.                     ])
  291.         ;
  292.         $this->mailer->send($email);
  293.     }
  294.     public function DemmandeAvisClient()
  295.     {
  296.         $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - demmande avis au client'))
  297.             ->setFrom('radokoadolphe@gmail.com')
  298.             ->setTo('rajoharisonadolphe@gmail.com')
  299.             ->setBody(
  300.                 $this->environment->render(
  301.                     'front/email/avis.html.twig'
  302.                 ),
  303.                 'text/html'
  304.             );
  305.         $this->swift_Mailer->send($message);
  306.     }
  307.     public function confirmavis()
  308.     {
  309.         $commandes = $this->commandrepo->getCommandPasAvis();
  310.         foreach ($commandes as $command) {
  311.             $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Demande avis'))
  312.                 ->setFrom($_ENV['MAILER_USERNAME'])
  313.                 ->setTo($command->getClient()->getEmail())
  314.                 ->setBody(
  315.                     $this->environment->render(
  316.                         'front/email/confirmation_avis_client.html.twig'
  317.                     ),
  318.                     'text/html'
  319.                 );
  320.             $this->swift_Mailer->send($message);
  321.             $command->setIsAvis(true);
  322.             $this->em->persist($command);
  323.             $this->em->flush();
  324.         }
  325.     }
  326.     public function confirmRetourCaution()
  327.     {
  328.         $cautions = $this->caution->findAll();
  329.         
  330.         $domainName = $_ENV['DOMAIN_NAME'];
  331.         foreach ($cautions as $caution) {
  332.             $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - Demande avis'))
  333.                 ->setFrom($_ENV['MAILER_USERNAME'])
  334.                 ->setTo($caution->getUser()->getEmail())
  335.                 ->setBody(
  336.                     $this->environment->render(
  337.                         'front/email/confirma_retour_cation.html.twig',
  338.                         [
  339.                             'domain_name' => $domainName
  340.                         ]
  341.                     ),
  342.                     'text/html'
  343.                 );
  344.             $this->swift_Mailer->send($message);
  345.         }
  346.     }
  347.     public function testmail()
  348.     {
  349.         $message = (new \Swift_Message(ucfirst($_ENV['PLATEFORM_NAME']) . ' - demmande avis au client'))
  350.             ->setFrom('toetrainfo@gmail.com')
  351.             ->setTo('nyainalovanirina@gmail.com');
  352.         $img1 = $message->embed(\Swift_Image::fromPath('images/img-mail-1.jpeg')) ?? "";
  353.         $img2 = $message->embed(\Swift_Image::fromPath('images/img-mail-2.jpeg')) ?? "";
  354.         $imgLogo = $message->embed(\Swift_Image::fromPath('images/logo noir.png')) ?? "";
  355.         $message->setBody(
  356.             $this->environment->render(
  357.                 'front/email/confirmation_paiement.html.twig',
  358.                 compact('imgLogo', 'img1', 'img2')
  359.             ),
  360.             'text/html'
  361.         );
  362.         $this->swift_Mailer->send($message);
  363.     }
  364.     public function confirm_location_refund(Location $location)
  365.     {
  366.         $domainName = $_ENV['DOMAIN_NAME'];
  367.         $email = (new TemplatedEmail())
  368.             ->from($_ENV['MAILER_USERNAME'])
  369.             ->to(new Address($location->getProduct()->getCompany()->getEmail()))
  370.             ->addTo(new Address($location->getClient()->getEmail()))
  371.             ->subject('Thanks for signing up!')
  372.             // path of the Twig template to render
  373.             ->htmlTemplate('front/email/confirmation_refund.html.twig')
  374.             // pass variables (name => value) to the template
  375.             ->context([
  376.                     'domain_name' => $domainName,
  377.                     'location' => $location,
  378.                 ])
  379.         ;
  380.         $this->mailer->send($email);
  381.     }
  382.     public function testmailer(string $destination = 'hentsraf@gmail.com') {
  383.         $email = (new TemplatedEmail())
  384.             ->from($_ENV['MAILER_USERNAME'])
  385.             ->to($destination)
  386.             ->subject('Time for Symfony Mailer!')
  387.             ->text('Sending emails is fun again!')
  388.             ->htmlTemplate('front/email/test_email.html.twig');
  389.             // ->html('<p>See Twig integration for better HTML integration!</p>');
  390.         
  391.         // $signer = new DkimSigner('file://'.$_ENV['DKIM_PRIVATE_KEY_PATH'], $_ENV['DKIM_DOMAIN'], $_ENV['DKIM_SELECTOR']);
  392.         
  393.         // $signedEmail = $signer->sign($email);
  394.         $this->mailer->send($email);
  395.     }
  396. }