|
348 | 348 | ->and($message->getParts()[1]->getContent())->toBe('This is a test string'); |
349 | 349 | }); |
350 | 350 |
|
| 351 | +it('can parse a nested multipart email with alternatives and attachments', function () { |
| 352 | + $message = Message::fromFile(__DIR__ . '/../Fixtures/multiformat_email_2.eml'); |
| 353 | + |
| 354 | + expect($message->getFrom())->toBe('Acme Corp <sender@acme.test>') |
| 355 | + ->and($message->getTo())->toBe('Jane Doe <jane@example.test>') |
| 356 | + ->and($message->getReplyTo())->toBe('Jane Doe <jane@example.test>') |
| 357 | + ->and($message->getSubject())->toBe('Order Confirmation for Jane Doe') |
| 358 | + ->and($message->getId())->toBe('a1b2c3d4e5f6a7b8c9d0e1f2a3b4c5d6@acme.test') |
| 359 | + ->and($message->getDate()->format('Y-m-d H:i:s'))->toBe('2025-04-15 10:30:00') |
| 360 | + ->and($message->getBoundary())->toBe('Xq3mB9fZ'); |
| 361 | + |
| 362 | + // The nested multipart/alternative should be flattened into its leaf parts, |
| 363 | + // so we expect: text/plain, text/html, and 3 PDF attachments = 5 parts |
| 364 | + $parts = $message->getParts(); |
| 365 | + expect($parts)->toHaveCount(5); |
| 366 | + |
| 367 | + // Text part from the nested multipart/alternative |
| 368 | + $textPart = $message->getTextPart(); |
| 369 | + expect($textPart)->not->toBeNull() |
| 370 | + ->and($textPart->getContentType())->toBe('text/plain; charset=utf-8') |
| 371 | + ->and($textPart->getContent())->toContain('Your order has been confirmed.') |
| 372 | + ->and($textPart->getContent())->toContain('Thank you for shopping with Acme Corp!'); |
| 373 | + |
| 374 | + // HTML part from the nested multipart/alternative |
| 375 | + $htmlPart = $message->getHtmlPart(); |
| 376 | + expect($htmlPart)->not->toBeNull() |
| 377 | + ->and($htmlPart->getContentType())->toBe('text/html; charset=utf-8') |
| 378 | + ->and($htmlPart->getContent())->toContain('Your order has been confirmed.') |
| 379 | + ->and($htmlPart->getContent())->toContain('</html>'); |
| 380 | + |
| 381 | + // 3 PDF attachments |
| 382 | + $attachments = $message->getAttachments(); |
| 383 | + expect($attachments)->toHaveCount(3) |
| 384 | + ->and($attachments[0]->getFilename())->toBe('receipt.pdf') |
| 385 | + ->and($attachments[0]->isAttachment())->toBeTrue() |
| 386 | + ->and($attachments[1]->getFilename())->toBe('invoice.pdf') |
| 387 | + ->and($attachments[1]->isAttachment())->toBeTrue() |
| 388 | + ->and($attachments[2]->getFilename())->toBe('terms.pdf') |
| 389 | + ->and($attachments[2]->isAttachment())->toBeTrue(); |
| 390 | +}); |
| 391 | + |
351 | 392 | it('still parses with a broken boundary', function () { |
352 | 393 | $messageString = <<<EOF |
353 | 394 | From: sender@example.com |
|
0 commit comments