Skip to content

Commit

Permalink
Created a new method to return the transport class before send. In so…
Browse files Browse the repository at this point in the history
…me cases you need to do extra calls/configurations before send.
  • Loading branch information
jrbasso committed Apr 13, 2011
1 parent f4f3bfe commit 3200e38
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 29 deletions.
48 changes: 39 additions & 9 deletions lib/Cake/Network/CakeEmail.php
Expand Up @@ -193,6 +193,13 @@ class CakeEmail {
*/
protected $_transportName = 'mail';

/**
* Instance of tranport class
*
* @var object
*/
protected $_transportClass = null;

/**
* charset the email is sent in
*
Expand Down Expand Up @@ -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
*
Expand Down Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
}

Expand Down Expand Up @@ -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;
Expand Down
22 changes: 12 additions & 10 deletions lib/Cake/Network/Email/AbstractTransport.php
Expand Up @@ -31,16 +31,6 @@ abstract class AbstractTransport {
*/
protected $_config = array();

/**
* Constructor
*
*/
public function __construct($config = array()) {
if (!empty($config)) {
$this->_config = $config;
}
}

/**
* Send mail
*
Expand All @@ -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
*
Expand Down
28 changes: 18 additions & 10 deletions lib/Cake/Network/Email/SmtpTransport.php
Expand Up @@ -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();
Expand All @@ -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
*
Expand Down

0 comments on commit 3200e38

Please sign in to comment.