Skip to content

Commit

Permalink
fix text part extraction
Browse files Browse the repository at this point in the history
  • Loading branch information
arukompas committed Nov 19, 2023
1 parent 7668596 commit 639ef31
Show file tree
Hide file tree
Showing 4 changed files with 64 additions and 4 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
vendor
composer.lock
.idea
.DS_Store
10 changes: 6 additions & 4 deletions src/Message.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,9 +154,11 @@ public function jsonSerialize(): mixed
return $this->toArray();
}

protected function parse()
/**
* Parse the email message into headers and body parts.
*/
protected function parse(): void
{
// Parse the email message into headers and body
$lines = explode("\n", $this->message);
$headerInProgress = null;

Expand Down Expand Up @@ -217,8 +219,8 @@ protected function parse()
continue;
}

if (preg_match('/^Content-Type: multipart\/mixed; boundary=(?<boundary>.*)$/', $line, $matches)) {
$this->headers['Content-Type'] = 'multipart/mixed; boundary='.$matches['boundary'];
if (preg_match("/^Content-Type: (?<contenttype>multipart\/.*); boundary=(?<boundary>.*)$/", $line, $matches)) {
$this->headers['Content-Type'] = $matches['contenttype']."; boundary=".$matches['boundary'];
$this->boundary = trim($matches['boundary'], '"');
continue;
}
Expand Down
27 changes: 27 additions & 0 deletions tests/Fixtures/multiformat_email.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
From: Arunas Practice <[email protected]>
To: Arunas arukomp <[email protected]>
Reply-To: Arunas Practice <[email protected]>
Subject: Appointment confirmation
Message-ID: <[email protected]>
MIME-Version: 1.0
Date: Thu, 24 Aug 2023 14:51:14 +0100
Content-Type: multipart/alternative; boundary=s1NCDW_3

--s1NCDW_3
Content-Type: text/plain; charset=utf-8
Content-Transfer-Encoding: quoted-printable
Hi Arunas Skirius,
This is a confirmation of your appointment.
--s1NCDW_3
Content-Type: text/html; charset=utf-8
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>Title</title>
</head>
</html>
--s1NCDW_3--
30 changes: 30 additions & 0 deletions tests/Unit/MessageTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,36 @@
]);
});

it('can parse a multi-format mail message', function () {
$message = Message::fromFile(__DIR__ . '/../Fixtures/multiformat_email.eml');

expect($message->getFrom())->toBe('Arunas Practice <[email protected]>')
->and($message->getTo())->toBe('Arunas arukomp <[email protected]>')
->and($message->getReplyTo())->toBe('Arunas Practice <[email protected]>')
->and($message->getSubject())->toBe('Appointment confirmation')
->and($message->getId())->toBe('[email protected]')
->and($message->getDate()->format('Y-m-d H:i:s'))->toBe('2023-08-24 14:51:14')
->and($message->getBoundary())->toBe('s1NCDW_3');

$parts = $message->getParts();

expect($parts)->toHaveCount(2)
->and($parts[0]->getContentType())->toBe('text/plain; charset=utf-8')
->and($parts[0]->getHeaders())->toBe([
'Content-Type' => 'text/plain; charset=utf-8',
'Content-Transfer-Encoding' => 'quoted-printable',
])
->and($parts[1]->getContentType())->toBe('text/html; charset=utf-8')
->and($parts[1]->getHeaders())->toBe([
'Content-Type' => 'text/html; charset=utf-8',
'Content-Transfer-Encoding' => 'quoted-printable',
])->and($message->getTextPart()?->getContent())->toBe(<<<EOF
Hi Arunas Skirius,
This is a confirmation of your appointment.
EOF);

});

it('can get contents of an encoded part', function () {
$messageString = <<<EOF
From: [email protected]
Expand Down

0 comments on commit 639ef31

Please sign in to comment.