From d2e8c9caed7e2b705ccb9d9f8e18be2d8eb1d5ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?K=C3=A9vin=20Weyhaupt?= Date: Thu, 11 Dec 2025 10:51:16 +0100 Subject: [PATCH] fix(email): reinforcement of the validator and skip invalid address when sending alerts --- src/Command/AlertCommand.php | 44 ++++++++++++++++++++--------------- src/Form/SubscriptionType.php | 2 +- 2 files changed, 26 insertions(+), 20 deletions(-) diff --git a/src/Command/AlertCommand.php b/src/Command/AlertCommand.php index bcf898b..f14591c 100644 --- a/src/Command/AlertCommand.php +++ b/src/Command/AlertCommand.php @@ -12,6 +12,7 @@ use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; +use Symfony\Component\Mime\Exception\RfcComplianceException; use Symfony\Component\Routing\RouterInterface; use Webgriffe\SyliusBackInStockNotificationPlugin\Entity\SubscriptionInterface; use Webgriffe\SyliusBackInStockNotificationPlugin\Repository\SubscriptionRepositoryInterface; @@ -44,27 +45,32 @@ protected function execute(InputInterface $input, OutputInterface $output): int //I think that this load in the long time can be a bottle necklace $subscriptions = $this->backInStockNotificationRepository->findBy(['notify' => false]); foreach ($subscriptions as $subscription) { - $channel = $subscription->getChannel(); - $productVariant = $subscription->getProductVariant(); - if ($productVariant === null || $channel === null) { - $this->backInStockNotificationRepository->remove($subscription); - $this->logger->warning( - 'The back in stock subscription for the product does not have all the information required', - ['subscription' => var_export($subscription, true)], - ); + try { + $channel = $subscription->getChannel(); + $productVariant = $subscription->getProductVariant(); + if ($productVariant === null || $channel === null) { + $this->backInStockNotificationRepository->remove($subscription); + $this->logger->warning( + 'The back in stock subscription for the product does not have all the information required', + ['subscription' => var_export($subscription, true)], + ); - continue; - } + continue; + } - if ( - $this->availabilityChecker->isStockAvailable($productVariant) && - $productVariant->isEnabled() && - $productVariant->getProduct()?->isEnabled() === true - ) { - $this->router->getContext()->setHost($channel->getHostname() ?? 'localhost'); - $this->sendEmail($subscription, $productVariant, $channel); - $subscription->setNotify(true); - $this->backInStockNotificationRepository->add($subscription); + if ( + $this->availabilityChecker->isStockAvailable($productVariant) && + $productVariant->isEnabled() && + $productVariant->getProduct()?->isEnabled() === true + ) { + $this->router->getContext()->setHost($channel->getHostname() ?? 'localhost'); + $this->sendEmail($subscription, $productVariant, $channel); + $subscription->setNotify(true); + $this->backInStockNotificationRepository->add($subscription); + } + } catch (RfcComplianceException $e) { + // Invalid email address, continue to the next one + $this->logger->warning($e->getMessage()); } } diff --git a/src/Form/SubscriptionType.php b/src/Form/SubscriptionType.php index 4c60ddd..a2ed9f6 100644 --- a/src/Form/SubscriptionType.php +++ b/src/Form/SubscriptionType.php @@ -25,7 +25,7 @@ public function buildForm(FormBuilderInterface $builder, array $options): void ->add('email', EmailType::class, [ 'constraints' => [ new NotBlank([], null, null, null, ['webgriffe_sylius_back_in_stock_notification_plugin']), - new Email([], null, null, null, ['webgriffe_sylius_back_in_stock_notification_plugin']), + new Email(['mode' => Email::VALIDATION_MODE_STRICT], null, null, null, ['webgriffe_sylius_back_in_stock_notification_plugin']), ], ]) ->add('product_variant_code', HiddenType::class)