diff --git a/core/includes/mail.inc b/core/includes/mail.inc index e9c87f66485a153b0f8f387d16caf4e2a484d7c8..1f7897ea505abeae7fc08c45c2f2d7b060fb193e 100644 --- a/core/includes/mail.inc +++ b/core/includes/mail.inc @@ -119,7 +119,8 @@ * 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) { - $site_mail = config('system.site')->get('mail'); + $site_config = config('system.site'); + $site_mail = $site_config->get('mail'); if (empty($site_mail)) { $site_mail = ini_get('sendmail_from'); } @@ -150,9 +151,10 @@ function drupal_mail($module, $key, $to, $langcode, $params = array(), $from = N // 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['From'] = $headers['Sender'] = $headers['Return-Path'] = $default_from; + $headers['Sender'] = $headers['Return-Path'] = $default_from; + $headers['From'] = $site_config->get('name') . ' <' . $default_from . '>'; } - if ($from) { + if ($from && $from != $default_from) { $headers['From'] = $from; } $message['headers'] = $headers; 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 92be196ed672dd0c7243a3c6aed1399ec8d9fb2d..bb72d293075ea1634b3d9850612482d2f3fa9033 100644 --- a/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php +++ b/core/modules/system/lib/Drupal/system/Tests/Mail/MailTest.php @@ -76,6 +76,27 @@ public function testCancelMessage() { $this->assertNull(self::$sent_message, 'Message was canceled.'); } + /** + * Checks for the site name in an auto-generated From: header. + */ + function testFromHeader() { + 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 + // default sender e-mail. + $this->assertEqual($from_email, self::$sent_message['headers']['From']); + + 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']); + } + /** * Concatenate and wrap the e-mail body for plain-text mails. *