Implemented methods to attachments.

This commit is contained in:
Juan Basso 2011-03-01 18:51:12 -03:00
parent 6c3a42c82a
commit b29c31709e
2 changed files with 65 additions and 9 deletions

View file

@ -159,18 +159,11 @@ class CakeEmail {
/**
* List of files that should be attached to the email.
*
* Can be both absolute and relative paths
* Only absolute paths
*
* @var array
*/
public $attachments = array();
/**
* The list of paths to search if an attachment isnt absolute
*
* @var array
*/
public $filePaths = array();
protected $_attachments = array();
/**
* If set, boundary to use for multipart mime messages
@ -613,6 +606,48 @@ class CakeEmail {
$this->_transportName = (string)$name;
}
/**
* Set attachments
*
* @param mixed $attachments String with the filename or array with filenames
* @return void
* @thrown SocketException
*/
public function setAttachments($attachments) {
$attachments = (array)$attachments;
foreach ($attachments as &$attach) {
$path = realpath($attach);
if ($path === false) {
throw new SocketException(__('File not found: "%s"', $attach));
}
$attach = $path;
}
$this->_attachments = $attachments;
}
/**
* Add attachments
*
* @param mixed $attachments String with the filename or array with filenames
* @return void
* @thrown SocketException
*/
public function addAttachments($attachments) {
$current = $this->_attachments;
$this->setAttachments($attachments);
$this->_attachments = array_unique(array_merge($current, $this->_attachments));
}
/**
* Get attachments
*
* @return array
*/
public function getAttachments() {
return $this->_attachments;
}
/**
* Send an email using the specified content, template and layout
*
@ -640,6 +675,7 @@ class CakeEmail {
$this->_template = '';
$this->_emailFormat = 'text';
$this->_transportName = 'mail';
$this->_attachments = array();
}
}

View file

@ -156,6 +156,26 @@ class CakeEmailTest extends CakeTestCase {
$this->assertIdentical($this->CakeEmail->getHeaders(array('from' => true, 'to' => true)), $expected);
}
/**
* testAttachments
*
* @return void
*/
public function testAttachments() {
$this->CakeEmail->setAttachments(WWW_ROOT . 'index.php');
$expected = array(WWW_ROOT . 'index.php');
$this->assertIdentical($this->CakeEmail->getAttachments(), $expected);
$this->CakeEmail->setAttachments(array());
$this->assertIdentical($this->CakeEmail->getAttachments(), array());
$this->CakeEmail->setAttachments(WWW_ROOT . 'index.php');
$this->CakeEmail->addAttachments(WWW_ROOT . 'test.php');
$this->CakeEmail->addAttachments(array(WWW_ROOT . 'test.php', WWW_ROOT . '.htaccess'));
$expected = array(WWW_ROOT . 'index.php', WWW_ROOT . 'test.php', WWW_ROOT . '.htaccess');
$this->assertIdentical(array_values($this->CakeEmail->getAttachments()), $expected);
}
/**
* testSend method
*