Issue #111702 by mvc, penyaskito, naxoc, jenlampton, montesq: Set fixed "from:" and add "Reply-to:" to improve deliverability of Drupal mail.

8.0.x
Alex Pott 2013-12-31 00:35:14 +01:00
parent cc7b115ed8
commit 8e92d3b1f1
4 changed files with 30 additions and 25 deletions

View File

@ -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');
}
}

View File

@ -73,7 +73,8 @@ class ContactPersonalTest extends WebTestBase {
$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'),

View File

@ -78,24 +78,29 @@ class MailTest extends WebTestBase implements MailInterface {
}
/**
* 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.');
}
/**

View File

@ -159,7 +159,8 @@ class UserAdminTest extends WebTestBase {
// 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');