Skip to content
This repository has been archived by the owner on Jan 30, 2020. It is now read-only.

Commit

Permalink
Merge branch 'hotfix/30'
Browse files Browse the repository at this point in the history
Close #30
  • Loading branch information
weierophinney committed Nov 28, 2017
2 parents b4f9d91 + 9aaf979 commit 085bba4
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
4 changes: 3 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ All notable changes to this project will be documented in this file, in reverse

### Fixed

- Nothing.
- [#30](https://github.com/zendframework/zend-mime/pull/30) fixes how EOL
characters are detected, to ensure that mail using `\r\n` as an EOL sequence
(including mail emitted by Cyrus and Dovecot) will be properly parsed.

## 2.6.1 - 2017-01-16

Expand Down
9 changes: 8 additions & 1 deletion src/Decode.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,24 +122,31 @@ public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LI
}
}

// @todo splitMime removes "\r" sequences, which breaks valid mime
// messages as returned by many mail servers
$headersEOL = $EOL;

// find an empty line between headers and body
// default is set new line
// @todo Maybe this is too much "magic"; we should be more strict here
if (strpos($message, $EOL . $EOL)) {
list($headers, $body) = explode($EOL . $EOL, $message, 2);
// next is the standard new line
} elseif ($EOL != "\r\n" && strpos($message, "\r\n\r\n")) {
list($headers, $body) = explode("\r\n\r\n", $message, 2);
$headersEOL = "\r\n"; // Headers::fromString will fail with incorrect EOL
// next is the other "standard" new line
} elseif ($EOL != "\n" && strpos($message, "\n\n")) {
list($headers, $body) = explode("\n\n", $message, 2);
$headersEOL = "\n";
// at last resort find anything that looks like a new line
} else {
ErrorHandler::start(E_NOTICE | E_WARNING);
list($headers, $body) = preg_split("%([\r\n]+)\\1%U", $message, 2);
ErrorHandler::stop();
}

$headers = Headers::fromString($headers, $EOL);
$headers = Headers::fromString($headers, $headersEOL);
}

/**
Expand Down
22 changes: 22 additions & 0 deletions test/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
namespace ZendTest\Mime;

use Zend\Mime;
use Zend\Mime\Message;

/**
* @group Zend_Mime
Expand Down Expand Up @@ -200,4 +201,25 @@ public function testDuplicatePartAddedWillThrowException()
$message->addPart($part);
$message->addPart($part);
}

public function testFromStringWithCrlfAndRfc2822FoldedHeaders()
{
// This is a fixture as provided by many mailservers
// e.g. cyrus or dovecot
$eol = "\r\n";
$fixture = 'This is a MIME-encapsulated message' . $eol . $eol
. '--=_af4357ef34b786aae1491b0a2d14399f' . $eol
. 'Content-Type: text/plain' . $eol
. 'Content-Disposition: attachment;' . $eol
. "\t" . 'filename="test.txt"' . $eol // Valid folding
. $eol
. 'This is a test' . $eol
. '--=_af4357ef34b786aae1491b0a2d14399f--';

$message = Message::createFromMessage($fixture, '=_af4357ef34b786aae1491b0a2d14399f', $eol);
$parts = $message->getParts();

$this->assertEquals(1, count($parts));
$this->assertEquals('attachment; filename="test.txt"', $parts[0]->getDisposition());
}
}

0 comments on commit 085bba4

Please sign in to comment.