src/Service/Email/EmailService.php line 36

  1. <?php
  2. namespace App\Service\Email;
  3. use Exception;
  4. use App\Entity\User;
  5. use App\Entity\ImportFile;
  6. use App\Requests\ContactFormRequest;
  7. use Symfony\Component\Mime\Address;
  8. use Symfony\Bridge\Twig\Mime\TemplatedEmail;
  9. use Symfony\Component\Mailer\MailerInterface;
  10. use Symfony\Component\HttpFoundation\RequestStack;
  11. use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;
  12. use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;
  13. class EmailService
  14. {
  15.     public string $defaultEmailFrom;
  16.     public string $defaultEmailName;
  17.     public array $toOverride = [];
  18.     /**
  19.      * @var MailerInterface
  20.      */
  21.     private $mailer;
  22.     private ?FlashBagInterface $flashBag;
  23.     public function __construct(ParameterBagInterface $parameterBagMailerInterface $mailerRequestStack $requestStack)
  24.     {
  25.         $this->defaultEmailFrom $parameterBag->get('mailer_default_from');
  26.         $this->defaultEmailName $parameterBag->get('mailer_default_name');
  27.         $this->mailer $mailer;
  28.         try {
  29.             $this->flashBag $requestStack->getSession()->getFlashBag();
  30.         }catch (Exception $exception){
  31.             $this->flashBag null;
  32.         }
  33.     }
  34.     public function createEmail(): TemplatedEmail
  35.     {
  36.         return (new TemplatedEmail())->from(
  37.             new Address($this->defaultEmailFrom$this->defaultEmailName)
  38.         );
  39.     }
  40.     public function sendResetPasswordEmail(User $user$resetToken): void
  41.     {
  42.         $email = ($this->createEmail())
  43.             ->to($user->getEmail())
  44.             ->subject('Global Commission Password Reset')
  45.             ->htmlTemplate('emails/reset_password.html.twig')
  46.             ->context([
  47.                 'user' => $user,
  48.                 'resetToken' => $resetToken,
  49.             ]);
  50.         try {
  51.             $this->mailer->send($email);
  52.         } catch (Exception $exception) {
  53.             $this->flashBag?->add('danger'$exception->getMessage());
  54.         }
  55.     }
  56.     public function test()
  57.     {
  58.         $email $this->createEmail()
  59.             ->to('aaliyev@level6.com')
  60.             ->subject('Test Email')
  61.             ->htmlTemplate('emails/test.html.twig')
  62.             ->context([
  63.                 'title' => 'Test Email',
  64.             ]);
  65.         try {
  66.             $this->mailer->send($email);
  67.         } catch (Exception $exception) {
  68.             $this->flashBag?->add('danger'$exception->getMessage());
  69.         }
  70.     }
  71.     public function sendImportImportFtpFilesResult($result$globalErrors)
  72.     {
  73.         $to = ['JDeerr-Ostertag@globalfurnituregroup.com''fsantisi@globalfurnituregroup.com''darcy@level6marketing.com'];
  74.         #$to = ['aaliyev@level6.com'];
  75.         $email = (new TemplatedEmail())
  76.             ->from('no-reply@globalindustries.com')
  77.             ->to(...$to)
  78.             ->subject('Import FTP Files Result')
  79.             ->htmlTemplate('emails/import_ftp_files_result.html.twig')
  80.             ->context([
  81.                 'result' => $result,
  82.                 'globalErrors' => $globalErrors,
  83.                 'importFileObj' => new ImportFile()
  84.             ])
  85.         ;
  86.         try {
  87.             $this->mailer->send($email);
  88.         }catch (\Exception $exception){
  89.             $this->flashBag?->add('danger'$exception->getMessage());
  90.         }
  91.     }
  92.     public function sendGuaranteeEndsAlert($user)
  93.     {
  94.         $to = ['JDeerr-Ostertag@globalfurnituregroup.com''fsantisi@globalfurnituregroup.com''darcy@level6marketing.com''aaliyev@level6.com'];
  95.         $email = (new TemplatedEmail())
  96.             ->from('no-reply@globalindustries.com')
  97.             ->to(...$to)
  98.             ->subject(sprintf('%s Guarantee Ending'$user->getFullName()))
  99.             ->htmlTemplate('emails/guaranteeEndsAlert.html.twig')
  100.             ->context([
  101.                 'user' => $user,
  102.             ])
  103.         ;
  104.         try {
  105.             $this->mailer->send($email);
  106.         }catch (\Exception $exception){
  107.             $this->flashBag?->add('danger'$exception->getMessage());
  108.         }
  109.     }
  110.     public function importFileCompletedAlert(ImportFile $importFile)
  111.     {
  112.         // Create temporary file
  113.         $tmpFile tempnam(sys_get_temp_dir(), 'import_errors_');
  114.         $handle fopen($tmpFile'w');
  115.         // Get errors and write to CSV
  116.         $list $importFile->getErrors();
  117.         if (!empty($list)) {
  118.             // Write headers
  119.             foreach (min($list) as $key => $value){
  120.                 $headers[] = $key;
  121.             }
  122.             fputcsv($handle$headers);
  123.             // Write data
  124.             foreach ($list as $fields) {
  125.                 fputcsv($handle$fields);
  126.             }
  127.         }
  128.         fclose($handle);
  129.         $to = ['JDeerr-Ostertag@globalfurnituregroup.com''fsantisi@globalfurnituregroup.com''darcy@level6marketing.com'];
  130.         // Create email with attachment
  131.         $email = (new TemplatedEmail())
  132.             ->from('no-reply@globalindustries.com')
  133.             ->to(...$to)
  134.             ->subject('Import File Completed')
  135.             ->htmlTemplate('emails/importFileCompletedAlert.html.twig')
  136.             ->context([
  137.                 'importFile' => $importFile,
  138.             ])
  139.             ->attach(
  140.                 fopen($tmpFile'r'),
  141.                 sprintf('import_errors_%s.csv'$importFile->getId()),
  142.                 'text/csv'
  143.             );
  144.         try {
  145.             $this->mailer->send($email);
  146.         } catch (\Exception $exception) {
  147.             $this->flashBag?->add('danger'$exception->getMessage());
  148.         } finally {
  149.             // Clean up temporary file
  150.             if (file_exists($tmpFile)) {
  151.                 unlink($tmpFile);
  152.             }
  153.         }
  154.     }
  155.     public function sendContactForm(ContactFormRequest $contactFormRequest)
  156.     {
  157.         $to = [
  158.             'jdeerr-ostertag@globalindustries.com',
  159.             'fsantisi@globalindustries.com',
  160.         ];
  161.         $cc = ['dstgeorge@level6.com'];
  162.         $category $contactFormRequest->getCategory();
  163.         $subject $category
  164.             sprintf('Commissions Inquiry: %s'$category)
  165.             : 'Commissions Inquiry';
  166.         $email = (new TemplatedEmail())
  167.             ->from('no-reply@globalindustries.com')
  168.             ->to(...$to)
  169.             ->cc(...$cc)
  170.             ->replyTo(new Address($contactFormRequest->getEmail(), $contactFormRequest->getName()))
  171.             ->subject($subject)
  172.             ->htmlTemplate('emails/contact_form.html.twig')
  173.             ->context([
  174.                 'contactFormRequest' => $contactFormRequest,
  175.             ]);
  176.         try {
  177.             $this->mailer->send($email);
  178.         } catch (Exception $exception) {
  179.             $this->flashBag?->add('danger'$exception->getMessage());
  180.         }
  181.     }
  182. }