Skip to content

Commit 3837f40

Browse files
committed
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.
1 parent c0150f6 commit 3837f40

File tree

2 files changed

+14
-11
lines changed

2 files changed

+14
-11
lines changed

lib/Cake/Network/Email/CakeEmail.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1081,6 +1081,9 @@ public function attachments($attachments = null) {
10811081
$name = basename($fileInfo['file']);
10821082
}
10831083
}
1084+
if (!isset($fileInfo['mimetype']) && function_exists('mime_content_type')) {
1085+
$fileInfo['mimetype'] = mime_content_type($fileInfo['file']);
1086+
}
10841087
if (!isset($fileInfo['mimetype'])) {
10851088
$fileInfo['mimetype'] = 'application/octet-stream';
10861089
}

lib/Cake/Test/Case/Network/Email/CakeEmailTest.php

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,7 @@ public function testAttachments() {
821821
$expected = array(
822822
'basics.php' => array(
823823
'file' => CAKE . 'basics.php',
824-
'mimetype' => 'application/octet-stream'
824+
'mimetype' => 'text/x-php'
825825
)
826826
);
827827
$this->assertSame($expected, $this->CakeEmail->attachments());
@@ -837,9 +837,9 @@ public function testAttachments() {
837837
$this->CakeEmail->addAttachments(array('other.txt' => CAKE . 'bootstrap.php', 'license' => CAKE . 'LICENSE.txt'));
838838
$expected = array(
839839
'basics.php' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain'),
840-
'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
841-
'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'application/octet-stream'),
842-
'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'application/octet-stream')
840+
'bootstrap.php' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'text/x-php'),
841+
'other.txt' => array('file' => CAKE . 'bootstrap.php', 'mimetype' => 'text/x-php'),
842+
'license' => array('file' => CAKE . 'LICENSE.txt', 'mimetype' => 'text/plain')
843843
);
844844
$this->assertSame($expected, $this->CakeEmail->attachments());
845845

@@ -1075,7 +1075,7 @@ public function testSendNoTemplateWithAttachments() {
10751075
"\r\n" .
10761076
"\r\n" .
10771077
"--$boundary\r\n" .
1078-
"Content-Type: application/octet-stream\r\n" .
1078+
"Content-Type: text/x-php\r\n" .
10791079
"Content-Transfer-Encoding: base64\r\n" .
10801080
"Content-Disposition: attachment; filename=\"basics.php\"\r\n\r\n";
10811081
$this->assertContains($expected, $result['message']);
@@ -1156,7 +1156,7 @@ public function testSendNoTemplateWithAttachmentsAsBoth() {
11561156
"--alt-{$boundary}--\r\n" .
11571157
"\r\n" .
11581158
"--$boundary\r\n" .
1159-
"Content-Type: application/octet-stream\r\n" .
1159+
"Content-Type: text/plain\r\n" .
11601160
"Content-Transfer-Encoding: base64\r\n" .
11611161
"Content-Disposition: attachment; filename=\"VERSION.txt\"\r\n\r\n";
11621162
$this->assertContains($expected, $result['message']);
@@ -1208,7 +1208,7 @@ public function testSendWithInlineAttachments() {
12081208
"--alt-{$boundary}--\r\n" .
12091209
"\r\n" .
12101210
"--rel-$boundary\r\n" .
1211-
"Content-Type: application/octet-stream\r\n" .
1211+
"Content-Type: text/plain\r\n" .
12121212
"Content-Transfer-Encoding: base64\r\n" .
12131213
"Content-ID: <abc123>\r\n" .
12141214
"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
@@ -1250,7 +1250,7 @@ public function testSendWithInlineAttachmentsHtmlOnly() {
12501250
"\r\n" .
12511251
"\r\n" .
12521252
"--rel-$boundary\r\n" .
1253-
"Content-Type: application/octet-stream\r\n" .
1253+
"Content-Type: text/plain\r\n" .
12541254
"Content-Transfer-Encoding: base64\r\n" .
12551255
"Content-ID: <abc123>\r\n" .
12561256
"Content-Disposition: inline; filename=\"cake.png\"\r\n\r\n";
@@ -1289,7 +1289,7 @@ public function testSendWithNoContentDispositionAttachments() {
12891289
"\r\n" .
12901290
"\r\n" .
12911291
"--{$boundary}\r\n" .
1292-
"Content-Type: application/octet-stream\r\n" .
1292+
"Content-Type: text/plain\r\n" .
12931293
"Content-Transfer-Encoding: base64\r\n" .
12941294
"\r\n";
12951295

@@ -1707,11 +1707,11 @@ public function testSendAttachment() {
17071707
$this->CakeEmail->config(array());
17081708
$this->CakeEmail->attachments(array(CAKE . 'basics.php'));
17091709
$result = $this->CakeEmail->send('body');
1710-
$this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\"", $result['message']);
1710+
$this->assertContains("Content-Type: text/x-php\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"basics.php\"", $result['message']);
17111711

17121712
$this->CakeEmail->attachments(array('my.file.txt' => CAKE . 'basics.php'));
17131713
$result = $this->CakeEmail->send('body');
1714-
$this->assertContains("Content-Type: application/octet-stream\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\"", $result['message']);
1714+
$this->assertContains("Content-Type: text/x-php\r\nContent-Transfer-Encoding: base64\r\nContent-Disposition: attachment; filename=\"my.file.txt\"", $result['message']);
17151715

17161716
$this->CakeEmail->attachments(array('file.txt' => array('file' => CAKE . 'basics.php', 'mimetype' => 'text/plain')));
17171717
$result = $this->CakeEmail->send('body');

0 commit comments

Comments
 (0)