diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..94d6d75 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +/vendor/ +/composer.lock \ No newline at end of file diff --git a/PlancakeEmailParser.php b/PlancakeEmailParser.php index dfd5a2f..a1e3f0c 100644 --- a/PlancakeEmailParser.php +++ b/PlancakeEmailParser.php @@ -413,10 +413,15 @@ public function getBody($returnType = self::PLAINTEXT) // there could be more than one boundary preg_match_all('!boundary=(.*)$!mi', $this->emailRawContent, $matches); - $boundaries = $matches[1]; - // sometimes boundaries are delimited by quotes - we want to remove them - foreach ($boundaries as $i => $v) { - $boundaries[$i] = str_replace(array("'", '"'), '', $v); + $boundariesRaw = $matches[1]; + $boundaries = array(); + foreach ($boundariesRaw as $i => $v) { + // sometimes boundaries are delimited by quotes - we want to remove them + $tempboundary = str_replace(array("'", '"'), '', $v); + // actual boundary lines start with -- + $boundaries[] = '--' . $tempboundary; + // or start and end with -- + $boundaries[] = '--' . $tempboundary . '--'; } foreach ($this->rawBodyLines as $line) { @@ -428,7 +433,7 @@ public function getBody($returnType = self::PLAINTEXT) if (preg_match('/charset=(.*)/i', $line, $matches)) { $charset = strtoupper(trim($matches[1], '"')); } - } else if ($detectedContentType && $waitingForContentStart) { + } elseif ($detectedContentType && $waitingForContentStart) { if (preg_match('/charset=(.*)/i', $line, $matches)) { $charset = strtoupper(trim($matches[1], '"')); } @@ -442,9 +447,8 @@ public function getBody($returnType = self::PLAINTEXT) } } else { // ($detectedContentType && !$waitingForContentStart) // collecting the actual content until we find the delimiter - // if the delimited is AAAAA, the line will be --AAAAA - that's why we use substr if (is_array($boundaries)) { - if (in_array(substr($line, 2), $boundaries)) { // found the delimiter + if (in_array($line, $boundaries)) { // found the delimiter break; } } @@ -463,7 +467,7 @@ public function getBody($returnType = self::PLAINTEXT) if ($contentTransferEncoding == 'base64') { $body = base64_decode($body); - } else if ($contentTransferEncoding == 'quoted-printable') { + } elseif ($contentTransferEncoding == 'quoted-printable') { $body = quoted_printable_decode($body); } diff --git a/composer.json b/composer.json index 926339c..1eb7217 100644 --- a/composer.json +++ b/composer.json @@ -18,6 +18,9 @@ "require": { "php": ">=5.3.0" }, + "require-dev": { + "squizlabs/php_codesniffer": "~2.5.1" + }, "autoload": { "psr-0": { "PlancakeEmailParser": "." } } diff --git a/tests/run_tests.php b/tests/run_tests.php index e8337e4..43ea2cb 100644 --- a/tests/run_tests.php +++ b/tests/run_tests.php @@ -3,19 +3,45 @@ // run this as: // php run_tests.php -function printBarrier() { echo "\r\n\r\n\r\n"; } -function printnl($message) { echo "$message\r\n"; } +// added a clear barrier between messages +function printBarrier() +{ + echo "\r\n"; + echo "=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-="; + echo "\r\n"; +} +// add in-message barrier +function printInMessageBarrier() +{ + echo "\r\n"; + echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"; + echo "\r\n"; +} +function printnl($message) +{ + echo "$message\r\n"; +} + +// include the PlancakeEmailParser class require_once(dirname(__DIR__) . DIRECTORY_SEPARATOR . "PlancakeEmailParser.php"); +// fetch all sample emails $emails = glob(__DIR__ . DIRECTORY_SEPARATOR . "emails" . DIRECTORY_SEPARATOR . "*"); +// start output with a barrier printBarrier(); +// after correction, getHTMLBody doesnt include barrier in output foreach ($emails as $email) { printnl("Email $email"); $emailParser = new PlancakeEmailParser(file_get_contents($email)); printnl("subject: " . $emailParser->getSubject()); + printInMessageBarrier(); printnl("body: " . $emailParser->getBody()); + printInMessageBarrier(); + printnl("plain body: " . $emailParser->getPlainBody()); + printInMessageBarrier(); + printnl("html body: " . $emailParser->getHTMLBody()); printBarrier(); }