diff --git a/lib/Cake/Network/CakeEmail.php b/lib/Cake/Network/CakeEmail.php index 05b953cbc..fc8647015 100644 --- a/lib/Cake/Network/CakeEmail.php +++ b/lib/Cake/Network/CakeEmail.php @@ -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(); } } diff --git a/lib/Cake/tests/Case/Network/CakeEmailTest.php b/lib/Cake/tests/Case/Network/CakeEmailTest.php index 13a5a3a39..806a49488 100644 --- a/lib/Cake/tests/Case/Network/CakeEmailTest.php +++ b/lib/Cake/tests/Case/Network/CakeEmailTest.php @@ -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 *