Commit e5921e78 authored by alexpott's avatar alexpott

Issue #2325801 by jibran, larowlan: Abstract contact module mail delivery out...

Issue #2325801 by jibran, larowlan: Abstract contact module mail delivery out of form into a service.
parent ee083e0e
......@@ -4,3 +4,11 @@ services:
tags:
- { name: access_check, applies_to: _access_contact_personal_tab }
arguments: ['@config.factory', '@user.data']
contact.mail_handler:
class: Drupal\contact\MailHandler
arguments: ['@plugin.manager.mail', '@language_manager', '@logger.channel.contact', '@string_translation', '@entity.manager']
logger.channel.contact:
class: Drupal\Core\Logger\LoggerChannel
factory_method: get
factory_service: logger.factory
arguments: ['contact']
<?php
/**
* @file
* Contains \Drupal\contact\MailHandler.
*/
namespace Drupal\contact;
use Drupal\Core\Entity\EntityManagerInterface;
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;
/**
* Provides a class for handling assembly and dispatch of contact mail messages.
*/
class MailHandler implements MailHandlerInterface {
use StringTranslationTrait;
/**
* Language manager service.
*
* @var \Drupal\Core\Language\LanguageManagerInterface
*/
protected $languageManager;
/**
* Logger service.
*
* @var \Drupal\Core\Logger\LoggerChannelInterface
*/
protected $logger;
/**
* Mail manager service.
*
* @var \Drupal\Core\Mail\MailManagerInterface
*/
protected $mailManager;
/**
* The user entity storage handler.
*
* @var \Drupal\Core\Entity\EntityStorageInterface
*/
protected $userStorage;
/**
* Constructs a new \Drupal\contact\MailHandler object.
*
* @param \Drupal\Core\Mail\MailManagerInterface $mail_manager
* Mail manager service.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* Language manager service.
* @param \Psr\Log\LoggerInterface $logger
* A logger instance.
* @param \Drupal\Core\StringTranslation\TranslationInterface $string_translation
* String translation service.
* @param \Drupal\Core\Entity\EntityManagerInterface $entity_manager
* Entity manager service.
*/
public function __construct(MailManagerInterface $mail_manager, LanguageManagerInterface $language_manager, LoggerInterface $logger, TranslationInterface $string_translation, EntityManagerInterface $entity_manager) {
$this->languageManager = $language_manager;
$this->mailManager = $mail_manager;
$this->logger = $logger;
$this->stringTranslation = $string_translation;
$this->userStorage = $entity_manager->getStorage('user');
}
/**
* {@inheritdoc}
*/
public function sendMailMessages(MessageInterface $message, AccountInterface $sender) {
// Clone the sender, as we make changes to mail and name properties.
$sender_cloned = clone $this->userStorage->load($sender->id());
$params = array();
$current_langcode = $this->languageManager->getCurrentLanguage()->getId();
$recipient_langcode = $this->languageManager->getDefaultLanguage()->getId();
$contact_form = $message->getContactForm();
if ($sender_cloned->isAnonymous()) {
// At this point, $sender contains an anonymous user, so we need to take
// over the submitted form values.
$sender_cloned->name = $message->getSenderName();
$sender_cloned->mail = $message->getSenderMail();
// For the email message, clarify that the sender name is not verified; it
// could potentially clash with a username on this site.
$sender_cloned->name = $this->t('!name (not verified)', array('!name' => $message->getSenderName()));
}
// Build email parameters.
$params['contact_message'] = $message;
$params['sender'] = $sender_cloned;
if (!$message->isPersonal()) {
// Send to the form recipient(s), using the site's default language.
$params['contact_form'] = $contact_form;
$to = implode(', ', $contact_form->getRecipients());
}
elseif ($recipient = $message->getPersonalRecipient()) {
// Send to the user in the user's preferred language.
$to = $recipient->getEmail();
$recipient_langcode = $recipient->getPreferredLangcode();
$params['recipient'] = $recipient;
}
else {
throw new MailHandlerException('Unable to determine message recipient');
}
// Send email to the recipient(s).
$key_prefix = $message->isPersonal() ? 'user' : 'page';
$this->mailManager->mail('contact', $key_prefix . '_mail', $to, $recipient_langcode, $params, $sender_cloned->getEmail());
// If requested, send a copy to the user, using the current language.
if ($message->copySender()) {
$this->mailManager->mail('contact', $key_prefix . '_copy', $sender_cloned->getEmail(), $current_langcode, $params, $sender_cloned->getEmail());
}
// If configured, send an auto-reply, using the current language.
if (!$message->isPersonal() && $contact_form->getReply()) {
// User contact forms do not support an auto-reply message, so this
// message always originates from the site.
$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.', array(
'%sender-name' => $sender_cloned->getUsername(),
'@sender-from' => $sender_cloned->getEmail(),
'%contact_form' => $contact_form->label(),
));
}
else {
$this->logger->notice('%sender-name (@sender-from) sent %recipient-name an email.', array(
'%sender-name' => $sender_cloned->getUsername(),
'@sender-from' => $sender_cloned->getEmail(),
'%recipient-name' => $message->getPersonalRecipient()->getUsername(),
));
}
}
}
<?php
/**
* @file
* Contains \Drupal\contact\MailHandlerException.
*/
namespace Drupal\contact;
/**
* Exception thrown by MailHandler when unable to determine message recipient.
*/
class MailHandlerException extends \RuntimeException {}
<?php
/**
* @file
* Contains \Drupal\contact\MailHandlerInterface.
*/
namespace Drupal\contact;
use Drupal\Core\Session\AccountInterface;
/**
* Provides an interface for assembly and dispatch of contact mail messages.
*/
interface MailHandlerInterface {
/**
* Sends mail messages as appropriate for a given Message form submission.
*
* Can potentially send up to three messages as follows:
* - To the configured recipient;
* - Auto-reply to the sender; and
* - Carbon copy to the sender.
*
* @param \Drupal\contact\MessageInterface $message
* Submitted message entity.
* @param \Drupal\Core\Session\AccountInterface $sender
* User that submitted the message entity form.
*
* @throws \Drupal\contact\MailHandlerException
* When unable to determine message recipient.
*/
public function sendMailMessages(MessageInterface $message, AccountInterface $sender);
}
......@@ -43,6 +43,13 @@ class MessageForm extends ContentEntityForm {
*/
protected $languageManager;
/**
* The contact mail handler service.
*
* @var \Drupal\contact\MailHandlerInterface
*/
protected $mailHandler;
/**
* Constructs a MessageForm object.
*
......@@ -52,11 +59,14 @@ class MessageForm extends ContentEntityForm {
* The flood control mechanism.
* @param \Drupal\Core\Language\LanguageManagerInterface $language_manager
* The language manager service.
* @param \Drupal\contact\MailHandlerInterface $mail_handler
* The contact mail handler service.
*/
public function __construct(EntityManagerInterface $entity_manager, FloodInterface $flood, LanguageManagerInterface $language_manager) {
public function __construct(EntityManagerInterface $entity_manager, FloodInterface $flood, LanguageManagerInterface $language_manager, MailHandlerInterface $mail_handler) {
parent::__construct($entity_manager);
$this->flood = $flood;
$this->languageManager = $language_manager;
$this->mailHandler = $mail_handler;
}
/**
......@@ -66,7 +76,8 @@ public static function create(ContainerInterface $container) {
return new static(
$container->get('entity.manager'),
$container->get('flood'),
$container->get('language_manager')
$container->get('language_manager'),
$container->get('contact.mail_handler')
);
}
......@@ -175,76 +186,11 @@ public function preview(array $form, FormStateInterface $form_state) {
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
$user = $this->currentUser();
$language_interface = $this->languageManager->getCurrentLanguage();
$message = $this->entity;
$sender = clone $this->entityManager->getStorage('user')->load($user->id());
if ($user->isAnonymous()) {
// At this point, $sender contains an anonymous user, so we need to take
// over the submitted form values.
$sender->name = $message->getSenderName();
$sender->mail = $message->getSenderMail();
// For the email message, clarify that the sender name is not verified; it
// could potentially clash with a username on this site.
$sender->name = $this->t('!name (not verified)', array('!name' => $message->getSenderName()));
}
// Build email parameters.
$params['contact_message'] = $message;
$params['sender'] = $sender;
if (!$message->isPersonal()) {
// Send to the form recipient(s), using the site's default language.
$contact_form = $message->getContactForm();
$params['contact_form'] = $contact_form;
$to = implode(', ', $contact_form->getRecipients());
$recipient_langcode = $this->languageManager->getDefaultLanguage()->getId();
}
elseif ($recipient = $message->getPersonalRecipient()) {
// Send to the user in the user's preferred language.
$to = $recipient->getEmail();
$recipient_langcode = $recipient->getPreferredLangcode();
$params['recipient'] = $recipient;
}
else {
throw new \RuntimeException($this->t('Unable to determine message recipient.'));
}
// Send email to the recipient(s).
$key_prefix = $message->isPersonal() ? 'user' : 'page';
drupal_mail('contact', $key_prefix . '_mail', $to, $recipient_langcode, $params, $sender->getEmail());
// If requested, send a copy to the user, using the current language.
if ($message->copySender()) {
drupal_mail('contact', $key_prefix . '_copy', $sender->getEmail(), $language_interface->id, $params, $sender->getEmail());
}
// If configured, send an auto-reply, using the current language.
if (!$message->isPersonal() && $contact_form->getReply()) {
// User contact forms do not support an auto-reply message, so this
// message always originates from the site.
drupal_mail('contact', 'page_autoreply', $sender->getEmail(), $language_interface->id, $params);
}
$user = $this->currentUser();
$this->mailHandler->sendMailMessages($message, $user);
$this->flood->register('contact', $this->config('contact.settings')->get('flood.interval'));
if (!$message->isPersonal()) {
$this->logger('contact')->notice('%sender-name (@sender-from) sent an email regarding %contact_form.', array(
'%sender-name' => $sender->getUsername(),
'@sender-from' => $sender->getEmail(),
'%contact_form' => $contact_form->label(),
));
}
else {
$this->logger('contact')->notice('%sender-name (@sender-from) sent %recipient-name an email.', array(
'%sender-name' => $sender->getUsername(),
'@sender-from' => $sender->getEmail(),
'%recipient-name' => $message->getPersonalRecipient()->getUsername(),
));
}
drupal_set_message($this->t('Your message has been sent.'));
// To avoid false error messages caused by flood control, redirect away from
......
This diff is collapsed.
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment