diff --git a/core/includes/mail.inc b/core/includes/mail.inc index dfa3f1d820fe51ae22548f7ebab6f35f2b567b63..61dfb7ef807d2d569e75bd2fc186fc50ca0efd78 100644 --- a/core/includes/mail.inc +++ b/core/includes/mail.inc @@ -96,8 +96,8 @@ * Language code to use to compose the e-mail. * @param array $params * (optional) Parameters to build the e-mail. - * @param string|null $from - * Sets From to this value, if given. + * @param string|null $reply + * Optional e-mail address to be used to answer. * @param bool $send * If TRUE, drupal_mail() will call drupal_mail_system()->mail() to deliver * the message, and store the result in $message['result']. Modules @@ -111,13 +111,12 @@ * written to the watchdog. (Success means nothing more than the message being * accepted at php-level, which still doesn't guarantee it to be delivered.) */ -function drupal_mail($module, $key, $to, $langcode, $params = array(), $from = NULL, $send = TRUE) { +function drupal_mail($module, $key, $to, $langcode, $params = array(), $reply = NULL, $send = TRUE) { $site_config = \Drupal::config('system.site'); $site_mail = $site_config->get('mail'); if (empty($site_mail)) { $site_mail = ini_get('sendmail_from'); } - $default_from = $site_mail; // Bundle up the variables into a structured array for altering. $message = array( @@ -125,7 +124,8 @@ function drupal_mail($module, $key, $to, $langcode, $params = array(), $from = N 'module' => $module, 'key' => $key, 'to' => $to, - 'from' => isset($from) ? $from : $default_from, + 'from' => $site_mail, + 'reply-to' => $reply, 'langcode' => $langcode, 'params' => $params, 'send' => TRUE, @@ -140,15 +140,13 @@ function drupal_mail($module, $key, $to, $langcode, $params = array(), $from = N 'Content-Transfer-Encoding' => '8Bit', 'X-Mailer' => 'Drupal' ); - if ($default_from) { - // To prevent e-mail from looking like spam, the addresses in the Sender and - // Return-Path headers should have a domain authorized to use the originating - // SMTP server. - $headers['Sender'] = $headers['Return-Path'] = $default_from; - $headers['From'] = $site_config->get('name') . ' <' . $default_from . '>'; - } - if ($from && $from != $default_from) { - $headers['From'] = $from; + // To prevent e-mail from looking like spam, the addresses in the Sender and + // Return-Path headers should have a domain authorized to use the + // originating SMTP server. + $headers['Sender'] = $headers['Return-Path'] = $site_mail; + $headers['From'] = $site_config->get('name') . ' <' . $site_mail . '>'; + if ($reply) { + $headers['Reply-to'] = $reply; } $message['headers'] = $headers; @@ -181,7 +179,7 @@ function drupal_mail($module, $key, $to, $langcode, $params = array(), $from = N $message['result'] = $system->mail($message); // Log errors. if (!$message['result']) { - watchdog('mail', 'Error sending e-mail (from %from to %to).', array('%from' => $message['from'], '%to' => $message['to']), WATCHDOG_ERROR); + watchdog('mail', 'Error sending e-mail (from %from to %to with reply-to %reply).', array('%from' => $message['from'], '%to' => $message['to'], '%reply' => $message['reply-to'] ? $message['reply-to'] : t('not set')), WATCHDOG_ERROR); drupal_set_message(t('Unable to send e-mail. Contact the site administrator if the problem persists.'), 'error'); } } diff --git a/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php b/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php index a6ca7f9371e67b180db3220d29d175d3a2778158..c7056a7bca04110abb168088b232ccfe85f9ab59 100644 --- a/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php +++ b/core/modules/contact/lib/Drupal/contact/Tests/ContactPersonalTest.php @@ -73,7 +73,8 @@ function testSendPersonalContactMessage() { $this->assertEqual(1, count($mails)); $mail = $mails[0]; $this->assertEqual($mail['to'], $this->contact_user->getEmail()); - $this->assertEqual($mail['from'], $this->web_user->getEmail()); + $this->assertEqual($mail['from'], \Drupal::config('system.site')->get('mail')); + $this->assertEqual($mail['reply-to'], $this->web_user->getEmail()); $this->assertEqual($mail['key'], 'user_mail'); $variables = array( '!site-name' => \Drupal::config('system.site')->get('name'), diff --git a/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php b/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php index 4326c96def109cedb22aeddcb0dfa2a6768be394..927b7b1e2384e0785ab91a8a3b65c899feec10a5 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php @@ -78,24 +78,29 @@ public function testCancelMessage() { } /** - * Checks for the site name in an auto-generated From: header. + * Checks the From: and Reply-to: headers. */ - function testFromHeader() { + public function testFromAndReplyToHeader() { global $language; // Reset the class variable holding a copy of the last sent message. self::$sent_message = NULL; - // Send an e-mail with a sender address specified. - $from_email = 'someone_else@example.com'; - drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $from_email); - // Test that the from e-mail is just the e-mail and not the site name and + // Send an e-mail with a reply-to address specified. + $from_email = 'Drupal <simpletest@example.com>'; + $reply_email = 'someone_else@example.com'; + drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language, array(), $reply_email); + // Test that the reply-to e-mail is just the e-mail and not the site name and // default sender e-mail. - $this->assertEqual($from_email, self::$sent_message['headers']['From']); + $this->assertEqual($from_email, self::$sent_message['headers']['From'], 'Message is sent from the site email account.'); + $this->assertEqual($reply_email, self::$sent_message['headers']['Reply-to'], 'Message reply-to headers are set.'); + $this->assertFalse(isset(self::$sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); self::$sent_message = NULL; // Send an e-mail and check that the From-header contains the site name. drupal_mail('simpletest', 'from_test', 'from_test@example.com', $language); - $this->assertEqual('Drupal <simpletest@example.com>', self::$sent_message['headers']['From']); + $this->assertEqual($from_email, self::$sent_message['headers']['From'], 'Message is sent from the site email account.'); + $this->assertFalse(isset(self::$sent_message['headers']['Reply-to']), 'Message reply-to is not set if not specified.'); + $this->assertFalse(isset(self::$sent_message['headers']['Errors-To']), 'Errors-to header must not be set, it is deprecated.'); } /** diff --git a/core/modules/user/lib/Drupal/user/Tests/UserAdminTest.php b/core/modules/user/lib/Drupal/user/Tests/UserAdminTest.php index 1f28492d12c1239dcd10dc41687afe364e2efbed..fb58bc13fd7af0947182f3274fbcdc8dcedafa7f 100644 --- a/core/modules/user/lib/Drupal/user/Tests/UserAdminTest.php +++ b/core/modules/user/lib/Drupal/user/Tests/UserAdminTest.php @@ -159,7 +159,8 @@ function testNotificationEmailAddress() { // Notification E-mail address. $user_mail = $this->drupalGetMails(array( 'to' => $edit['mail'], - 'from' => $notify_address, + 'from' => $server_address, + 'reply-to' => $notify_address, 'subject' => $subject, )); $this->assertTrue(count($user_mail), 'New user mail to user is sent from configured Notification E-mail address');