diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/CakeEmail.php index 8de03b18212..fd8161fd80a 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 @@ public function transport($name = null) { 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 @@ public function config($config = null) { 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 @@ public function send($content = null) { $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 @@ public function reset() { $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 9e6a59139b3..a655eaded3d 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 @@ public function __construct($config = array()) { */ 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 cec2cfb6139..aba0bd9f2fe 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 @@ public function send(CakeEmail $email) { 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 *