Skip to content

Commit

Permalink
bug fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
arukompas committed Aug 25, 2023
1 parent f9d2239 commit 038866c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 28 deletions.
56 changes: 29 additions & 27 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,25 @@ public function getAttachments(): array
return array_filter($this->parts, fn ($part) => $part->isAttachment());
}

public function toArray(): array
{
return [
'id' => $this->getId(),
'subject' => $this->getSubject(),
'from' => $this->getFrom(),
'to' => $this->getTo(),
'reply_to' => $this->getReplyTo(),
'date' => $this->getDate() ? $this->getDate()->format('c') : null,
'headers' => $this->getHeaders(),
'parts' => array_map(fn ($part) => $part->toArray(), $this->getParts()),
];
}

public function jsonSerialize(): mixed
{
return $this->toArray();
}

protected function parse()
{
// Parse the email message into headers and body
Expand All @@ -129,22 +148,24 @@ protected function parse()
$currentBodyHeaderInProgress = null;

foreach ($lines as $line) {
$line = rtrim($line, "\r\n");

if ($headerInProgress) {
$this->headers[$headerInProgress] .= "\n" . rtrim($line);
$headerInProgress = str_ends_with($line, ';');
$this->headers[$headerInProgress] .= "\n" . $line;
$headerInProgress = str_ends_with($this->headers[$headerInProgress], ';');
continue;
}

if ($currentBodyHeaderInProgress) {
$currentBodyHeaders[$currentBodyHeaderInProgress] .= "\n" . rtrim($line);
$currentBodyHeaderInProgress = str_ends_with($line, ';');
$currentBodyHeaders[$currentBodyHeaderInProgress] .= "\n" . $line;
$currentBodyHeaderInProgress = str_ends_with($currentBodyHeaders[$currentBodyHeaderInProgress], ';');
continue;
}

if (isset($this->boundary) && $line === '--'.$this->boundary.'--') {
// We've reached the end of the message
$this->addPart($currentBody, $currentBodyHeaders);
continue;
break;
}

if (isset($this->boundary) && $line === '--'.$this->boundary) {
Expand All @@ -160,7 +181,7 @@ protected function parse()
}

if ($collectingBody && preg_match('/^(?<key>[A-Za-z\-0-9]+): (?<value>.*)$/', $line, $matches)) {
$currentBodyHeaders[$matches['key']] = rtrim($matches['value']);
$currentBodyHeaders[$matches['key']] = $matches['value'];

// if the last character is a semicolon, then the header is continued on the next line
if (str_ends_with($currentBodyHeaders[$matches['key']], ';')) {
Expand All @@ -182,11 +203,11 @@ protected function parse()
}

if (preg_match('/^(?<key>[A-Za-z\-0-9]+): (?<value>.*)$/', $line, $matches)) {
$this->headers[$matches['key']] = rtrim($matches['value']);
$this->headers[$matches['key']] = $matches['value'];

// if the last character is a semicolon, then the header is continued on the next line
if (str_ends_with($this->headers[$matches['key']], ';')) {
$headerInProgress = rtrim($matches['key']);
$headerInProgress = $matches['key'];
}

continue;
Expand All @@ -198,23 +219,4 @@ protected function addPart(string $currentBody, array $currentBodyHeaders): void
{
$this->parts[] = new MessagePart(trim($currentBody), $currentBodyHeaders);
}

public function toArray(): array
{
return [
'id' => $this->getId(),
'subject' => $this->getSubject(),
'from' => $this->getFrom(),
'to' => $this->getTo(),
'reply_to' => $this->getReplyTo(),
'date' => $this->getDate() ? $this->getDate()->format('c') : null,
'headers' => $this->getHeaders(),
'parts' => array_map(fn ($part) => $part->toArray(), $this->getParts()),
];
}

public function jsonSerialize(): mixed
{
return $this->toArray();
}
}
7 changes: 6 additions & 1 deletion src/MessagePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

namespace Opcodes\MailParser;

class MessagePart
class MessagePart implements \JsonSerializable
{
protected string $content;

Expand Down Expand Up @@ -79,4 +79,9 @@ public function toArray(): array
'filename' => $this->getFilename(),
];
}

public function jsonSerialize(): mixed
{
return $this->toArray();
}
}

0 comments on commit 038866c

Please sign in to comment.