Skip to content

Commit

Permalink
Merge pull request #764 from jenlampton/305-mail-from
Browse files Browse the repository at this point in the history
Issue #305: Set fixed From and add Reply-to to improve deliverability of email.
  • Loading branch information
quicksketch committed May 8, 2015
2 parents 2fecd82 + 713fc4c commit 31d9995
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 16 deletions.
23 changes: 11 additions & 12 deletions core/includes/mail.inc
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
<?php

/**
* @file
* API functions for processing and sending e-mail.
Expand Down Expand Up @@ -102,8 +101,8 @@ define('MAIL_LINE_ENDINGS', isset($_SERVER['WINDIR']) || (isset($_SERVER['SERVER
* Language object to use to compose the e-mail.
* @param $params
* Optional parameters to build the e-mail.
* @param $from
* Sets From to this value, if given.
* @param string $reply
* Optional e-mail address to be used to answer.
* @param $send
* If TRUE, backdrop_mail() will call backdrop_mail_system()->mail() to deliver
* the message, and store the result in $message['result']. Modules
Expand All @@ -117,20 +116,20 @@ define('MAIL_LINE_ENDINGS', isset($_SERVER['WINDIR']) || (isset($_SERVER['SERVER
* 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 backdrop_mail($module, $key, $to, $language, $params = array(), $from = NULL, $send = TRUE) {
$site_mail = config_get('system.core', 'site_mail');
function backdrop_mail($module, $key, $to, $language, $params = array(), $reply = NULL, $send = TRUE) {
$from = config_get('system.core', 'site_mail');
if (empty($site_mail)) {
$site_mail = ini_get('sendmail_from');
$from = ini_get('sendmail_from');
}
$default_from = $site_mail;

// Bundle up the variables into a structured array for altering.
$message = array(
'id' => $module . '_' . $key,
'module' => $module,
'key' => $key,
'to' => $to,
'from' => isset($from) ? $from : $default_from,
'from' => $from,
'reply-to' => isset($reply) ? $reply : $from,
'language' => $language,
'params' => $params,
'send' => TRUE,
Expand All @@ -145,14 +144,14 @@ function backdrop_mail($module, $key, $to, $language, $params = array(), $from =
'Content-Transfer-Encoding' => '8Bit',
'X-Mailer' => 'Backdrop CMS'
);
if ($default_from) {
if ($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['From'] = $headers['Sender'] = $headers['Return-Path'] = $default_from;
$headers['From'] = $headers['Sender'] = $headers['Return-Path'] = $from;
}
if ($from) {
$headers['From'] = $from;
if ($reply) {
$headers['Reply-to'] = $reply;
}
$message['headers'] = $headers;

Expand Down
8 changes: 4 additions & 4 deletions core/modules/contact/tests/contact.test
Original file line number Diff line number Diff line change
Expand Up @@ -166,15 +166,15 @@ class ContactSitewideTestCase extends BackdropWebTestCase {
$bar_autoreply = $this->randomName(40);
$this->addCategory('foo', 'foo@example.com', $foo_autoreply, FALSE);
$this->addCategory('bar', 'bar@example.com', $bar_autoreply, FALSE);
$this->addCategory('no_autoreply', 'bar@example.com', '', FALSE);
$this->addCategory('no_autoreply', 'no_autoreply@example.com', '', FALSE);

// Test the auto-reply for category 'foo'.
$email = $this->randomName(32) . '@example.com';
$subject = $this->randomName(64);
$this->submitContact($this->randomName(16), $email, $subject, 2, $this->randomString(128));

// We are testing the auto-reply, so there should be one e-mail going to the sender.
$captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'foo@example.com'));
$captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'reply-to' => 'foo@example.com'));
$this->assertEqual(count($captured_emails), 1, 'Auto-reply e-mail was sent to the sender for category "foo".', 'Contact');
$this->assertEqual($captured_emails[0]['body'], backdrop_html_to_text($foo_autoreply), 'Auto-reply e-mail body is correct for category "foo".', 'Contact');

Expand All @@ -183,14 +183,14 @@ class ContactSitewideTestCase extends BackdropWebTestCase {
$this->submitContact($this->randomName(16), $email, $this->randomString(64), 3, $this->randomString(128));

// Auto-reply for category 'bar' should result in one auto-reply e-mail to the sender.
$captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'bar@example.com'));
$captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'reply-to' => 'bar@example.com'));
$this->assertEqual(count($captured_emails), 1, 'Auto-reply e-mail was sent to the sender for category "bar".', 'Contact');
$this->assertEqual($captured_emails[0]['body'], backdrop_html_to_text($bar_autoreply), 'Auto-reply e-mail body is correct for category "bar".', 'Contact');

// Verify that no auto-reply is sent when the auto-reply field is left blank.
$email = $this->randomName(32) . '@example.com';
$this->submitContact($this->randomName(16), $email, $this->randomString(64), 4, $this->randomString(128));
$captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'from' => 'no_autoreply@example.com'));
$captured_emails = $this->backdropGetMails(array('id' => 'contact_page_autoreply', 'to' => $email, 'reply-to' => 'no_autoreply@example.com'));
$this->assertEqual(count($captured_emails), 0, 'No auto-reply e-mail was sent to the sender for category "no-autoreply".', 'Contact');
}

Expand Down

0 comments on commit 31d9995

Please sign in to comment.