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

Commit a8c1366

Browse files
committed
Merge branch 'hotfix/30' into develop
Forward port #30
2 parents 243772f + 9aaf979 commit a8c1366

File tree

3 files changed

+33
-2
lines changed

3 files changed

+33
-2
lines changed

CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,9 @@ All notable changes to this project will be documented in this file, in reverse
3636

3737
### Fixed
3838

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

4143
## 2.6.1 - 2017-01-16
4244

src/Decode.php

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,24 +122,31 @@ public static function splitMessage($message, &$headers, &$body, $EOL = Mime::LI
122122
}
123123
}
124124

125+
// @todo splitMime removes "\r" sequences, which breaks valid mime
126+
// messages as returned by many mail servers
127+
$headersEOL = $EOL;
128+
125129
// find an empty line between headers and body
126130
// default is set new line
131+
// @todo Maybe this is too much "magic"; we should be more strict here
127132
if (strpos($message, $EOL . $EOL)) {
128133
list($headers, $body) = explode($EOL . $EOL, $message, 2);
129134
// next is the standard new line
130135
} elseif ($EOL != "\r\n" && strpos($message, "\r\n\r\n")) {
131136
list($headers, $body) = explode("\r\n\r\n", $message, 2);
137+
$headersEOL = "\r\n"; // Headers::fromString will fail with incorrect EOL
132138
// next is the other "standard" new line
133139
} elseif ($EOL != "\n" && strpos($message, "\n\n")) {
134140
list($headers, $body) = explode("\n\n", $message, 2);
141+
$headersEOL = "\n";
135142
// at last resort find anything that looks like a new line
136143
} else {
137144
ErrorHandler::start(E_NOTICE | E_WARNING);
138145
list($headers, $body) = preg_split("%([\r\n]+)\\1%U", $message, 2);
139146
ErrorHandler::stop();
140147
}
141148

142-
$headers = Headers::fromString($headers, $EOL);
149+
$headers = Headers::fromString($headers, $headersEOL);
143150
}
144151

145152
/**

test/MessageTest.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
namespace ZendTest\Mime;
1111

1212
use Zend\Mime;
13+
use Zend\Mime\Message;
1314

1415
/**
1516
* @group Zend_Mime
@@ -200,4 +201,25 @@ public function testDuplicatePartAddedWillThrowException()
200201
$message->addPart($part);
201202
$message->addPart($part);
202203
}
204+
205+
public function testFromStringWithCrlfAndRfc2822FoldedHeaders()
206+
{
207+
// This is a fixture as provided by many mailservers
208+
// e.g. cyrus or dovecot
209+
$eol = "\r\n";
210+
$fixture = 'This is a MIME-encapsulated message' . $eol . $eol
211+
. '--=_af4357ef34b786aae1491b0a2d14399f' . $eol
212+
. 'Content-Type: text/plain' . $eol
213+
. 'Content-Disposition: attachment;' . $eol
214+
. "\t" . 'filename="test.txt"' . $eol // Valid folding
215+
. $eol
216+
. 'This is a test' . $eol
217+
. '--=_af4357ef34b786aae1491b0a2d14399f--';
218+
219+
$message = Message::createFromMessage($fixture, '=_af4357ef34b786aae1491b0a2d14399f', $eol);
220+
$parts = $message->getParts();
221+
222+
$this->assertEquals(1, count($parts));
223+
$this->assertEquals('attachment; filename="test.txt"', $parts[0]->getDisposition());
224+
}
203225
}

0 commit comments

Comments
 (0)