Skip to content

Commit

Permalink
Message: fixed wrong content type for .eml files added as attachment [C…
Browse files Browse the repository at this point in the history
…loses #47]
  • Loading branch information
dg committed Dec 5, 2017
1 parent 8e37678 commit d5fd390
Show file tree
Hide file tree
Showing 3 changed files with 93 additions and 2 deletions.
11 changes: 9 additions & 2 deletions src/Mail/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -298,9 +298,16 @@ private function createAttachment(string $file, string $content = null, string $
throw new Nette\FileNotFoundException("Unable to read file '$file'.");
}
}
if (!$contentType) {
$contentType = finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content);
}
if (!strcasecmp($contentType, 'message/rfc822')) { // not allowed for attached files
$contentType = 'application/octet-stream';
}

$part->setBody($content);
$part->setContentType($contentType ? $contentType : finfo_buffer(finfo_open(FILEINFO_MIME_TYPE), $content));
$part->setEncoding($contentType && preg_match('#(multipart|message)/#A', $contentType) ? self::ENCODING_8BIT : self::ENCODING_BASE64);
$part->setContentType($contentType);
$part->setEncoding(preg_match('#(multipart|message)/#A', $contentType) ? self::ENCODING_8BIT : self::ENCODING_BASE64);
$part->setHeader('Content-Disposition', $disposition . '; filename="' . Strings::fixEncoding(basename($file)) . '"');
return $part;
}
Expand Down
45 changes: 45 additions & 0 deletions tests/Mail/Mail.attachment.eml.phpt
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
<?php

/**
* Test: Nette\Mail\Message - attachments.
*/

declare(strict_types=1);

use Nette\Mail\Message;
use Tester\Assert;


require __DIR__ . '/../bootstrap.php';

require __DIR__ . '/Mail.php';


$mailer = new TestMailer;

$mail = new Message;
$mail->addAttachment(__DIR__ . '/files/example.eml', null, 'MESSAGE/RFC822');
$mailer->send($mail);

Assert::match(<<<'EOD'
MIME-Version: 1.0
X-Mailer: Nette Framework
Date: %a%
Message-ID: <%S%@%S%>
Content-Type: multipart/mixed;
boundary="--------%S%"
----------%S%
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
----------%S%
Content-Type: application/octet-stream
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="example.eml"
Um%A%=
----------%S%--
EOD
, TestMailer::$output);
39 changes: 39 additions & 0 deletions tests/Mail/files/example.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
Return-Path: <example@acm.org>
To: Example <example@linux.local>
Subject: Testing
From: example <example@acm.org>
Reply-To: example <example@acm.org>
Sender: example@acm.org
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="652b8c4dcb00cdcdda1e16af36781caf"
Message-ID: <20050430192829.0489.example@acm.org>
Date: Sat, 30 Apr 2005 19:28:29 -0300


--652b8c4dcb00cdcdda1e16af36781caf
Content-Type: multipart/related; boundary="6a82fb459dcaacd40ab3404529e808dc"
--6a82fb459dcaacd40ab3404529e808dc
Content-Type: multipart/alternative; boundary="69c1683a3ee16ef7cf16edd700694a2f"
--69c1683a3ee16ef7cf16edd700694a2f
Content-Type: text/plain; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable
This is an HTML message. Please use an HTML capable mail program to read
this message.
--69c1683a3ee16ef7cf16edd700694a2f
Content-Type: text/html; charset=ISO-8859-1
Content-Transfer-Encoding: quoted-printable

<html>
<head>
<P>Hello</p>
</body>
</html>
--69c1683a3ee16ef7cf16edd700694a2f--

--652b8c4dcb00cdcdda1e16af36781caf--

0 comments on commit d5fd390

Please sign in to comment.