diff --git a/src/Controller/OpenyMemberships.php b/src/Controller/OpenyMemberships.php index e53ae98..f0e1ce5 100644 --- a/src/Controller/OpenyMemberships.php +++ b/src/Controller/OpenyMemberships.php @@ -18,6 +18,7 @@ use Drupal\commerce_product\Entity\Product; use Drupal\commerce_promotion\Entity\Promotion; use Drupal\node\Entity\Node; +use Drupal\Component\Utility\EmailValidator; /** * Provides OpenyMemberships controller. @@ -84,6 +85,7 @@ class OpenyMemberships extends ControllerBase { * @param \Drupal\Core\Render\RendererInterface $renderer * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager * @param \Drupal\openy_memberships\OmPDFGenerator $pdf_generator + * @param \Drupal\Component\Utility\EmailValidator $email_validator */ public function __construct( EntityTypeManagerInterface $entity_type_manager, @@ -92,7 +94,8 @@ public function __construct( AccountProxyInterface $current_user, RendererInterface $renderer, MailManagerInterface $mail_manager, - OmPDFGenerator $pdf_generator + OmPDFGenerator $pdf_generator, + EmailValidator $email_validator ) { $this->entityTypeManager = $entity_type_manager; $this->configFactory = $config_factory; @@ -102,6 +105,7 @@ public function __construct( $this->renderer = $renderer; $this->mailManager = $mail_manager; $this->pdfGenerator = $pdf_generator; + $this->emailValidator = $email_validator; } /** @@ -115,7 +119,8 @@ public static function create(ContainerInterface $container) { $container->get('current_user'), $container->get('renderer'), $container->get('plugin.manager.mail'), - $container->get('openy_memberships_pdf_generator') + $container->get('openy_memberships_pdf_generator'), + $container->get('email.validator') ); } @@ -551,8 +556,19 @@ public function sendSummaryEmail(Request $request, $order_uuid) { $order = reset($order); $store = $this->entityTypeManager->getStorage('commerce_store')->loadDefault(); $user_email = $order->get('mail')->getValue()[0]['value']; + $store_email = $store->getEmail(); + + if ($user_email && $this->emailValidator->isValid($user_email) && $store_email && $this->emailValidator->isValid($store_email)) { + $to = implode(', ', [$store_email, $user_email]); + } + elseif ($store_email && $this->emailValidator->isValid($store_email)) { + $to = $store->getEmail(); + } + else { + // You need to setup store email at least. Using site email as a backup. + $to = \Drupal::config('system.site')->get('mail'); + } - $to = implode(', ', [$store->getEmail(), $user_email]); $from = $this->siteConfig->get('mail'); $langcode = $this->currentUser->getPreferredLangcode(); $subject = $this->t('Your Membership!');