Commit 8e92d3b1 authored by alexpott's avatar alexpott

Issue #111702 by mvc, penyaskito, naxoc, jenlampton, montesq: Set fixed...

Issue #111702 by mvc, penyaskito, naxoc, jenlampton, montesq: Set fixed "from:" and add "Reply-to:" to improve deliverability of Drupal mail.
parent cc7b115e
......@@ -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');
}
}
......
......@@ -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'),
......
......@@ -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.');
}
/**
......
......@@ -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');
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment