3 namespace Drupal\contact;
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;
14 * Provides a class for handling assembly and dispatch of contact mail messages.
16 class MailHandler implements MailHandlerInterface {
18 use StringTranslationTrait;
21 * Language manager service.
23 * @var \Drupal\Core\Language\LanguageManagerInterface
25 protected $languageManager;
30 * @var \Drupal\Core\Logger\LoggerChannelInterface
35 * Mail manager service.
37 * @var \Drupal\Core\Mail\MailManagerInterface
39 protected $mailManager;
42 * The user entity storage handler.
44 * @var \Drupal\Core\Entity\EntityStorageInterface
46 protected $userStorage;
49 * Constructs a new \Drupal\contact\MailHandler object.
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
57 * @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
58 * String translation service.
59 * @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
60 * Entity manager service.
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');
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());
77 $current_langcode = $this->languageManager->getCurrentLanguage()->getId();
78 $recipient_langcode = $this->languageManager->getDefaultLanguage()->getId();
79 $contact_form = $message->getContactForm();
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();
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()]);
92 // Build email parameters.
93 $params['contact_message'] = $message;
94 $params['sender'] = $sender_cloned;
96 if (!$message->isPersonal()) {
97 // Send to the form recipient(s), using the site's default language.
98 $params['contact_form'] = $contact_form;
100 $to = implode(', ', $contact_form->getRecipients());
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;
109 throw new MailHandlerException('Unable to determine message recipient');
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());
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());
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(),
131 $this->mailManager->mail('contact', 'page_autoreply', $sender_cloned->getEmail(), $current_langcode, $params);
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(),
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(),