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