Upgraded drupal core with security updates
[yaffs-website] / web / core / modules / contact / src / MailHandler.php
1 <?php
2
3 namespace Drupal\contact;
4
5 use Drupal\Core\Entity\EntityManagerInterface;
6 use Drupal\Core\Language\LanguageManagerInterface;
7 use Drupal\Core\Mail\MailManagerInterface;
8 use Drupal\Core\Session\AccountInterface;
9 use Drupal\Core\StringTranslation\StringTranslationTrait;
10 use Drupal\Core\StringTranslation\TranslationInterface;
11 use Psr\Log\LoggerInterface;
12
13 /**
14  * Provides a class for handling assembly and dispatch of contact mail messages.
15  */
16 class MailHandler implements MailHandlerInterface {
17
18   use StringTranslationTrait;
19
20   /**
21    * Language manager service.
22    *
23    * @var \Drupal\Core\Language\LanguageManagerInterface
24    */
25   protected $languageManager;
26
27   /**
28    * Logger service.
29    *
30    * @var \Drupal\Core\Logger\LoggerChannelInterface
31    */
32   protected $logger;
33
34   /**
35    * Mail manager service.
36    *
37    * @var \Drupal\Core\Mail\MailManagerInterface
38    */
39   protected $mailManager;
40
41   /**
42    * The user entity storage handler.
43    *
44    * @var \Drupal\Core\Entity\EntityStorageInterface
45    */
46   protected $userStorage;
47
48   /**
49    * Constructs a new \Drupal\contact\MailHandler object.
50    *
51    * @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
52    *   Mail manager service.
53    * @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
54    *   Language manager service.
55    * @param \Psr\Log\LoggerInterface $logger
56    *   A logger instance.
57    * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
58    *   String translation service.
59    * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
60    *   Entity manager service.
61    */
62   public function __construct(MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, LoggerInterface $logger, TranslationInterface $string_translation, EntityManagerInterface $entity_manager) {
63     $this->languageManager = $language_manager;
64     $this->mailManager = $mail_manager;
65     $this->logger = $logger;
66     $this->stringTranslation = $string_translation;
67     $this->userStorage = $entity_manager->getStorage('user');
68   }
69
70   /**
71    * {@inheritdoc}
72    */
73   public function sendMailMessages(MessageInterface $message, AccountInterface $sender) {
74     // Clone the sender, as we make changes to mail and name properties.
75     $sender_cloned = clone $this->userStorage->load($sender->id());
76     $params = [];
77     $current_langcode = $this->languageManager->getCurrentLanguage()->getId();
78     $recipient_langcode = $this->languageManager->getDefaultLanguage()->getId();
79     $contact_form = $message->getContactForm();
80
81     if ($sender_cloned->isAnonymous()) {
82       // At this point, $sender contains an anonymous user, so we need to take
83       // over the submitted form values.
84       $sender_cloned->name = $message->getSenderName();
85       $sender_cloned->mail = $message->getSenderMail();
86
87       // For the email message, clarify that the sender name is not verified; it
88       // could potentially clash with a username on this site.
89       $sender_cloned->name = $this->t('@name (not verified)', ['@name' => $message->getSenderName()]);
90     }
91
92     // Build email parameters.
93     $params['contact_message'] = $message;
94     $params['sender'] = $sender_cloned;
95
96     if (!$message->isPersonal()) {
97       // Send to the form recipient(s), using the site's default language.
98       $params['contact_form'] = $contact_form;
99
100       $to = implode(', ', $contact_form->getRecipients());
101     }
102     elseif ($recipient = $message->getPersonalRecipient()) {
103       // Send to the user in the user's preferred language.
104       $to = $recipient->getEmail();
105       $recipient_langcode = $recipient->getPreferredLangcode();
106       $params['recipient'] = $recipient;
107     }
108     else {
109       throw new MailHandlerException('Unable to determine message recipient');
110     }
111
112     // Send email to the recipient(s).
113     $key_prefix = $message->isPersonal() ? 'user' : 'page';
114     $this->mailManager->mail('contact', $key_prefix . '_mail', $to, $recipient_langcode, $params, $sender_cloned->getEmail());
115
116     // If requested, send a copy to the user, using the current language.
117     if ($message->copySender()) {
118       $this->mailManager->mail('contact', $key_prefix . '_copy', $sender_cloned->getEmail(), $current_langcode, $params, $sender_cloned->getEmail());
119     }
120
121     // If configured, send an auto-reply, using the current language.
122     if (!$message->isPersonal() && $contact_form->getReply()) {
123       // User contact forms do not support an auto-reply message, so this
124       // message always originates from the site.
125       if (!$sender_cloned->getEmail()) {
126         $this->logger->error('Error sending auto-reply, missing sender e-mail address in %contact_form', [
127           '%contact_form' => $contact_form->label(),
128         ]);
129       }
130       else {
131         $this->mailManager->mail('contact', 'page_autoreply', $sender_cloned->getEmail(), $current_langcode, $params);
132       }
133     }
134
135     if (!$message->isPersonal()) {
136       $this->logger->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', [
137         '%sender-name' => $sender_cloned->getUsername(),
138         '@sender-from' => $sender_cloned->getEmail(),
139         '%contact_form' => $contact_form->label(),
140       ]);
141     }
142     else {
143       $this->logger->notice('%sender-name (@sender-from) sent %recipient-name an email.', [
144         '%sender-name' => $sender_cloned->getUsername(),
145         '@sender-from' => $sender_cloned->getEmail(),
146         '%recipient-name' => $message->getPersonalRecipient()->getUsername(),
147       ]);
148     }
149   }
150
151 }