Merge pull request #1385 from LeaseWeb/2.4

Add option to send email attachment from string (not only from file).
This commit is contained in:
Mark Story 2013-07-10 10:12:07 -07:00
commit d992d3a626
2 changed files with 63 additions and 10 deletions

View file

@ -975,6 +975,15 @@ class CakeEmail {
* 'contentDisposition' => false * 'contentDisposition' => false
* )); * ));
* }}} * }}}
*
* Attach a file from string and specify additional properties:
*
* {{{
* $email->attachments(array('custom_name.png' => array(
* 'data' => file_get_contents('path/to/file'),
* 'mimetype' => 'image/png'
* ));
* }}}
* *
* The `contentId` key allows you to specify an inline attachment. In your email text, you * The `contentId` key allows you to specify an inline attachment. In your email text, you
* can use `<img src="cid:abc123" />` to display the image inline. * can use `<img src="cid:abc123" />` to display the image inline.
@ -996,14 +1005,21 @@ class CakeEmail {
$fileInfo = array('file' => $fileInfo); $fileInfo = array('file' => $fileInfo);
} }
if (!isset($fileInfo['file'])) { if (!isset($fileInfo['file'])) {
throw new SocketException(__d('cake_dev', 'File not specified.')); if (!isset($fileInfo['data'])) {
} throw new SocketException(__d('cake_dev', 'No file or data specified.'));
$fileInfo['file'] = realpath($fileInfo['file']); }
if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) { if (is_int($name)) {
throw new SocketException(__d('cake_dev', 'File not found: "%s"', $fileInfo['file'])); throw new SocketException(__d('cake_dev', 'No filename specified.'));
} }
if (is_int($name)) { $fileInfo['data'] = chunk_split(base64_encode($fileInfo['data']), 76, "\r\n");
$name = basename($fileInfo['file']); } else {
$fileInfo['file'] = realpath($fileInfo['file']);
if ($fileInfo['file'] === false || !file_exists($fileInfo['file'])) {
throw new SocketException(__d('cake_dev', 'File not found: "%s"', $fileInfo['file']));
}
if (is_int($name)) {
$name = basename($fileInfo['file']);
}
} }
if (!isset($fileInfo['mimetype'])) { if (!isset($fileInfo['mimetype'])) {
$fileInfo['mimetype'] = 'application/octet-stream'; $fileInfo['mimetype'] = 'application/octet-stream';
@ -1409,7 +1425,7 @@ class CakeEmail {
if (!empty($fileInfo['contentId'])) { if (!empty($fileInfo['contentId'])) {
continue; continue;
} }
$data = $this->_readFile($fileInfo['file']); $data = isset($fileInfo['data']) ? $fileInfo['data'] : $this->_readFile($fileInfo['file']);
$msg[] = '--' . $boundary; $msg[] = '--' . $boundary;
$msg[] = 'Content-Type: ' . $fileInfo['mimetype']; $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];
@ -1454,7 +1470,7 @@ class CakeEmail {
if (empty($fileInfo['contentId'])) { if (empty($fileInfo['contentId'])) {
continue; continue;
} }
$data = $this->_readFile($fileInfo['file']); $data = isset($fileInfo['data']) ? $fileInfo['data'] : $this->_readFile($fileInfo['file']);
$msg[] = '--' . $boundary; $msg[] = '--' . $boundary;
$msg[] = 'Content-Type: ' . $fileInfo['mimetype']; $msg[] = 'Content-Type: ' . $fileInfo['mimetype'];

View file

@ -927,6 +927,43 @@ class CakeEmailTest extends CakeTestCase {
$this->assertContains($expected, $result['message']); $this->assertContains($expected, $result['message']);
} }
/**
* Test send() with no template and data string attachment
*
* @return void
*/
public function testSendNoTemplateWithDataStringAttachment() {
$this->CakeEmail->transport('debug');
$this->CakeEmail->from('cake@cakephp.org');
$this->CakeEmail->to('cake@cakephp.org');
$this->CakeEmail->subject('My title');
$this->CakeEmail->emailFormat('text');
$data = file_get_contents(CAKE . 'Console/Templates/skel/webroot/img/cake.icon.png');
$this->CakeEmail->attachments(array('cake.icon.png' => array(
'data' => $data,
'mimetype' => 'image/png'
)));
$result = $this->CakeEmail->send('Hello');
$boundary = $this->CakeEmail->getBoundary();
$this->assertContains('Content-Type: multipart/mixed; boundary="' . $boundary . '"', $result['headers']);
$expected = "--$boundary\r\n" .
"Content-Type: text/plain; charset=UTF-8\r\n" .
"Content-Transfer-Encoding: 8bit\r\n" .
"\r\n" .
"Hello" .
"\r\n" .
"\r\n" .
"\r\n" .
"--$boundary\r\n" .
"Content-Type: image/png\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment; filename=\"cake.icon.png\"\r\n\r\n";
$expected .= chunk_split(base64_encode($data), 76, "\r\n");
$this->assertContains($expected, $result['message']);
}
/** /**
* Test send() with no template as both * Test send() with no template as both
* *