Skip to content
Permalink
Browse files

Autodetect content-types for email attachments (2.x)

This ports the safe parts of #9619 and updates the tests. Because
existing tests had to change and I was concerned about changing people's
email messages in a bugfix release I'm targetting 2.next with this
change.
  • Loading branch information...
markstory committed Nov 7, 2016
1 parent c0150f6 commit 3837f40394c1ffd9477c65a12b0da84b9e5d1622
Showing with 14 additions and 11 deletions.
  1. +3 −0 lib/Cake/Network/Email/CakeEmail.php
  2. +11 −11 lib/Cake/Test/Case/Network/Email/CakeEmailTest.php
@@ -1081,6 +1081,9 @@ public function attachments($attachments = null) {
$name = basename($fileInfo['file']);
}
}
if (!isset($fileInfo['mimetype']) && function_exists('mime_content_type')) {
$fileInfo['mimetype'] = mime_content_type($fileInfo['file']);
}
if (!isset($fileInfo['mimetype'])) {
$fileInfo['mimetype'] = 'application/octet-stream';
}
@@ -821,7 +821,7 @@ public function testAttachments() {
$expected = array(
'basics.php' => array(
'file' => CAKE . 'basics.php',
'mimetype' => 'application/octet-stream'
'mimetype' => 'text/x-php'
)
);
$this->assertSame($expected, $this->CakeEmail->attachments());
@@ -837,9 +837,9 @@ public function testAttachments() {
$this->CakeEmail->addAttachments(array('other.txt' => CAKE . 'bootstrap.php', 'license' => CAKE . 'LICENSE.txt'));
$expected = array(
'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'text/x-php'),
'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'text/x-php'),
'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'text/plain')
);
$this->assertSame($expected, $this->CakeEmail->attachments());
@@ -1075,7 +1075,7 @@ public function testSendNoTemplateWithAttachments() {
"\r\n" .
"\r\n" .
"--$boundary\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/x-php\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment; filename=\"basics.php\"\r\n\r\n";
$this->assertContains($expected, $result['message']);
@@ -1156,7 +1156,7 @@ public function testSendNoTemplateWithAttachmentsAsBoth() {
"--alt-{$boundary}--\r\n" .
"\r\n" .
"--$boundary\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/plain\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n\r\n";
$this->assertContains($expected, $result['message']);
@@ -1208,7 +1208,7 @@ public function testSendWithInlineAttachments() {
"--alt-{$boundary}--\r\n" .
"\r\n" .
"--rel-$boundary\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/plain\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-ID: <abc123>\r\n" .
"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
@@ -1250,7 +1250,7 @@ public function testSendWithInlineAttachmentsHtmlOnly() {
"\r\n" .
"\r\n" .
"--rel-$boundary\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/plain\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"Content-ID: <abc123>\r\n" .
"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
@@ -1289,7 +1289,7 @@ public function testSendWithNoContentDispositionAttachments() {
"\r\n" .
"\r\n" .
"--{$boundary}\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/plain\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"\r\n";
@@ -1707,11 +1707,11 @@ public function testSendAttachment() {
$this->CakeEmail->config(array());
$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
$result = $this->CakeEmail->send('body');
$this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\"", $result['message']);
$this->assertContains("Content-Type: text/x-php\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\"", $result['message']);
$this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
$result = $this->CakeEmail->send('body');
$this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\"", $result['message']);
$this->assertContains("Content-Type: text/x-php\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\"", $result['message']);
$this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
$result = $this->CakeEmail->send('body');

0 comments on commit 3837f40

Please sign in to comment.
You can’t perform that action at this time.