Skip to content

Commit

Permalink
Created the email class and a logic in CakeEmail.
Browse files Browse the repository at this point in the history
  • Loading branch information
jrbasso committed Apr 13, 2011
1 parent 5f54946 commit daa90ed
Show file tree
Hide file tree
Showing 3 changed files with 107 additions and 2 deletions.
58 changes: 58 additions & 0 deletions app/config/email.php.default
@@ -0,0 +1,58 @@
<?php
/**
* This is email configuration file.
*
* Use it to configure email transports of Cake.
*
* PHP 5
*
* CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
* Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
*
* Licensed under The MIT License
* Redistributions of files must retain the above copyright notice.
*
* @copyright Copyright 2005-2011, Cake Software Foundation, Inc. (http://cakefoundation.org)
* @link http://cakephp.org CakePHP(tm) Project
* @package app.config
* @since CakePHP(tm) v 2.0.0
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
*/
/**
* In this file you set up your database connection details.
*
* @package cake.config
*/
/**
* Email configuration class.
* You can specify multiple configurations for production, development and testing.
*
* transport => The name of a supported transport; valid options are as follows:
* mail - MySQL 4 & 5,
* smtp - Send using SMTP
*
* You can add custom database transports (or override existing transports) by adding the
* appropriate file to app/libs/emails. Transports should be named 'x_transport.php',
* where 'x' is the name of the transport.
*
* from =>
* The origin email. See CakeEmail::to() about the valid values
*
*/
class EMAIL_CONFIG {

public $default = array(
'driver' => 'mail',
'from' => 'you@localhost'
);

public $smtp = array(
'driver' => 'smtp',
'from' => array('My Site', 'site@localhost'),
'host' => 'localhost',
'port' => 25,
'username' => 'user',
'password' => 'secret'
);

}
45 changes: 43 additions & 2 deletions lib/Cake/Network/CakeEmail.php
Expand Up @@ -215,6 +215,13 @@ class CakeEmail {
*/
protected $_boundary = null;

/**
* Configuration to transport
*
* @var mixed
*/
protected $_config = 'default';

/**
* Constructor
*
Expand Down Expand Up @@ -727,15 +734,49 @@ public function message() {
return $this->_message;
}

/**
* Configuration to use when send email
*
* @param mixed $config String with configuration name (from email.php), array with config or null to return current config
* @return string
*/
public function config($config = null) {
if (!empty($config)) {
if (is_array($config)) {
$this->_config = $config;
} else {
$this->_config = (string)$config;
}
}
return $this->_config;
}

/**
* Send an email using the specified content, template and layout
*
* @return boolean Success
* @thrown SocketExpcetion
*/
public function send($content = null) {
if (is_string($this->_config)) {
if (!config('email')) {
throw new SocketException(__('%s not found.', APP . DS . 'email.php'));
}
$configs = new EMAIL_CONFIG();
if (!isset($configs->{$this->_config})) {
throw new SocketException(__('Unknown email configuration "%s".', $this->_config));
}
$config = $configs->{$this->_config};
} else {
$config = $this->_config;
}

if (empty($this->_from)) {
throw new SocketException(__('From is not specified.'));
if (!empty($config['from'])) {
$this->to($config['from']);
} else {
throw new SocketException(__('From is not specified.'));
}
}
if (empty($this->_to) && empty($this->_cc) && empty($this->_bcc)) {
throw new SocketExpcetion(__('You need specify one destination on to, cc or bcc.'));
Expand Down Expand Up @@ -782,7 +823,7 @@ public function send($content = null) {
throw new SocketException(__('The "%s" do not have send method.', $transportClassname));
}

$transport = new $transportClassname();
$transport = new $transportClassname($config);
return $transport->send($this);
}

Expand Down
6 changes: 6 additions & 0 deletions lib/Cake/tests/Case/Network/CakeEmailTest.php
Expand Up @@ -24,6 +24,12 @@
*/
class TestCakeEmail extends CakeEmail {

/**
* Config
*
*/
protected $_config = array();

/**
* Wrap to protected method
*
Expand Down

0 comments on commit daa90ed

Please sign in to comment.