Chris@0: languageManager = $language_manager; Chris@0: $this->mailManager = $mail_manager; Chris@0: $this->logger = $logger; Chris@0: $this->stringTranslation = $string_translation; Chris@18: $this->userStorage = $entity_type_manager->getStorage('user'); Chris@0: } Chris@0: Chris@0: /** Chris@0: * {@inheritdoc} Chris@0: */ Chris@0: public function sendMailMessages(MessageInterface $message, AccountInterface $sender) { Chris@0: // Clone the sender, as we make changes to mail and name properties. Chris@0: $sender_cloned = clone $this->userStorage->load($sender->id()); Chris@0: $params = []; Chris@0: $current_langcode = $this->languageManager->getCurrentLanguage()->getId(); Chris@0: $recipient_langcode = $this->languageManager->getDefaultLanguage()->getId(); Chris@0: $contact_form = $message->getContactForm(); Chris@0: Chris@0: if ($sender_cloned->isAnonymous()) { Chris@0: // At this point, $sender contains an anonymous user, so we need to take Chris@0: // over the submitted form values. Chris@0: $sender_cloned->name = $message->getSenderName(); Chris@0: $sender_cloned->mail = $message->getSenderMail(); Chris@0: Chris@0: // For the email message, clarify that the sender name is not verified; it Chris@0: // could potentially clash with a username on this site. Chris@0: $sender_cloned->name = $this->t('@name (not verified)', ['@name' => $message->getSenderName()]); Chris@0: } Chris@0: Chris@0: // Build email parameters. Chris@0: $params['contact_message'] = $message; Chris@0: $params['sender'] = $sender_cloned; Chris@0: Chris@0: if (!$message->isPersonal()) { Chris@0: // Send to the form recipient(s), using the site's default language. Chris@0: $params['contact_form'] = $contact_form; Chris@0: Chris@0: $to = implode(', ', $contact_form->getRecipients()); Chris@0: } Chris@0: elseif ($recipient = $message->getPersonalRecipient()) { Chris@0: // Send to the user in the user's preferred language. Chris@0: $to = $recipient->getEmail(); Chris@0: $recipient_langcode = $recipient->getPreferredLangcode(); Chris@0: $params['recipient'] = $recipient; Chris@0: } Chris@0: else { Chris@0: throw new MailHandlerException('Unable to determine message recipient'); Chris@0: } Chris@0: Chris@0: // Send email to the recipient(s). Chris@0: $key_prefix = $message->isPersonal() ? 'user' : 'page'; Chris@0: $this->mailManager->mail('contact', $key_prefix . '_mail', $to, $recipient_langcode, $params, $sender_cloned->getEmail()); Chris@0: Chris@0: // If requested, send a copy to the user, using the current language. Chris@0: if ($message->copySender()) { Chris@0: $this->mailManager->mail('contact', $key_prefix . '_copy', $sender_cloned->getEmail(), $current_langcode, $params, $sender_cloned->getEmail()); Chris@0: } Chris@0: Chris@0: // If configured, send an auto-reply, using the current language. Chris@0: if (!$message->isPersonal() && $contact_form->getReply()) { Chris@0: // User contact forms do not support an auto-reply message, so this Chris@0: // message always originates from the site. Chris@0: if (!$sender_cloned->getEmail()) { Chris@0: $this->logger->error('Error sending auto-reply, missing sender e-mail address in %contact_form', [ Chris@0: '%contact_form' => $contact_form->label(), Chris@0: ]); Chris@0: } Chris@0: else { Chris@0: $this->mailManager->mail('contact', 'page_autoreply', $sender_cloned->getEmail(), $current_langcode, $params); Chris@0: } Chris@0: } Chris@0: Chris@0: if (!$message->isPersonal()) { Chris@0: $this->logger->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', [ Chris@17: '%sender-name' => $sender_cloned->getAccountName(), Chris@0: '@sender-from' => $sender_cloned->getEmail(), Chris@0: '%contact_form' => $contact_form->label(), Chris@0: ]); Chris@0: } Chris@0: else { Chris@0: $this->logger->notice('%sender-name (@sender-from) sent %recipient-name an email.', [ Chris@17: '%sender-name' => $sender_cloned->getAccountName(), Chris@0: '@sender-from' => $sender_cloned->getEmail(), Chris@17: '%recipient-name' => $message->getPersonalRecipient()->getAccountName(), Chris@0: ]); Chris@0: } Chris@0: } Chris@0: Chris@0: }