Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modified logic to avoid returning boundary in HTMLBody #2

Merged
merged 3 commits into from
Mar 9, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/vendor/
/composer.lock
20 changes: 12 additions & 8 deletions PlancakeEmailParser.php
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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], '"'));
}
Expand All @@ -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;
}
}
Expand All @@ -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);
}

Expand Down
3 changes: 3 additions & 0 deletions composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
"require": {
"php": ">=5.3.0"
},
"require-dev": {
"squizlabs/php_codesniffer": "~2.5.1"
},
"autoload": {
"psr-0": { "PlancakeEmailParser": "." }
}
Expand Down
30 changes: 28 additions & 2 deletions tests/run_tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}