Skip to content

Commit

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

public function getSize(): int
{
return strlen($this->message);
}

public function toArray(): array
{
return [
Expand Down Expand Up @@ -148,7 +153,7 @@ protected function parse()
$currentBodyHeaderInProgress = null;

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

if ($headerInProgress) {
$this->headers[$headerInProgress] .= "\n" . $line;
Expand Down Expand Up @@ -192,7 +197,7 @@ protected function parse()
}

if ($collectingBody) {
$currentBody .= rtrim($line)."\n";
$currentBody .= $line."\n";
continue;
}

Expand All @@ -212,6 +217,9 @@ protected function parse()

continue;
}

// The line is not part of the email message. Let's remove it altogether.
$this->message = ltrim(substr($this->message, strlen($line)));
}
}

Expand Down
6 changes: 6 additions & 0 deletions src/MessagePart.php
Original file line number Diff line number Diff line change
Expand Up @@ -71,12 +71,18 @@ public function getFilename(): string
return '';
}

public function getSize(): int
{
return strlen($this->getContent());
}

public function toArray(): array
{
return [
'headers' => $this->getHeaders(),
'content' => $this->getContent(),
'filename' => $this->getFilename(),
'size' => $this->getSize(),
];
}

Expand Down
41 changes: 41 additions & 0 deletions tests/Unit/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -152,3 +152,44 @@
$attachments = $message->getAttachments();
expect($attachments)->toHaveCount(1);
});

it('skips initial content that is not part of the message', function () {
$messageString = <<<EOF
This is some initial content that is not part of the message.
From: [email protected]
To: [email protected]
Subject: This is an email with common headers
Date: Thu, 24 Aug 2023 21:15:01 PST
MIME-Version: 1.0
Content-Type: multipart/mixed; boundary="----=_Part_1_1234567890"
------=_Part_1_1234567890
Content-Type: text/html; charset="utf-8"
<html>
<head>
<title>This is an HTML email</title>
</head>
<body>
<h1>This is the HTML version of the email</h1>
</body>
</html>
------=_Part_1_1234567890--
EOF;

$message = Message::fromString($messageString);

expect($message->getFrom())->toBe('[email protected]')
->and($message->getHtmlPart()?->getContent())->toBe(<<<EOF
<html>
<head>
<title>This is an HTML email</title>
</head>
<body>
<h1>This is the HTML version of the email</h1>
</body>
</html>
EOF);
});

0 comments on commit 9cf221b

Please sign in to comment.