View source
<?php
namespace Drupal\contact;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Language\LanguageManagerInterface;
use Drupal\Core\Mail\MailManagerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\StringTranslationTrait;
use Drupal\Core\StringTranslation\TranslationInterface;
use Psr\Log\LoggerInterface;
class MailHandler implements MailHandlerInterface {
use StringTranslationTrait;
protected $languageManager;
protected $logger;
protected $mailManager;
protected $userStorage;
public function __construct(MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, LoggerInterface $logger, TranslationInterface $string_translation, EntityTypeManagerInterface $entity_type_manager) {
$this->languageManager = $language_manager;
$this->mailManager = $mail_manager;
$this->logger = $logger;
$this->stringTranslation = $string_translation;
$this->userStorage = $entity_type_manager
->getStorage('user');
}
public function sendMailMessages(MessageInterface $message, AccountInterface $sender) {
$sender_cloned = clone $this->userStorage
->load($sender
->id());
$params = [];
$current_langcode = $this->languageManager
->getCurrentLanguage()
->getId();
$recipient_langcode = $this->languageManager
->getDefaultLanguage()
->getId();
$contact_form = $message
->getContactForm();
if ($sender_cloned
->isAnonymous()) {
$sender_cloned->name = $message
->getSenderName();
$sender_cloned->mail = $message
->getSenderMail();
$sender_cloned->name = $this
->t('@name (not verified)', [
'@name' => $message
->getSenderName(),
]);
}
$params['contact_message'] = $message;
$params['sender'] = $sender_cloned;
if (!$message
->isPersonal()) {
$params['contact_form'] = $contact_form;
$to = implode(', ', $contact_form
->getRecipients());
}
elseif ($recipient = $message
->getPersonalRecipient()) {
$to = $recipient
->getEmail();
$recipient_langcode = $recipient
->getPreferredLangcode();
$params['recipient'] = $recipient;
}
else {
throw new MailHandlerException('Unable to determine message recipient');
}
$key_prefix = $message
->isPersonal() ? 'user' : 'page';
$this->mailManager
->mail('contact', $key_prefix . '_mail', $to, $recipient_langcode, $params, $sender_cloned
->getEmail());
if ($message
->copySender()) {
$this->mailManager
->mail('contact', $key_prefix . '_copy', $sender_cloned
->getEmail(), $current_langcode, $params, $sender_cloned
->getEmail());
}
if (!$message
->isPersonal() && $contact_form
->getReply()) {
if (!$sender_cloned
->getEmail()) {
$this->logger
->error('Error sending auto-reply, missing sender e-mail address in %contact_form', [
'%contact_form' => $contact_form
->label(),
]);
}
else {
$this->mailManager
->mail('contact', 'page_autoreply', $sender_cloned
->getEmail(), $current_langcode, $params);
}
}
if (!$message
->isPersonal()) {
$this->logger
->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', [
'%sender-name' => $sender_cloned
->getAccountName(),
'@sender-from' => $sender_cloned
->getEmail() ?? '',
'%contact_form' => $contact_form
->label(),
]);
}
else {
$this->logger
->notice('%sender-name (@sender-from) sent %recipient-name an email.', [
'%sender-name' => $sender_cloned
->getAccountName(),
'@sender-from' => $sender_cloned
->getEmail(),
'%recipient-name' => $message
->getPersonalRecipient()
->getAccountName(),
]);
}
}
}