Skip to content

Commit

Permalink
Merge pull request #102 from genkgo/fix_double_html_content
Browse files Browse the repository at this point in the history
when a message contains multiple html parts, extract the first one
  • Loading branch information
frederikbosch authored Jul 27, 2022
2 parents a15a6a2 + 413326f commit c391380
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion src/MessageBodyCollection.php
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@ private function extractFromMimePart(MultiPartInterface $parts): void
$disposition = 'inline';
}

if ($contentType === 'text/html' && $disposition === 'inline') {
if ($this->html === '' && $contentType === 'text/html' && $disposition === 'inline') {
$this->html = self::ensureHtmlCharset(
(string)new MimeBodyDecodedStream($part),
$charset
Expand Down
29 changes: 29 additions & 0 deletions test/Stub/MessageBodyCollection/double-html.eml
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
Subject: Hello World
From: me <[email protected]>
To: you <[email protected]>
Cc: other <[email protected]>
MIME-Version: 1.0
Content-Type: multipart/alternative; boundary=GenkgoMailV2Part187e28bf3cb4

This is a multipart message in MIME format.

--GenkgoMailV2Part187e28bf3cb4
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 7bit
Hello World
--GenkgoMailV2Part187e28bf3cb4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<html><head><meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/></head><body><p>Hello World</p></body></html>

--GenkgoMailV2Part187e28bf3cb4
Content-Type: text/html; charset=UTF-8
Content-Transfer-Encoding: 7bit

<html><head><meta http-equiv="Content-Type" content="text/html;
charset=UTF-8"/></head><body><p>Another Text</p></body></html>
--GenkgoMailV2Part187e28bf3cb4--
22 changes: 22 additions & 0 deletions test/Unit/MessageBodyCollectionTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -617,4 +617,26 @@ public function it_should_accept_an_attachment_with_a_long_filename(): void
$this->replaceBoundaries((string)$body->createMessage())
);
}

/**
* @test
*/
public function it_should_extract_the_first_html_part(): void
{
$messageBodyCollection = MessageBodyCollection::extract(
GenericMessage::fromString(
\file_get_contents(__DIR__ . '/../Stub/MessageBodyCollection/double-html.eml')
)
);

$this->assertStringNotContainsString(
'Another Text',
$messageBodyCollection->getHtml()
);

$this->assertStringContainsString(
'Hello World',
$messageBodyCollection->getHtml()
);
}
}

0 comments on commit c391380

Please sign in to comment.