|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Zoon\BounceHandler\Tests; |
| 6 | + |
| 7 | +use PHPUnit\Framework\TestCase; |
| 8 | +use Zoon\BounceHandler\BounceHandler; |
| 9 | + |
| 10 | +final class MessageIdExtractionTest extends TestCase { |
| 11 | + private BounceHandler $handler; |
| 12 | + |
| 13 | + protected function setUp(): void { |
| 14 | + $this->handler = new BounceHandler(); |
| 15 | + } |
| 16 | + |
| 17 | + public function testMessageIdFromStandard3PartMime(): void { |
| 18 | + $eml = $this->buildEml([ |
| 19 | + 'Return-Path' => '<>', |
| 20 | + 'From' => 'mailer-daemon@example.com', |
| 21 | + 'Subject' => 'Mail delivery failed', |
| 22 | + 'Content-Type' => 'multipart/report; report-type=delivery-status; boundary="BOUND3PART"', |
| 23 | + ], implode("\r\n", [ |
| 24 | + '--BOUND3PART', |
| 25 | + 'Content-Type: text/plain', |
| 26 | + '', |
| 27 | + 'The message could not be delivered.', |
| 28 | + 'user@example.com: mailbox not found', |
| 29 | + '', |
| 30 | + '--BOUND3PART', |
| 31 | + 'Content-Type: message/delivery-status', |
| 32 | + '', |
| 33 | + 'Final-Recipient: rfc822;user@example.com', |
| 34 | + 'Action: failed', |
| 35 | + 'Status: 5.1.1', |
| 36 | + '', |
| 37 | + '--BOUND3PART', |
| 38 | + 'Content-Type: message/rfc822', |
| 39 | + '', |
| 40 | + 'Message-ID: <original-3part@example.com>', |
| 41 | + 'From: sender@example.com', |
| 42 | + 'To: user@example.com', |
| 43 | + 'Subject: Original Subject', |
| 44 | + '', |
| 45 | + 'Original body text', |
| 46 | + '--BOUND3PART--', |
| 47 | + ])); |
| 48 | + |
| 49 | + $results = $this->handler->parse($eml); |
| 50 | + |
| 51 | + self::assertCount(1, $results); |
| 52 | + self::assertSame('<original-3part@example.com>', $results[0]->messageId); |
| 53 | + self::assertSame('Original Subject', $results[0]->subject); |
| 54 | + } |
| 55 | + |
| 56 | + public function testMessageIdFrom2PartMimeFallback(): void { |
| 57 | + $eml = $this->buildEml([ |
| 58 | + 'Return-Path' => '<>', |
| 59 | + 'From' => 'mailer-daemon@example.com', |
| 60 | + 'Subject' => 'Mail delivery failed', |
| 61 | + 'Content-Type' => 'multipart/mixed; boundary="BOUND2PART"', |
| 62 | + ], implode("\r\n", [ |
| 63 | + '--BOUND2PART', |
| 64 | + '', |
| 65 | + 'Su mensaje no pudo ser entregado.', |
| 66 | + 'user@example.com: mailbox not found', |
| 67 | + '', |
| 68 | + '--- Mensaje original adjunto.', |
| 69 | + '', |
| 70 | + '--BOUND2PART', |
| 71 | + 'Content-Type: message/rfc822', |
| 72 | + '', |
| 73 | + 'Message-ID: <original-2part@example.com>', |
| 74 | + 'From: sender@example.com', |
| 75 | + 'To: user@example.com', |
| 76 | + 'Subject: Two Part Subject', |
| 77 | + '', |
| 78 | + 'Original body text', |
| 79 | + '--BOUND2PART--', |
| 80 | + ])); |
| 81 | + |
| 82 | + $results = $this->handler->parse($eml); |
| 83 | + |
| 84 | + self::assertCount(1, $results); |
| 85 | + self::assertSame('<original-2part@example.com>', $results[0]->messageId); |
| 86 | + self::assertSame('Two Part Subject', $results[0]->subject); |
| 87 | + } |
| 88 | + |
| 89 | + public function testMessageIdFromReferencesHeader(): void { |
| 90 | + $eml = $this->buildEml([ |
| 91 | + 'Return-Path' => '<>', |
| 92 | + 'From' => 'mailer-daemon@example.com', |
| 93 | + 'Subject' => 'Mail delivery failed', |
| 94 | + 'References' => '<ref-fallback@example.com>', |
| 95 | + 'X-Failed-Recipients' => 'user@example.com', |
| 96 | + ], implode("\r\n", [ |
| 97 | + 'This message was created automatically by mail delivery software.', |
| 98 | + '', |
| 99 | + 'A message that you sent could not be delivered.', |
| 100 | + '', |
| 101 | + ' user@example.com', |
| 102 | + ' mailbox is full', |
| 103 | + ])); |
| 104 | + |
| 105 | + $results = $this->handler->parse($eml); |
| 106 | + |
| 107 | + self::assertCount(1, $results); |
| 108 | + self::assertSame('<ref-fallback@example.com>', $results[0]->messageId); |
| 109 | + } |
| 110 | + |
| 111 | + public function testMessageIdFromInReplyToHeader(): void { |
| 112 | + $eml = $this->buildEml([ |
| 113 | + 'Return-Path' => '<>', |
| 114 | + 'From' => 'mailer-daemon@example.com', |
| 115 | + 'Subject' => 'Mail delivery failed', |
| 116 | + 'In-Reply-To' => '<inreply-fallback@example.com>', |
| 117 | + 'X-Failed-Recipients' => 'user@example.com', |
| 118 | + ], implode("\r\n", [ |
| 119 | + 'This message was created automatically by mail delivery software.', |
| 120 | + '', |
| 121 | + 'A message that you sent could not be delivered.', |
| 122 | + '', |
| 123 | + ' user@example.com', |
| 124 | + ' mailbox is full', |
| 125 | + ])); |
| 126 | + |
| 127 | + $results = $this->handler->parse($eml); |
| 128 | + |
| 129 | + self::assertCount(1, $results); |
| 130 | + self::assertSame('<inreply-fallback@example.com>', $results[0]->messageId); |
| 131 | + } |
| 132 | + |
| 133 | + public function testReferencesPreferredOverInReplyTo(): void { |
| 134 | + $eml = $this->buildEml([ |
| 135 | + 'Return-Path' => '<>', |
| 136 | + 'From' => 'mailer-daemon@example.com', |
| 137 | + 'Subject' => 'Mail delivery failed', |
| 138 | + 'References' => '<from-references@example.com>', |
| 139 | + 'In-Reply-To' => '<from-inreply@example.com>', |
| 140 | + 'X-Failed-Recipients' => 'user@example.com', |
| 141 | + ], implode("\r\n", [ |
| 142 | + 'This message was created automatically by mail delivery software.', |
| 143 | + '', |
| 144 | + ' user@example.com', |
| 145 | + ' mailbox is full', |
| 146 | + ])); |
| 147 | + |
| 148 | + $results = $this->handler->parse($eml); |
| 149 | + |
| 150 | + self::assertCount(1, $results); |
| 151 | + self::assertSame('<from-references@example.com>', $results[0]->messageId); |
| 152 | + } |
| 153 | + |
| 154 | + public function testOriginalLetterMessageIdPreferredOverReferences(): void { |
| 155 | + $eml = $this->buildEml([ |
| 156 | + 'Return-Path' => '<>', |
| 157 | + 'From' => 'mailer-daemon@example.com', |
| 158 | + 'Subject' => 'Mail delivery failed', |
| 159 | + 'References' => '<should-not-use@example.com>', |
| 160 | + 'Content-Type' => 'multipart/report; report-type=delivery-status; boundary="BOUNDPRIO"', |
| 161 | + ], implode("\r\n", [ |
| 162 | + '--BOUNDPRIO', |
| 163 | + 'Content-Type: text/plain', |
| 164 | + '', |
| 165 | + 'user@example.com: mailbox not found', |
| 166 | + '', |
| 167 | + '--BOUNDPRIO', |
| 168 | + 'Content-Type: message/delivery-status', |
| 169 | + '', |
| 170 | + 'Final-Recipient: rfc822;user@example.com', |
| 171 | + 'Action: failed', |
| 172 | + 'Status: 5.1.1', |
| 173 | + '', |
| 174 | + '--BOUNDPRIO', |
| 175 | + 'Content-Type: message/rfc822', |
| 176 | + '', |
| 177 | + 'Message-ID: <from-original@example.com>', |
| 178 | + 'From: sender@example.com', |
| 179 | + 'To: user@example.com', |
| 180 | + 'Subject: Test', |
| 181 | + '', |
| 182 | + 'Body', |
| 183 | + '--BOUNDPRIO--', |
| 184 | + ])); |
| 185 | + |
| 186 | + $results = $this->handler->parse($eml); |
| 187 | + |
| 188 | + self::assertCount(1, $results); |
| 189 | + self::assertSame('<from-original@example.com>', $results[0]->messageId); |
| 190 | + } |
| 191 | + |
| 192 | + public function testEmptyMessageIdWhenNoSourceAvailable(): void { |
| 193 | + $eml = $this->buildEml([ |
| 194 | + 'Return-Path' => '<>', |
| 195 | + 'From' => 'mailer-daemon@example.com', |
| 196 | + 'Subject' => 'Mail delivery failed', |
| 197 | + 'X-Failed-Recipients' => 'user@example.com', |
| 198 | + ], implode("\r\n", [ |
| 199 | + 'This message was created automatically by mail delivery software.', |
| 200 | + '', |
| 201 | + 'A message that you sent could not be delivered.', |
| 202 | + '', |
| 203 | + ' user@example.com', |
| 204 | + ' mailbox is full', |
| 205 | + ])); |
| 206 | + |
| 207 | + $results = $this->handler->parse($eml); |
| 208 | + |
| 209 | + self::assertCount(1, $results); |
| 210 | + self::assertSame('', $results[0]->messageId); |
| 211 | + } |
| 212 | + |
| 213 | + public function testReferencesWithMultipleMessageIds(): void { |
| 214 | + $eml = $this->buildEml([ |
| 215 | + 'Return-Path' => '<>', |
| 216 | + 'From' => 'mailer-daemon@example.com', |
| 217 | + 'Subject' => 'Mail delivery failed', |
| 218 | + 'References' => '<first@example.com> <second@example.com>', |
| 219 | + 'X-Failed-Recipients' => 'user@example.com', |
| 220 | + ], implode("\r\n", [ |
| 221 | + 'This message was created automatically by mail delivery software.', |
| 222 | + '', |
| 223 | + ' user@example.com', |
| 224 | + ' mailbox is full', |
| 225 | + ])); |
| 226 | + |
| 227 | + $results = $this->handler->parse($eml); |
| 228 | + |
| 229 | + self::assertCount(1, $results); |
| 230 | + self::assertSame('<first@example.com>', $results[0]->messageId); |
| 231 | + } |
| 232 | + |
| 233 | + public function testRealEmlFile1HasMessageId(): void { |
| 234 | + $emlPath = __DIR__ . '/../eml/1.eml'; |
| 235 | + if (!file_exists($emlPath)) { |
| 236 | + self::markTestSkipped('eml/1.eml not found'); |
| 237 | + } |
| 238 | + |
| 239 | + $results = $this->handler->parse(file_get_contents($emlPath)); |
| 240 | + |
| 241 | + self::assertGreaterThan(0, count($results)); |
| 242 | + self::assertSame( |
| 243 | + '<11885fd3ea338f04de950704ee6b30f5@ar1.outmailing.com>', |
| 244 | + $results[0]->messageId, |
| 245 | + ); |
| 246 | + } |
| 247 | + |
| 248 | + /** |
| 249 | + * @param array<string, string> $headers |
| 250 | + */ |
| 251 | + private function buildEml(array $headers, string $body): string { |
| 252 | + $lines = []; |
| 253 | + foreach ($headers as $name => $value) { |
| 254 | + $lines[] = "{$name}: {$value}"; |
| 255 | + } |
| 256 | + |
| 257 | + return implode("\r\n", $lines) . "\r\n\r\n" . $body; |
| 258 | + } |
| 259 | +} |
0 commit comments