transportBuilder = $transportBuilder; $this->config = $config; $this->deployConfig = $deployConfig; $this->storeManager = $storeManager; } /** * Send a notification. * * @param string $templateConfigId * @param array $templateVars * @param string $toEmail * @param string $toName * @throws MailException * * @return void */ private function sendNotification( string $templateConfigId, array $templateVars, string $toEmail, string $toName ): void { $transport = $this->transportBuilder ->setTemplateIdentifier($this->config->getValue($templateConfigId)) ->setTemplateModel(BackendTemplate::class) ->setTemplateOptions([ 'area' => FrontNameResolver::AREA_CODE, 'store' => Store::DEFAULT_STORE_ID ]) ->setTemplateVars($templateVars) ->setFrom( $this->config->getValue('admin/emails/forgot_email_identity') ) ->addTo($toEmail, $toName) ->getTransport(); $transport->sendMessage(); } /** * @inheritDoc */ public function sendForgotPassword(UserInterface $user): void { try { $this->sendNotification( 'admin/emails/forgot_email_template', [ 'user' => $user, 'store' => $this->storeManager->getStore( Store::DEFAULT_STORE_ID ) ], $user->getEmail(), $user->getFirstName().' '.$user->getLastName() ); } catch (LocalizedException $exception) { throw new NotificatorException( __($exception->getMessage()), $exception ); } } /** * @inheritDoc */ public function sendCreated(UserInterface $user): void { $toEmails = []; $generalEmail = $this->config->getValue( 'trans_email/ident_general/email' ); if ($generalEmail) { $toEmails[] = $generalEmail; } if ($adminEmail = $this->deployConfig->get('user_admin_email')) { $toEmails[] = $adminEmail; } try { foreach ($toEmails as $toEmail) { $this->sendNotification( 'admin/emails/new_user_notification_template', [ 'user' => $user, 'store' => $this->storeManager->getStore( Store::DEFAULT_STORE_ID ) ], $toEmail, __('Administrator')->getText() ); } } catch (LocalizedException $exception) { throw new NotificatorException( __($exception->getMessage()), $exception ); } } /** * @inheritDoc */ public function sendUpdated(UserInterface $user, array $changed): void { $email = $user->getEmail(); if ($user instanceof User) { $email = $user->getOrigData('email'); } try { $this->sendNotification( 'admin/emails/user_notification_template', [ 'user' => $user, 'store' => $this->storeManager->getStore( Store::DEFAULT_STORE_ID ), 'changes' => implode(', ', $changed) ], $email, $user->getFirstName().' '.$user->getLastName() ); } catch (LocalizedException $exception) { throw new NotificatorException( __($exception->getMessage()), $exception ); } } }