src/Service/Email/EmailService.php line 35
<?phpnamespace App\Service\Email;use Exception;use App\Entity\User;use App\Entity\ImportFile;use Symfony\Component\Mime\Address;use Symfony\Bridge\Twig\Mime\TemplatedEmail;use Symfony\Component\Mailer\MailerInterface;use Symfony\Component\HttpFoundation\RequestStack;use Symfony\Component\HttpFoundation\Session\Flash\FlashBagInterface;use Symfony\Component\DependencyInjection\ParameterBag\ParameterBagInterface;class EmailService{public string $defaultEmailFrom;public string $defaultEmailName;public array $toOverride = [];/*** @var MailerInterface*/private $mailer;private ?FlashBagInterface $flashBag;public function __construct(ParameterBagInterface $parameterBag, MailerInterface $mailer, RequestStack $requestStack){$this->defaultEmailFrom = $parameterBag->get('mailer_default_from');$this->defaultEmailName = $parameterBag->get('mailer_default_name');$this->mailer = $mailer;try {$this->flashBag = $requestStack->getSession()->getFlashBag();}catch (Exception $exception){$this->flashBag = null;}}public function createEmail(): TemplatedEmail{return (new TemplatedEmail())->from(new Address($this->defaultEmailFrom, $this->defaultEmailName));}public function sendResetPasswordEmail(User $user, $resetToken): void{$email = ($this->createEmail())->to($user->getEmail())->subject('Global Commission Password Reset')->htmlTemplate('emails/reset_password.html.twig')->context(['user' => $user,'resetToken' => $resetToken,]);try {$this->mailer->send($email);} catch (Exception $exception) {$this->flashBag?->add('danger', $exception->getMessage());}}public function test(){$email = $this->createEmail()->to('aaliyev@level6.com')->subject('Test Email')->htmlTemplate('emails/test.html.twig')->context(['title' => 'Test Email',]);try {$this->mailer->send($email);} catch (Exception $exception) {$this->flashBag?->add('danger', $exception->getMessage());}}public function sendImportImportFtpFilesResult($result, $globalErrors){$to = ['JDeerr-Ostertag@globalfurnituregroup.com', 'fsantisi@globalfurnituregroup.com', 'darcy@level6marketing.com'];#$to = ['aaliyev@level6.com'];$email = (new TemplatedEmail())->from('no-reply@globalindustries.com')->to(...$to)->subject('Import FTP Files Result')->htmlTemplate('emails/import_ftp_files_result.html.twig')->context(['result' => $result,'globalErrors' => $globalErrors,'importFileObj' => new ImportFile()]);try {$this->mailer->send($email);}catch (\Exception $exception){$this->flashBag?->add('danger', $exception->getMessage());}}public function sendGuaranteeEndsAlert($user){$to = ['JDeerr-Ostertag@globalfurnituregroup.com', 'fsantisi@globalfurnituregroup.com', 'darcy@level6marketing.com', 'aaliyev@level6.com'];$email = (new TemplatedEmail())->from('no-reply@globalindustries.com')->to(...$to)->subject(sprintf('%s Guarantee Ending', $user->getFullName()))->htmlTemplate('emails/guaranteeEndsAlert.html.twig')->context(['user' => $user,]);try {$this->mailer->send($email);}catch (\Exception $exception){$this->flashBag?->add('danger', $exception->getMessage());}}public function importFileCompletedAlert(ImportFile $importFile){// Create temporary file$tmpFile = tempnam(sys_get_temp_dir(), 'import_errors_');$handle = fopen($tmpFile, 'w');// Get errors and write to CSV$list = $importFile->getErrors();if (!empty($list)) {// Write headersforeach (min($list) as $key => $value){$headers[] = $key;}fputcsv($handle, $headers);// Write dataforeach ($list as $fields) {fputcsv($handle, $fields);}}fclose($handle);$to = ['JDeerr-Ostertag@globalfurnituregroup.com', 'fsantisi@globalfurnituregroup.com', 'darcy@level6marketing.com'];// Create email with attachment$email = (new TemplatedEmail())->from('no-reply@globalindustries.com')->to(...$to)->subject('Import File Completed')->htmlTemplate('emails/importFileCompletedAlert.html.twig')->context(['importFile' => $importFile,])->attach(fopen($tmpFile, 'r'),sprintf('import_errors_%s.csv', $importFile->getId()),'text/csv');try {$this->mailer->send($email);} catch (\Exception $exception) {$this->flashBag?->add('danger', $exception->getMessage());} finally {// Clean up temporary fileif (file_exists($tmpFile)) {unlink($tmpFile);}}}}