Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Create method readAndBase64Encode() in File utility.
Move this utility method out of CakeEmail, which allows other Mail transports to encode files manually. Maintains BC.
  • Loading branch information
jmillerdesign committed May 16, 2013
1 parent f47609b commit dfdde95
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 8 deletions.
13 changes: 5 additions & 8 deletions lib/Cake/Network/Email/CakeEmail.php
Expand Up @@ -21,6 +21,7 @@
App::uses('Validation', 'Utility'); App::uses('Validation', 'Utility');
App::uses('Multibyte', 'I18n'); App::uses('Multibyte', 'I18n');
App::uses('AbstractTransport', 'Network/Email'); App::uses('AbstractTransport', 'Network/Email');
App::uses('File', 'Utility');
App::uses('String', 'Utility'); App::uses('String', 'Utility');
App::uses('View', 'View'); App::uses('View', 'View');
App::import('I18n', 'Multibyte'); App::import('I18n', 'Multibyte');
Expand Down Expand Up @@ -1359,7 +1360,7 @@ protected function _attachFiles($boundary = null) {
if (!empty($fileInfo['contentId'])) { if (!empty($fileInfo['contentId'])) {
continue; continue;
} }
$data = $this->readFile($fileInfo['file']); $data = $this->_readFile($fileInfo['file']);


$msg[] = '--' . $boundary; $msg[] = '--' . $boundary;
$msg[] = 'Content-Type: ' . $fileInfo['mimetype']; $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
Expand All @@ -1383,12 +1384,8 @@ protected function _attachFiles($boundary = null) {
* @param string $file The file to read. * @param string $file The file to read.
* @return string File contents in base64 encoding * @return string File contents in base64 encoding
*/ */
public function readFile($file) { protected function _readFile($file) {
$handle = fopen($file, 'rb'); return File::readAndBase64Encode($file);
$data = fread($handle, filesize($file));
$data = chunk_split(base64_encode($data));
fclose($handle);
return $data;
} }


/** /**
Expand All @@ -1407,7 +1404,7 @@ protected function _attachInlineFiles($boundary = null) {
if (empty($fileInfo['contentId'])) { if (empty($fileInfo['contentId'])) {
continue; continue;
} }
$data = $this->readFile($fileInfo['file']); $data = $this->_readFile($fileInfo['file']);


$msg[] = '--' . $boundary; $msg[] = '--' . $boundary;
$msg[] = 'Content-Type: ' . $fileInfo['mimetype']; $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
Expand Down
15 changes: 15 additions & 0 deletions lib/Cake/Utility/File.php
Expand Up @@ -180,6 +180,21 @@ public function read($bytes = false, $mode = 'rb', $force = false) {
return trim($data); return trim($data);
} }


/**
* Read the file contents and return a base64 version of the file contents.
*
* @param string $file The file to read.
* @return string File contents in base64 encoding
* @link http://book.cakephp.org/2.0/en/core-utility-libraries/file-folder.html#File::readAndBase64Encode
*/
public static function readAndBase64Encode($file) {
$handle = fopen($file, 'rb');
$data = fread($handle, filesize($file));
$data = chunk_split(base64_encode($data));
fclose($handle);
return $data;
}

/** /**
* Sets or gets the offset for the currently opened file. * Sets or gets the offset for the currently opened file.
* *
Expand Down

0 comments on commit dfdde95

Please sign in to comment.