Skip to content
Permalink
Browse files

Autodetect content-types for email attachments.

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 3.next with this
change.
  • Loading branch information...
markstory committed Nov 1, 2016
1 parent 270afde commit 580e4a6a276eec5102c8f9878e182b09a23e5cf6
Showing with 15 additions and 12 deletions.
  1. +3 −0 src/Mailer/Email.php
  2. +12 −12 tests/TestCase/Mailer/EmailTest.php
@@ -1177,6 +1177,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';
}
@@ -763,7 +763,7 @@ public function testAttachments()
$expected = [
'basics.php' => [
'file' => CAKE . 'basics.php',
'mimetype' => 'application/octet-stream'
'mimetype' => 'text/x-php'
]
];
$this->assertSame($expected, $this->CakeEmail->attachments());
@@ -782,9 +782,9 @@ public function testAttachments()
]);
$expected = [
'basics.php' => ['file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'],
'bootstrap.php' => ['file' => CORE_PATH . 'config' . DS . 'bootstrap.php', 'mimetype' => 'application/octet-stream'],
'other.txt' => ['file' => CORE_PATH . 'config' . DS . 'bootstrap.php', 'mimetype' => 'application/octet-stream'],
'license' => ['file' => CORE_PATH . 'LICENSE.txt', 'mimetype' => 'application/octet-stream']
'bootstrap.php' => ['file' => CORE_PATH . 'config' . DS . 'bootstrap.php', 'mimetype' => 'text/x-php'],
'other.txt' => ['file' => CORE_PATH . 'config' . DS . 'bootstrap.php', 'mimetype' => 'text/x-php'],
'license' => ['file' => CORE_PATH . 'LICENSE.txt', 'mimetype' => 'text/plain']
];
$this->assertSame($expected, $this->CakeEmail->attachments());
@@ -1207,7 +1207,7 @@ public function testSendNoTemplateWithAttachments()
"\r\n" .
"--$boundary\r\n" .
"Content-Disposition: attachment; filename=\"basics.php\"\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/x-php\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"\r\n";
$this->assertContains($expected, $result['message']);
@@ -1291,7 +1291,7 @@ public function testSendNoTemplateWithAttachmentsAsBoth()
"\r\n" .
"--$boundary\r\n" .
"Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/plain\r\n" .
"Content-Transfer-Encoding: base64\r\n" .
"\r\n";
$this->assertContains($expected, $result['message']);
@@ -1345,7 +1345,7 @@ public function testSendWithInlineAttachments()
"\r\n" .
"--rel-$boundary\r\n" .
"Content-Disposition: inline; filename=\"cake.png\"\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" .
"\r\n";
@@ -1389,7 +1389,7 @@ public function testSendWithInlineAttachmentsHtmlOnly()
"\r\n" .
"--rel-$boundary\r\n" .
"Content-Disposition: inline; filename=\"cake.png\"\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" .
"\r\n";
@@ -1429,7 +1429,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";
@@ -1872,14 +1872,14 @@ public function testSendAttachment()
$this->CakeEmail->attachments([CAKE . 'basics.php']);
$result = $this->CakeEmail->send('body');
$expected = "Content-Disposition: attachment; filename=\"basics.php\"\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/x-php\r\n" .
"Content-Transfer-Encoding: base64\r\n";
$this->assertContains($expected, $result['message']);
$this->CakeEmail->attachments(['my.file.txt' => CAKE . 'basics.php']);
$result = $this->CakeEmail->send('body');
$expected = "Content-Disposition: attachment; filename=\"my.file.txt\"\r\n" .
"Content-Type: application/octet-stream\r\n" .
"Content-Type: text/x-php\r\n" .
"Content-Transfer-Encoding: base64\r\n";
$this->assertContains($expected, $result['message']);
@@ -2785,7 +2785,7 @@ public function testJsonSerialize()
'_attachments' => [
'test.txt' => [
'data' => $encode(TEST_APP . 'config' . DS . 'empty.ini'),
'mimetype' => 'application/octet-stream'
'mimetype' => 'text/plain'
],
'image' => [
'data' => $encode(TEST_APP . 'webroot' . DS . 'img' . DS . 'cake.icon.png'),

0 comments on commit 580e4a6

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