From 3200e380d3abc53059a1916647bb9f6bc7c06763 Mon Sep 17 00:00:00 2001 From: Juan Basso Date: Wed, 13 Apr 2011 00:04:37 -0400 Subject: [PATCH] Created a new method to return the transport class before send. In some cases you need to do extra calls/configurations before send. --- lib/Cake/Network/CakeEmail.php | 48 ++++++++++++++++---- lib/Cake/Network/Email/AbstractTransport.php | 22 +++++---- lib/Cake/Network/Email/SmtpTransport.php | 28 ++++++++---- 3 files changed, 69 insertions(+), 29 deletions(-) diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/CakeEmail.php index 8de03b182..fd8161fd8 100644 --- a/lib/Cake/Network/CakeEmail.php +++ b/lib/Cake/Network/CakeEmail.php @@ -193,6 +193,13 @@ class CakeEmail { */ protected $_transportName = 'mail'; +/** + * Instance of tranport class + * + * @var object + */ + protected $_transportClass = null; + /** * charset the email is sent in * @@ -671,9 +678,32 @@ class CakeEmail { return $this->_transportName; } $this->_transportName = (string)$name; + $this->_transportClass = null; return $this; } +/** + * Return the transport class + * + * @return object + * @thrown SocketException + */ + public function transportClass() { + if ($this->_transportClass) { + return $this->_transportClass; + } + list($plugin, $transportClassname) = pluginSplit($this->_transportName, true); + $transportClassname .= 'Transport'; + App::uses($transportClassname, $plugin . 'Network/Email'); + if (!class_exists($transportClassname)) { + throw new SocketException(__d('cake', 'Class "%s" not found.', $transportClassname)); + } elseif (!method_exists($transportClassname, 'send')) { + throw new SocketException(__d('cake', 'The "%s" do not have send method.', $transportClassname)); + } + + return $this->_transportClass = new $transportClassname(); + } + /** * Message-ID * @@ -755,11 +785,17 @@ class CakeEmail { if (empty($config)) { return $this->_config; } + if (is_array($config)) { $this->_config = $config; } else { $this->_config = (string)$config; } + + if ($this->_transportClass) { + $this->_transportClass->config($this->_config); + } + return $this; } @@ -828,16 +864,9 @@ class CakeEmail { $this->_message[] = ''; } - list($plugin, $transportClassname) = pluginSplit($this->_transportName, true); - $transportClassname .= 'Transport'; - App::uses($transportClassname, $plugin . 'Network/Email'); - if (!class_exists($transportClassname)) { - throw new SocketException(__d('cake', 'Class "%s" not found.', $transportClassname)); - } elseif (!method_exists($transportClassname, 'send')) { - throw new SocketException(__d('cake', 'The "%s" do not have send method.', $transportClassname)); - } + $transport = $this->transportClass(); + $transport->config($config); - $transport = new $transportClassname($config); return $transport->send($this); } @@ -865,6 +894,7 @@ class CakeEmail { $this->_message = ''; $this->_emailFormat = 'text'; $this->_transportName = 'mail'; + $this->_transportClass = null; $this->_attachments = array(); $this->_config = 'default'; return $this; diff --git a/lib/Cake/Network/Email/AbstractTransport.php b/lib/Cake/Network/Email/AbstractTransport.php index 9e6a59139..a655eaded 100644 --- a/lib/Cake/Network/Email/AbstractTransport.php +++ b/lib/Cake/Network/Email/AbstractTransport.php @@ -31,16 +31,6 @@ abstract class AbstractTransport { */ protected $_config = array(); -/** - * Constructor - * - */ - public function __construct($config = array()) { - if (!empty($config)) { - $this->_config = $config; - } - } - /** * Send mail * @@ -49,6 +39,18 @@ abstract class AbstractTransport { */ abstract public function send(CakeEmail $email); +/** + * Set the config + * + * @param array $config + * @return object $this + */ + public function config($config = array()) { + if (!empty($config)) { + $this->_config = $config; + } + } + /** * Help to convert headers in string * diff --git a/lib/Cake/Network/Email/SmtpTransport.php b/lib/Cake/Network/Email/SmtpTransport.php index cec2cfb61..aba0bd9f2 100644 --- a/lib/Cake/Network/Email/SmtpTransport.php +++ b/lib/Cake/Network/Email/SmtpTransport.php @@ -47,16 +47,6 @@ class SmtpTransport extends AbstractTransport { * @thrown SocketException */ public function send(CakeEmail $email) { - $config = array( - 'host' => 'localhost', - 'port' => 25, - 'timeout' => 30, - 'username' => null, - 'password' => null, - 'client' => null - ); - $this->_config += $config; - $this->_cakeEmail = $email; $this->_connect(); @@ -68,6 +58,24 @@ class SmtpTransport extends AbstractTransport { return true; } +/** + * Set the configuration + * + * @param array $config + * @return object $this + */ + public function config($config = array()) { + $default = array( + 'host' => 'localhost', + 'port' => 25, + 'timeout' => 30, + 'username' => null, + 'password' => null, + 'client' => null + ); + $this->_config = $config + $default; + } + /** * Connect to SMTP Server *