|
12 | 12 |
|
13 | 13 | test('it can parse a standard EML message and read basic headers', function () { |
14 | 14 | $contents = <<<'EOT' |
15 | | -From: "John Doe" <[email protected]> |
16 | | -To: "Jane Roe" <[email protected]> |
17 | | -Subject: Test Subject |
18 | | -Date: Wed, 19 Feb 2025 12:34:56 -0500 |
19 | | - |
20 | | -MIME-Version: 1.0 |
21 | | -Content-Type: text/plain; charset="UTF-8" |
22 | | -
|
23 | | -Hello World |
24 | | -EOT; |
| 15 | + From: "John Doe" <[email protected]> |
| 16 | + To: "Jane Roe" <[email protected]> |
| 17 | + Subject: Test Subject |
| 18 | + Date: Wed, 19 Feb 2025 12:34:56 -0500 |
| 19 | + |
| 20 | + MIME-Version: 1.0 |
| 21 | + Content-Type: text/plain; charset="UTF-8" |
| 22 | + |
| 23 | + Hello World |
| 24 | + EOT; |
25 | 25 |
|
26 | 26 | $message = new FileMessage($contents); |
27 | 27 |
|
|
39 | 39 |
|
40 | 40 | test('it can parse HTML content in a multipart message', function () { |
41 | 41 | $contents = <<<'EOT' |
42 | | -From: "John Doe" <[email protected]> |
43 | | -To: "Jane Roe" <[email protected]> |
44 | | -Subject: HTML Email |
45 | | -Date: Wed, 19 Feb 2025 12:34:56 -0500 |
46 | | - |
47 | | -MIME-Version: 1.0 |
48 | | -Content-Type: multipart/alternative; |
49 | | - boundary="----BOUNDARY-ID----" |
50 | | -
|
51 | | -------BOUNDARY-ID---- |
52 | | -Content-Type: text/plain; charset="UTF-8" |
53 | | -
|
54 | | -Hello Plain |
55 | | -
|
56 | | -------BOUNDARY-ID---- |
57 | | -Content-Type: text/html; charset="UTF-8" |
58 | | -
|
59 | | -<html><body><p>Hello <strong>HTML</strong></p></body></html> |
60 | | -------BOUNDARY-ID------ |
61 | | -EOT; |
| 42 | + From: "John Doe" <[email protected]> |
| 43 | + To: "Jane Roe" <[email protected]> |
| 44 | + Subject: HTML Email |
| 45 | + Date: Wed, 19 Feb 2025 12:34:56 -0500 |
| 46 | + |
| 47 | + MIME-Version: 1.0 |
| 48 | + Content-Type: multipart/alternative; |
| 49 | + boundary="----BOUNDARY-ID----" |
| 50 | + |
| 51 | + ------BOUNDARY-ID---- |
| 52 | + Content-Type: text/plain; charset="UTF-8" |
| 53 | + |
| 54 | + Hello Plain |
| 55 | + |
| 56 | + ------BOUNDARY-ID---- |
| 57 | + Content-Type: text/html; charset="UTF-8" |
| 58 | + |
| 59 | + <html><body><p>Hello <strong>HTML</strong></p></body></html> |
| 60 | + ------BOUNDARY-ID------ |
| 61 | + EOT; |
62 | 62 |
|
63 | 63 | $message = new FileMessage($contents); |
64 | 64 |
|
|
69 | 69 | test('it can parse attachments', function () { |
70 | 70 | // Simple example with one attachment |
71 | 71 | $contents = <<<'EOT' |
72 | | -From: "John Doe" <[email protected]> |
73 | | -To: "Jane Roe" <[email protected]> |
74 | | -Subject: EML with Attachment |
75 | | -Date: Wed, 19 Feb 2025 12:34:56 -0500 |
76 | | - |
77 | | -MIME-Version: 1.0 |
78 | | -Content-Type: multipart/mixed; |
79 | | - boundary="----BOUNDARY-ID----" |
80 | | -
|
81 | | -------BOUNDARY-ID---- |
82 | | -Content-Type: text/plain; charset="UTF-8" |
83 | | -
|
84 | | -Hello with Attachment |
85 | | -
|
86 | | -------BOUNDARY-ID---- |
87 | | -Content-Type: application/pdf; name="file.pdf" |
88 | | -Content-Disposition: attachment; filename="file.pdf" |
89 | | -Content-Transfer-Encoding: base64 |
90 | | -
|
91 | | -JVBERi0xLjUKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggNCAgIC9GaWx0ZXIvQXNjaWlIYXgg |
92 | | -ICAgPj5zdHJlYW0Kc3R1ZmYKZW5kc3RyZWFtCmVuZG9iajAK |
93 | | -------BOUNDARY-ID------ |
94 | | -EOT; |
| 72 | + From: "John Doe" <[email protected]> |
| 73 | + To: "Jane Roe" <[email protected]> |
| 74 | + Subject: EML with Attachment |
| 75 | + Date: Wed, 19 Feb 2025 12:34:56 -0500 |
| 76 | + |
| 77 | + MIME-Version: 1.0 |
| 78 | + Content-Type: multipart/mixed; |
| 79 | + boundary="----BOUNDARY-ID----" |
| 80 | + |
| 81 | + ------BOUNDARY-ID---- |
| 82 | + Content-Type: text/plain; charset="UTF-8" |
| 83 | + |
| 84 | + Hello with Attachment |
| 85 | + |
| 86 | + ------BOUNDARY-ID---- |
| 87 | + Content-Type: application/pdf; name="file.pdf" |
| 88 | + Content-Disposition: attachment; filename="file.pdf" |
| 89 | + Content-Transfer-Encoding: base64 |
| 90 | + |
| 91 | + JVBERi0xLjUKJeLjz9MKMyAwIG9iago8PC9MZW5ndGggNCAgIC9GaWx0ZXIvQXNjaWlIYXgg |
| 92 | + ICAgPj5zdHJlYW0Kc3R1ZmYKZW5kc3RyZWFtCmVuZG9iajAK |
| 93 | + ------BOUNDARY-ID------ |
| 94 | + EOT; |
95 | 95 |
|
96 | 96 | $message = new FileMessage($contents); |
97 | 97 |
|
|
109 | 109 |
|
110 | 110 | test('it recognizes when there are no attachments', function () { |
111 | 111 | $contents = <<<'EOT' |
112 | | -From: "John Doe" <[email protected]> |
113 | | -To: "Jane Roe" <[email protected]> |
114 | | -Subject: No Attachments |
115 | | -Date: Wed, 19 Feb 2025 12:34:56 -0500 |
116 | | -Content-Type: text/plain; charset="UTF-8" |
117 | | -
|
118 | | -Just a plain text email without attachments. |
119 | | -EOT; |
| 112 | + From: "John Doe" <[email protected]> |
| 113 | + To: "Jane Roe" <[email protected]> |
| 114 | + Subject: No Attachments |
| 115 | + Date: Wed, 19 Feb 2025 12:34:56 -0500 |
| 116 | + Content-Type: text/plain; charset="UTF-8" |
| 117 | + |
| 118 | + Just a plain text email without attachments. |
| 119 | + EOT; |
120 | 120 |
|
121 | 121 | $message = new FileMessage($contents); |
122 | 122 |
|
|
127 | 127 |
|
128 | 128 | test('it can parse other header fields like IN-REPLY-TO', function () { |
129 | 129 | $contents = <<<'EOT' |
130 | | -From: "John Doe" <[email protected]> |
131 | | - |
132 | | -Subject: In-Reply-To Check |
133 | | -Date: Wed, 19 Feb 2025 12:34:56 -0500 |
134 | | -Content-Type: text/plain; charset="UTF-8" |
135 | | -
|
136 | | -Check the in-reply-to header |
137 | | -EOT; |
| 130 | + From: "John Doe" <[email protected]> |
| 131 | + |
| 132 | + Subject: In-Reply-To Check |
| 133 | + Date: Wed, 19 Feb 2025 12:34:56 -0500 |
| 134 | + Content-Type: text/plain; charset="UTF-8" |
| 135 | + |
| 136 | + Check the in-reply-to header |
| 137 | + EOT; |
138 | 138 |
|
139 | 139 | $message = new FileMessage($contents); |
140 | 140 |
|
|
145 | 145 |
|
146 | 146 | test('it can be cast to a string via __toString()', function () { |
147 | 147 | $contents = <<<'EOT' |
148 | | -From: "John Doe" <[email protected]> |
149 | | -Subject: Stringable Test |
150 | | -Date: Wed, 19 Feb 2025 12:34:56 -0500 |
151 | | -Content-Type: text/plain; charset="UTF-8" |
152 | | -
|
153 | | -Testing __toString |
154 | | -EOT; |
| 148 | + From: "John Doe" <[email protected]> |
| 149 | + Subject: Stringable Test |
| 150 | + Date: Wed, 19 Feb 2025 12:34:56 -0500 |
| 151 | + Content-Type: text/plain; charset="UTF-8" |
| 152 | + |
| 153 | + Testing __toString |
| 154 | + EOT; |
155 | 155 |
|
156 | 156 | $message = new FileMessage($contents); |
157 | 157 |
|
158 | 158 | expect((string) $message)->toBe($contents); |
159 | 159 | }); |
| 160 | + |
| 161 | +test('it can read inline attachment', function () { |
| 162 | + $contents = <<<'EOT' |
| 163 | + From: "Example Sender" <[email protected]> |
| 164 | + To: "Example Recipient" <[email protected]> |
| 165 | + Subject: Test Email With Inline Image |
| 166 | + MIME-Version: 1.0 |
| 167 | + Content-Type: multipart/related; boundary="BOUNDARY_STRING" |
| 168 | + |
| 169 | + --BOUNDARY_STRING |
| 170 | + Content-Type: text/html; charset=UTF-8 |
| 171 | + |
| 172 | + <html> |
| 173 | + <body> |
| 174 | + <p>This is a test email with an inline image:</p> |
| 175 | + <img src="cid:inline-image-id" alt="Inline Image" /> |
| 176 | + </body> |
| 177 | + </html> |
| 178 | + |
| 179 | + --BOUNDARY_STRING |
| 180 | + Content-Type: image/png; name="inline_image.png" |
| 181 | + Content-Transfer-Encoding: base64 |
| 182 | + Content-ID: <inline-image-id> |
| 183 | + Content-Disposition: inline; filename="inline_image.png" |
| 184 | + |
| 185 | + iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAADUlEQVR42mP8 |
| 186 | + z8BQDwABAgEA0xzY2QAAAABJRU5ErkJggg== |
| 187 | + |
| 188 | + --BOUNDARY_STRING-- |
| 189 | + EOT; |
| 190 | + |
| 191 | + $message = new FileMessage($contents); |
| 192 | + |
| 193 | + $attachments = $message->attachments(); |
| 194 | + |
| 195 | + expect($attachments)->toHaveCount(1); |
| 196 | + expect($attachments[0]->contentType())->toBe('image/png'); |
| 197 | + expect($attachments[0]->contentId())->toBe('inline-image-id'); |
| 198 | + expect($attachments[0]->filename())->toBe('inline_image.png'); |
| 199 | +}); |
0 commit comments