Skip to content

Commit

Permalink
Get Mailer from: host name from variety of sources depending on serve…
Browse files Browse the repository at this point in the history
…r configuration
  • Loading branch information
ginatrapani committed Dec 26, 2013
1 parent 3c053c4 commit b51731d
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 26 deletions.
16 changes: 14 additions & 2 deletions tests/TestOfMailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
require_once THINKUP_WEBAPP_PATH.'_lib/extlib/simpletest/autorun.php';
require_once THINKUP_WEBAPP_PATH.'config.inc.php';

class TestOfMailer extends ThinkUpBasicUnitTestCase {
class TestOfMailer extends ThinkUpUnitTestCase {

public function setUp() {
parent::setUp();
Expand Down Expand Up @@ -59,12 +59,24 @@ public function testFromName() {
$email_body);

$config->setValue("app_title_prefix", "My Other Installation of ");
$_SERVER['HTTP_HOST'] = "my_other_hostname";
$_SERVER['HTTP_HOST'] = null;
$_SERVER['SERVER_NAME'] = "my_other_hostname";
Mailer::mail('you@example.com', 'Testing 123', 'Me worky, yo?');
$email_body = Mailer::getLastMail();
$this->debug($email_body);
$this->assertPattern('/From: "My Other Installation of ThinkUp" <notifications@my_other_hostname>/',
$email_body);

$config->setValue("app_title_prefix", "Yet Another Installation of ");
$_SERVER['HTTP_HOST'] = null;
$_SERVER['SERVER_NAME'] = null;
$builder = FixtureBuilder::build('options', array('namespace'=>'application_options',
'option_name'=>'server_name', 'option_value'=>'testservername') );
Mailer::mail('you@example.com', 'Testing 123', 'Me worky, yo?');
$email_body = Mailer::getLastMail();
$this->debug($email_body);
$this->assertPattern('/From: "Yet Another Installation of ThinkUp" <notifications@testservername>/',
$email_body);
}

public function testMandrill() {
Expand Down
23 changes: 23 additions & 0 deletions tests/TestOfUtils.php
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,29 @@ public function testGetSiteRootPathFromFileSystem() {
$this->assertEqual($filesystem_site_root_path, $cfg_site_root_path);
}

public function testGetApplicationHostName() {
//no $_SERVER vars set, but with application setting set
$builder = FixtureBuilder::build('options', array('namespace'=>'application_options',
'option_name'=>'server_name', 'option_value'=>'testservername') );
$host_name = Utils::getApplicationHostName();
$expected_host_name = 'testservername';
$this->assertEqual($host_name, $expected_host_name);

//SERVER_NAME, not HTTP_HOST
$_SERVER['HTTP_HOST'] = null;
$_SERVER['SERVER_NAME'] = 'mytestservername';
$host_name = Utils::getApplicationHostName();
$expected_host_name = 'mytestservername';
$this->assertEqual($host_name, $expected_host_name);

//HTTP_HOST, not SERVER_NAME
$_SERVER['HTTP_HOST'] = 'myothertestservername';
$_SERVER['SERVER_NAME'] = null;
$host_name = Utils::getApplicationHostName();
$expected_host_name = 'myothertestservername';
$this->assertEqual($host_name, $expected_host_name);
}

public function testGetApplicationURL() {
$cfg = Config::getInstance();
$cfg->setValue('site_root_path', '/my/path/to/thinkup/');
Expand Down
21 changes: 5 additions & 16 deletions webapp/_lib/class.Mailer.php
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ public static function mail($to, $subject, $message) {
*/
public static function mailHTMLViaMandrillTemplate($to, $subject, $template_name, $template_params) {
$config = Config::getInstance();
$host = self::getHost();
$host = Utils::getApplicationHostName();
$app_title = $config->getValue('app_title_prefix'). "ThinkUp";
$mandrill_api_key = $config->getValue('mandrill_api_key');

Expand Down Expand Up @@ -92,19 +92,8 @@ public static function mailHTMLViaMandrillTemplate($to, $subject, $template_name
// We want to be able to handle this specific error differently.
throw $unknown_template_error;
} catch (Mandrill_Error $e) {
throw new Exception('An error occurred while sending email via Mandrill. ' . get_class($e) .
': ' . $e->getMessage());
}
}
/**
* Return the current host's name, ie, $_SERVER['HTTP_HOST'] if it is set.
* @return str Host name
*/
private static function getHost() {
if (isset($_SERVER['HTTP_HOST'])) {
return $_SERVER['HTTP_HOST'];
} else {
return "";
throw new Exception('An error occurred while sending email to '.$to.' from '.$from.' via Mandrill. '
. get_class($e) . ': ' . $e->getMessage());
}
}
/**
Expand Down Expand Up @@ -142,7 +131,7 @@ public static function mailViaPHP($to, $subject, $message) {
$config = Config::getInstance();

$app_title = $config->getValue('app_title_prefix'). "ThinkUp";
$host = self::getHost();
$host = Utils::getApplicationHostName();

$mail_header = "From: \"{$app_title}\" <notifications@{$host}>\r\n";
$mail_header .= "X-Mailer: PHP/".phpversion();
Expand All @@ -168,7 +157,7 @@ public static function mailViaMandrill($to, $subject, $message) {
$config = Config::getInstance();

$app_title = $config->getValue('app_title_prefix') . "ThinkUp";
$host = self::getHost();
$host = Utils::getApplicationHostName();
$mandrill_api_key = $config->getValue('mandrill_api_key');

try {
Expand Down
25 changes: 17 additions & 8 deletions webapp/_lib/class.Utils.php
Original file line number Diff line number Diff line change
Expand Up @@ -294,31 +294,40 @@ public static function getSiteRootPathFromFileSystem() {
}

/**
* Get the application's full URL, i.e., https://example.com/thinkup/
* @param $replace_localhost_with_ip Default to false
* @return str
* Get the application's host name or server name, i.e., example.com.
* @return str Host name either set by PHP global vars or stored in the database
*/
public static function getApplicationURL($replace_localhost_with_ip = false) {
public static function getApplicationHostName() {
//First attempt to get the host name without querying the database
//Try SERVER_NAME
$server = empty($_SERVER['SERVER_NAME']) ? '' : $_SERVER['SERVER_NAME'];
//Try HTTP_HOST
//Second, try HTTP_HOST
if ($server == '' ) {
$server = empty($_SERVER['HTTP_HOST']) ? '' : $_SERVER['HTTP_HOST'];
}
//Then fall back to stored application setting set by Installer::storeServerName
//Finally fall back to stored application setting set by Installer::storeServerName
if ($server == '') {
$option_dao = DAOFactory::getDAO('OptionDAO');
$server_app_setting = $option_dao->getOptionByName(OptionDAO::APP_OPTIONS, 'server_name');
if (isset($server_app_setting)) {
$server = $server_app_setting->option_value;
}
}
//domain name is always lowercase
$server = strtolower($server);
return $server;
}

/**
* Get the application's full URL, i.e., https://example.com/thinkup/
* @param $replace_localhost_with_ip Default to false
* @return str
*/
public static function getApplicationURL($replace_localhost_with_ip = false) {
$server = self::getApplicationHostName();
if ($replace_localhost_with_ip) {
$server = ($server == 'localhost')?'127.0.0.1':$server;
}
//domain name is always lowercase
$server = strtolower($server);
$site_root_path = Config::getInstance()->getValue('site_root_path');
if (!isset($site_root_path)) { //config file not written yet (during install)
$site_root_path = self::getSiteRootPathFromFileSystem();
Expand Down

0 comments on commit b51731d

Please sign in to comment.