From 5e6f5f9f7e3e15df4056873df60b8be64189e01e Mon Sep 17 00:00:00 2001 From: Sam Minnee Date: Thu, 10 Jan 2013 12:33:20 +1300 Subject: [PATCH] NEW: Allow configuration of send_all_emails_to, ccs_all_emails_to, and bcc_all_emails_to via the config system. --- conf/ConfigureFromEnv.php | 2 +- dev/SapphireTest.php | 3 +- email/Email.php | 66 +++++++++++++++++++++++++++++---------- 3 files changed, 52 insertions(+), 19 deletions(-) diff --git a/conf/ConfigureFromEnv.php b/conf/ConfigureFromEnv.php index a994f2880..5fc8a6461 100644 --- a/conf/ConfigureFromEnv.php +++ b/conf/ConfigureFromEnv.php @@ -105,7 +105,7 @@ if(defined('SS_DATABASE_USERNAME') && defined('SS_DATABASE_PASSWORD')) { } if(defined('SS_SEND_ALL_EMAILS_TO')) { - Email::send_all_emails_to(SS_SEND_ALL_EMAILS_TO); + Config::inst()->update("Email","send_all_emails_to", SS_SEND_ALL_EMAILS_TO); } if(defined('SS_DEFAULT_ADMIN_USERNAME')) { diff --git a/dev/SapphireTest.php b/dev/SapphireTest.php index efeb8f842..ab555b952 100644 --- a/dev/SapphireTest.php +++ b/dev/SapphireTest.php @@ -206,7 +206,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase { $className = get_class($this); $fixtureFile = eval("return {$className}::\$fixture_file;"); $prefix = defined('SS_DATABASE_PREFIX') ? SS_DATABASE_PREFIX : 'ss_'; - + // Todo: this could be a special test model $this->model = DataModel::inst(); @@ -263,6 +263,7 @@ class SapphireTest extends PHPUnit_Framework_TestCase { $this->originalMailer = Email::mailer(); $this->mailer = new TestMailer(); Email::set_mailer($this->mailer); + Config::inst()->remove('Email', 'send_all_emails_to'); Email::send_all_emails_to(null); // Preserve memory settings diff --git a/email/Email.php b/email/Email.php index 0a95099be..9c82f014a 100644 --- a/email/Email.php +++ b/email/Email.php @@ -125,16 +125,43 @@ class Email extends ViewableData { static $admin_email_address = ''; /** + * Send every email generated by the Email class to the given address. + * + * It will also add " [addressed to (email), cc to (email), bcc to (email)]" to the end of the subject line + * + * To set this, set Email.send_all_emails_to in your yml config file. + * It can also be set in _ss_environment.php with SS_SEND_ALL_EMAILS_TO. + * * @param string $send_all_emails_to Email-Address */ protected static $send_all_emails_to = null; /** + * BCC every email generated by the Email class to the given address. + * It won't affect the original delivery in the same way that send_all_emails_to does. It just adds a BCC header + * with the given email address. Note that you can only call this once - subsequent calls will overwrite the + * configuration variable. + * + * This can be used when you have a system that relies heavily on email and you want someone to be checking all + * correspondence. + * + * To set this, set Email.bcc_all_emails_to in your yml config file. + * * @param string $bcc_all_emails_to Email-Address */ protected static $bcc_all_emails_to = null; /** + * CC every email generated by the Email class to the given address. + * It won't affect the original delivery in the same way that send_all_emails_to does. It just adds a CC header + * with the given email address. Note that you can only call this once - subsequent calls will overwrite the + * configuration variable. + * + * This can be used when you have a system that relies heavily on email and you want someone to be checking all + * correspondence. + * + * To set this, set Email.cc_all_emails_to in your yml config file. + * * @param string $cc_all_emails_to Email-Address */ protected static $cc_all_emails_to = null; @@ -400,30 +427,32 @@ class Email extends ViewableData { $to = $this->to; $subject = $this->subject; - if(self::$send_all_emails_to) { + if($sendAllTo = $this->config()->send_all_emails_to) { $subject .= " [addressed to $to"; - $to = self::$send_all_emails_to; + $to = $sendAllTo; if($this->cc) $subject .= ", cc to $this->cc"; if($this->bcc) $subject .= ", bcc to $this->bcc"; $subject .= ']'; + unset($headers['Cc']); + unset($headers['Bcc']); } else { if($this->cc) $headers['Cc'] = $this->cc; if($this->bcc) $headers['Bcc'] = $this->bcc; } - if(self::$cc_all_emails_to) { + if($ccAllTo = $this->config()->cc_all_emails_to) { if(!empty($headers['Cc']) && trim($headers['Cc'])) { - $headers['Cc'] .= ', ' . self::$cc_all_emails_to; + $headers['Cc'] .= ', ' . $ccAllTo; } else { - $headers['Cc'] = self::$cc_all_emails_to; + $headers['Cc'] = $ccAllTo; } } - if(self::$bcc_all_emails_to) { + if($bccAllTo = $this->config()->bcc_all_emails_to) { if(!empty($headers['Bcc']) && trim($headers['Bcc'])) { - $headers['Bcc'] .= ', ' . self::$bcc_all_emails_to; + $headers['Bcc'] .= ', ' . $bccAllTo; } else { - $headers['Bcc'] = self::$bcc_all_emails_to; + $headers['Bcc'] = $bccAllTo; } } @@ -459,34 +488,37 @@ class Email extends ViewableData { if(project()) $headers['X-SilverStripeSite'] = project(); + $to = $this->to; $subject = $this->subject; - if(self::$send_all_emails_to) { + if($sendAllTo = $this->config()->send_all_emails_to) { $subject .= " [addressed to $to"; - $to = self::$send_all_emails_to; + $to = $sendAllTo; if($this->cc) $subject .= ", cc to $this->cc"; if($this->bcc) $subject .= ", bcc to $this->bcc"; $subject .= ']'; unset($headers['Cc']); unset($headers['Bcc']); + } else { if($this->cc) $headers['Cc'] = $this->cc; if($this->bcc) $headers['Bcc'] = $this->bcc; } - if(self::$cc_all_emails_to) { + + if($ccAllTo = $this->config()->cc_all_emails_to) { if(!empty($headers['Cc']) && trim($headers['Cc'])) { - $headers['Cc'] .= ', ' . self::$cc_all_emails_to; + $headers['Cc'] .= ', ' . $ccAllTo; } else { - $headers['Cc'] = self::$cc_all_emails_to; + $headers['Cc'] = $ccAllTo; } } - - if(self::$bcc_all_emails_to) { + + if($bccAllTo = $this->config()->bcc_all_emails_to) { if(!empty($headers['Bcc']) && trim($headers['Bcc'])) { - $headers['Bcc'] .= ', ' . self::$bcc_all_emails_to; + $headers['Bcc'] .= ', ' . $bccAllTo; } else { - $headers['Bcc'] = self::$bcc_all_emails_to; + $headers['Bcc'] = $bccAllTo; } }