Skip to content

Commit

Permalink
Merge pull request #100 from ymcatwincities/email_fix
Browse files Browse the repository at this point in the history
fix: Fix email for anonymous user
  • Loading branch information
anpolimus authored Jan 21, 2022
2 parents a94f494 + a8426ba commit 3b5979f
Showing 1 changed file with 19 additions and 3 deletions.
22 changes: 19 additions & 3 deletions src/Controller/OpenyMemberships.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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,
Expand All @@ -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;
Expand All @@ -102,6 +105,7 @@ public function __construct(
$this->renderer = $renderer;
$this->mailManager = $mail_manager;
$this->pdfGenerator = $pdf_generator;
$this->emailValidator = $email_validator;
}

/**
Expand All @@ -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')
);
}

Expand Down Expand Up @@ -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!');
Expand Down

0 comments on commit 3b5979f

Please sign in to comment.