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/MailHandlerInterface.php b/core/modules/contact/src/MailHandlerInterface.php index 516e92a..5b03fec 100644 --- a/core/modules/contact/src/MailHandlerInterface.php +++ b/core/modules/contact/src/MailHandlerInterface.php @@ -29,6 +29,11 @@ * * @throws \Drupal\contact\MailHandlerException * When unable to determine message recipient. + * + * @return array + * Array with keys of MailHandler::MESSAGE_RECIPIENT and optionally + * MailHandler::MESSAGE_COPY if a copy is sent, containing the boolean + * result of the mails being sent. */ public function sendMailMessages(MessageInterface $message, AccountInterface $sender); diff --git a/core/modules/contact/src/MessageForm.php b/core/modules/contact/src/MessageForm.php index 78de6dd..5b40b55 100644 --- a/core/modules/contact/src/MessageForm.php +++ b/core/modules/contact/src/MessageForm.php @@ -212,10 +212,19 @@ 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); + + // \Drupal\Core\Mail\MailManager will display an error message if the + // message couldn't be sent, so only set a message if it was sent. + if (!empty($message_results[MailHandler::MESSAGE_RECIPIENT]['result'])) { + drupal_set_message($this->t('Your message has been sent.')); + } + + if (!empty($message_results[MailHandler::MESSAGE_COPY]['result'])) { + drupal_set_message($this->t('Your message copy has been sent.')); + } $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. diff --git a/core/modules/contact/src/Tests/ContactEmailFailSendTest.php b/core/modules/contact/src/Tests/ContactEmailFailSendTest.php new file mode 100644 index 0000000..d77c99f --- /dev/null +++ b/core/modules/contact/src/Tests/ContactEmailFailSendTest.php @@ -0,0 +1,75 @@ +admin_user = $this->drupalCreateUser(['access site-wide contact form', 'administer contact forms', 'administer permissions', 'administer users']); + + $this->drupalLogin($this->admin_user); + // Create contact form. + $edit = [ + 'label' => $this->randomMachineName(16), + 'id' => Unicode::strtolower($this->randomMachineName(16)), + 'recipients' => 'test@example.com', + 'reply' => '', + 'selected' => TRUE, + ]; + $this->drupalPostForm('admin/structure/contact/add', $edit, t('Save')); + $this->assertRaw(t('Contact form %label has been added.', ['%label' => $edit['label']])); + + // Make sure that we can't send mails. + $this->config('system.mail')->set('interface.default', 'test_php_mail_failure')->save(); + + // Submit the contact form as anonymous user. + $this->drupalLogout(); + $this->drupalGet('/contact'); + user_role_grant_permissions(DRUPAL_ANONYMOUS_RID, ['access site-wide contact form']); + $edit2 = [ + 'name' => 'Test', + 'mail' => 'test@example.com', + 'subject[0][value]' => 'Sample Message', + 'message[0][value]' => 'Hallo world', + ]; + $this->drupalPostForm('contact/' . $edit['id'], $edit2, t('Send message')); + + // Unable to send email message should be displayed + $this->assertText(t('Unable to send email. Contact the site administrator if the problem persists.')); + // Make sure we don't print a status message about the e-mails being sent. + $errors = $this->xpath('//div[contains(@class, "messages--status")]'); + $this->assertFalse($errors, 'No action message shown.'); + } + +} diff --git a/sites/default/default.services.yml b/sites/default/default.services.yml old mode 100644 new mode 100755 diff --git a/sites/default/default.settings.php b/sites/default/default.settings.php old mode 100644 new mode 100755