Created a new method to return the transport class before send. In some cases you need to do extra calls/configurations before send.

This commit is contained in:
Juan Basso 2011-04-13 00:04:37 -04:00
parent f4f3bfe2fc
commit 3200e380d3
3 changed files with 69 additions and 29 deletions

View file

@ -193,6 +193,13 @@ class CakeEmail {
*/ */
protected $_transportName = 'mail'; protected $_transportName = 'mail';
/**
* Instance of tranport class
*
* @var object
*/
protected $_transportClass = null;
/** /**
* charset the email is sent in * charset the email is sent in
* *
@ -671,9 +678,32 @@ class CakeEmail {
return $this->_transportName; return $this->_transportName;
} }
$this->_transportName = (string)$name; $this->_transportName = (string)$name;
$this->_transportClass = null;
return $this; 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 * Message-ID
* *
@ -755,11 +785,17 @@ class CakeEmail {
if (empty($config)) { if (empty($config)) {
return $this->_config; return $this->_config;
} }
if (is_array($config)) { if (is_array($config)) {
$this->_config = $config; $this->_config = $config;
} else { } else {
$this->_config = (string)$config; $this->_config = (string)$config;
} }
if ($this->_transportClass) {
$this->_transportClass->config($this->_config);
}
return $this; return $this;
} }
@ -828,16 +864,9 @@ class CakeEmail {
$this->_message[] = ''; $this->_message[] = '';
} }
list($plugin, $transportClassname) = pluginSplit($this->_transportName, true); $transport = $this->transportClass();
$transportClassname .= 'Transport'; $transport->config($config);
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 = new $transportClassname($config);
return $transport->send($this); return $transport->send($this);
} }
@ -865,6 +894,7 @@ class CakeEmail {
$this->_message = ''; $this->_message = '';
$this->_emailFormat = 'text'; $this->_emailFormat = 'text';
$this->_transportName = 'mail'; $this->_transportName = 'mail';
$this->_transportClass = null;
$this->_attachments = array(); $this->_attachments = array();
$this->_config = 'default'; $this->_config = 'default';
return $this; return $this;

View file

@ -31,16 +31,6 @@ abstract class AbstractTransport {
*/ */
protected $_config = array(); protected $_config = array();
/**
* Constructor
*
*/
public function __construct($config = array()) {
if (!empty($config)) {
$this->_config = $config;
}
}
/** /**
* Send mail * Send mail
* *
@ -49,6 +39,18 @@ abstract class AbstractTransport {
*/ */
abstract public function send(CakeEmail $email); 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 * Help to convert headers in string
* *

View file

@ -47,16 +47,6 @@ class SmtpTransport extends AbstractTransport {
* @thrown SocketException * @thrown SocketException
*/ */
public function send(CakeEmail $email) { 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->_cakeEmail = $email;
$this->_connect(); $this->_connect();
@ -68,6 +58,24 @@ class SmtpTransport extends AbstractTransport {
return true; 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 * Connect to SMTP Server
* *