src/Service/Email/EmailService.php line 35

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