Skip to content

Commit

Permalink
Merge pull request #9684 from cakephp/email-detect-content-type
Browse files Browse the repository at this point in the history
Autodetect content-types for email attachments.
  • Loading branch information
dereuromark committed Nov 2, 2016
2 parents 9e6a3c3 + 09da3b0 commit 9456d22
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 13 deletions.
3 changes: 3 additions & 0 deletions src/Mailer/Email.php
Expand Up @@ -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';
}
Expand Down
24 changes: 12 additions & 12 deletions tests/TestCase/Mailer/EmailTest.php
Expand Up @@ -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());
Expand All @@ -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());

Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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']);
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -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";

Expand Down Expand Up @@ -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']);

Expand Down Expand Up @@ -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'),
Expand Down
2 changes: 1 addition & 1 deletion tests/TestCase/TestSuite/EmailAssertTraitTest.php
Expand Up @@ -57,7 +57,7 @@ public function testFunctional()
$this->assertEmailAttachmentsContains('TestUserMailer.php');
$this->assertEmailAttachmentsContains('TestMailer.php', [
'file' => dirname(dirname(__DIR__)) . DS . 'test_app' . DS . 'TestApp' . DS . 'Mailer' . DS . 'TestMailer.php',
'mimetype' => 'application/octet-stream',
'mimetype' => 'text/x-php',
]);
}
}

0 comments on commit 9456d22

Please sign in to comment.