diff --git a/core/modules/contact/src/MailHandler.php b/core/modules/contact/src/MailHandler.php index 308acf5..67646b0 100644 --- a/core/modules/contact/src/MailHandler.php +++ b/core/modules/contact/src/MailHandler.php @@ -23,6 +23,16 @@ class MailHandler implements MailHandlerInterface { use StringTranslationTrait; /** + * Index to identify the result of the message recipient. + */ + const MESSAGE_RECIPIENT = 'recipient'; + + /** + * Index to identify the result of the message copy. + */ + const MESSAGE_COPY = 'copy'; + + /** * Language manager service. * * @var \Drupal\Core\Language\LanguageManagerInterface @@ -116,15 +126,15 @@ public function sendMailMessages(MessageInterface $message, AccountInterface $se // 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()); + $message_results[MailHandler::MESSAGE_RECIPIENT] = $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 ($message->copySender() && $message_results[MailHandler::MESSAGE_RECIPIENT]['result']) { + $message_results[MailHandler::MESSAGE_COPY] = $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()) { + if (!$message->isPersonal() && $contact_form->getReply() && $message_results[MailHandler::MESSAGE_RECIPIENT]['result']) { // 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); @@ -144,6 +154,8 @@ public function sendMailMessages(MessageInterface $message, AccountInterface $se '%recipient-name' => $message->getPersonalRecipient()->getUsername(), )); } + + return $message_results; } } diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 78de6dd..1de2b8e 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -212,10 +212,25 @@ public function validate(array $form, FormStateInterface $form_state) { public function save(array $form, FormStateInterface $form_state) { $message = $this->entity; $user = $this->currentUser(); - $this->mailHandler->sendMailMessages($message, $user); + $message_results = $this->mailHandler->sendMailMessages($message, $user); + + if ($message_results[MailHandler::MESSAGE_RECIPIENT]['result']) { + drupal_set_message($this->t('Your message has been sent.')); + } + else { + drupal_set_message($this->t('Your message has not been sent.'), 'error'); + } + + if (isset($message_results[MailHandler::MESSAGE_COPY])) { + if (!$message_results[MailHandler::MESSAGE_COPY]['result']) { + drupal_set_message($this->t('Your message copy has been sent.')); + } + else { + drupal_set_message($this->t('Your message copy has not been sent.'), 'error'); + } + } $this->flood->register('contact', $this->config('contact.settings')->get('flood.interval')); - drupal_set_message($this->t('Your message has been sent.')); // To avoid false error messages caused by flood control, redirect away from // the contact form; either to the contacted user account or the front page.