Skip to content

Commit c391380

Browse files
Merge pull request #102 from genkgo/fix_double_html_content
when a message contains multiple html parts, extract the first one
2 parents a15a6a2 + 413326f commit c391380

File tree

3 files changed

+52
-1
lines changed

3 files changed

+52
-1
lines changed

src/MessageBodyCollection.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -396,7 +396,7 @@ private function extractFromMimePart(MultiPartInterface $parts): void
396396
$disposition = 'inline';
397397
}
398398

399-
if ($contentType === 'text/html' && $disposition === 'inline') {
399+
if ($this->html === '' && $contentType === 'text/html' && $disposition === 'inline') {
400400
$this->html = self::ensureHtmlCharset(
401401
(string)new MimeBodyDecodedStream($part),
402402
$charset
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
Subject: Hello World
2+
3+
4+
Cc: other <[email protected]>
5+
MIME-Version: 1.0
6+
Content-Type: multipart/alternative; boundary=GenkgoMailV2Part187e28bf3cb4
7+
8+
This is a multipart message in MIME format.
9+
10+
--GenkgoMailV2Part187e28bf3cb4
11+
Content-Type: text/plain; charset=UTF-8
12+
Content-Transfer-Encoding: 7bit
13+
14+
Hello World
15+
16+
--GenkgoMailV2Part187e28bf3cb4
17+
Content-Type: text/html; charset=UTF-8
18+
Content-Transfer-Encoding: 7bit
19+
20+
<html><head><meta http-equiv="Content-Type" content="text/html;
21+
charset=UTF-8"/></head><body><p>Hello World</p></body></html>
22+
23+
--GenkgoMailV2Part187e28bf3cb4
24+
Content-Type: text/html; charset=UTF-8
25+
Content-Transfer-Encoding: 7bit
26+
27+
<html><head><meta http-equiv="Content-Type" content="text/html;
28+
charset=UTF-8"/></head><body><p>Another Text</p></body></html>
29+
--GenkgoMailV2Part187e28bf3cb4--

test/Unit/MessageBodyCollectionTest.php

+22
Original file line numberDiff line numberDiff line change
@@ -617,4 +617,26 @@ public function it_should_accept_an_attachment_with_a_long_filename(): void
617617
$this->replaceBoundaries((string)$body->createMessage())
618618
);
619619
}
620+
621+
/**
622+
* @test
623+
*/
624+
public function it_should_extract_the_first_html_part(): void
625+
{
626+
$messageBodyCollection = MessageBodyCollection::extract(
627+
GenericMessage::fromString(
628+
\file_get_contents(__DIR__ . '/../Stub/MessageBodyCollection/double-html.eml')
629+
)
630+
);
631+
632+
$this->assertStringNotContainsString(
633+
'Another Text',
634+
$messageBodyCollection->getHtml()
635+
);
636+
637+
$this->assertStringContainsString(
638+
'Hello World',
639+
$messageBodyCollection->getHtml()
640+
);
641+
}
620642
}

0 commit comments

Comments
 (0)