Browse Source

Email log/live repaired

repaired live switch (now Config.live)
reimplemented live switch function according to documentation (as in
cake 2.x)
reimplemented logReport option (reimplemented _logEmail() as in cake
2.x with some changes)
Bernhard Picher 10 years ago
parent
commit
b77cfaa531
2 changed files with 36 additions and 10 deletions
  1. 2 2
      docs/Network/Email.md
  2. 34 8
      src/Network/Email/Email.php

+ 2 - 2
docs/Network/Email.md

@@ -12,7 +12,7 @@ An enhanced class to
 - Basic validation supported.
 - Quick way to send system emails/reports.
 - Extensive logging and error tracing as well as debugging using.
-- Don't send emails without Configure::write('Email.live'), but log them away verbosely. For testing.
+- Don't send emails without Configure::write('Config.live'), but log them away verbosely. For testing.
 - Security measure: Don't send emails to actual addresses in debug mode, they will be sent to Configure::read('Config.adminEmail') instead. Same for cc/bcc.
 
 
@@ -98,4 +98,4 @@ $email->addEmbeddedBlobAttachment($somePngFileBlobContent, 'my_filename.png');
 You can use `'Config.xMailer'` config to set a custom xMailer header.
 Priority and line length can also be adjusted if needed.
 
-By default it switches to "Debug" transport in debug mode. If you don't want that set Configure value `Email.ive` to true.
+By default it switches to "Debug" transport in debug mode. If you don't want that set Configure value `Config.live` to true.

+ 34 - 8
src/Network/Email/Email.php

@@ -6,9 +6,13 @@ use Cake\Network\Email\Email as CakeEmail;
 use Tools\Utility\Text;
 use InvalidArgumentException;
 use Tools\Utility\Mime;
+use Cake\Log\LogTrait;
+use Psr\Log\LogLevel;
 
 class Email extends CakeEmail {
 
+	use LogTrait;
+
 	protected $_wrapLength = null;
 
 	protected $_priority = null;
@@ -369,7 +373,7 @@ class Email extends CakeEmail {
 			'cc' => $this->_cc,
 			'subject' => $this->_subject,
 			'bcc' => $this->_bcc,
-			'transport' => $this->_transport
+			'transport' => get_class($this->_transport),
 		];
 		if ($this->_priority) {
 			$this->_headers['X-Priority'] = $this->_priority;
@@ -377,12 +381,20 @@ class Email extends CakeEmail {
 			//$this->_headers['Importance'] = 'High';
 		}
 
+		// if not live, just log but do not send any mails
+		if (! Configure::read('Config.live')) {
+        	$this->_logEmail();
+        	return true;
+		}
+
 		// Security measure to not sent to the actual addressee in debug mode while email sending is live
-		if (Configure::read('debug') && Configure::read('Email.live')) {
+		if (Configure::read('debug') && Configure::read('Config.live')) {
 			$adminEmail = Configure::read('Config.adminEmail');
+
 			if (!$adminEmail) {
 				$adminEmail = Configure::read('Config.systemEmail');
 			}
+
 			foreach ($this->_to as $k => $v) {
 				if ($k === $adminEmail) {
 					continue;
@@ -413,20 +425,34 @@ class Email extends CakeEmail {
 			$this->_error .= ' (line ' . $e->getLine() . ' in ' . $e->getFile() . ')' . PHP_EOL .
 				$e->getTraceAsString();
 
-			if (!empty($this->_config['logReport'])) {
-				$this->_logEmail();
-			} else {
-				//Log::write('error', $this->_error);
-			}
+			// always log report
+			$this->_logEmail(LogLevel::ERROR);
+
+			// log error
+			$this->log($this->_error, LogLevel::ERROR);
+			
 			return false;
 		}
 
-		if (!empty($this->_config['logReport'])) {
+		if ( $this->_profile['logReport'] ) {
 			$this->_logEmail();
 		}
+
 		return true;
 	}
 
+	protected function _logEmail($level = LogLevel::INFO)
+	{
+		$content =
+			$this->_log['transport'] .
+			' - ' . 'TO:' . implode(',', array_keys($this->_log['to'])) .
+			'||FROM:' . implode(',', array_keys($this->_log['from'])) .
+			'||REPLY:' . implode(',', array_keys($this->_log['replyTo'])) .
+			'||S:' . $this->_log['subject'];
+
+		$this->log($content, $level);
+	}
+
 	/**
 	 * Attach inline/embedded files to the message.
 	 *