Skip to content
Browse files

NEW: Allow configuration of send_all_emails_to, ccs_all_emails_to, an…

…d bcc_all_emails_to via the config system.
  • Loading branch information...
1 parent 7026a48 commit 5e6f5f9f7e3e15df4056873df60b8be64189e01e @sminnee sminnee committed with chillu
Showing with 52 additions and 19 deletions.
  1. +1 −1 conf/ConfigureFromEnv.php
  2. +2 −1 dev/SapphireTest.php
  3. +49 −17 email/Email.php
View
2 conf/ConfigureFromEnv.php
@@ -105,7 +105,7 @@
}
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')) {
View
3 dev/SapphireTest.php
@@ -206,7 +206,7 @@ public function setUp() {
$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 @@ public function setUp() {
$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
View
66 email/Email.php
@@ -125,16 +125,43 @@ public static function mailer() {
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 @@ public function sendPlain($messageID = null) {
$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 @@ public function send($messageID = null) {
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;
}
}

0 comments on commit 5e6f5f9

Please sign in to comment.
Something went wrong with that request. Please try again.