|
4 | 4 |
|
5 | 5 | namespace Modules\Smtp\Application\Storage; |
6 | 6 |
|
7 | | -use Modules\Smtp\Application\Mail\Parser; |
8 | | - |
9 | 7 | final class Message |
10 | 8 | { |
11 | 9 | public function __construct( |
@@ -45,21 +43,32 @@ public function setFrom(string $from): void |
45 | 43 |
|
46 | 44 | public function appendBody(string $body): void |
47 | 45 | { |
48 | | - $this->body .= \preg_replace("/^(\.\.)/m", '.', $body); |
| 46 | + // Handle escaped periods at the beginning of lines per SMTP spec |
| 47 | + $safeBody = \preg_replace("/^(\.\.)/m", '.', $body); |
| 48 | + |
| 49 | + // Ensure body is properly appended even with multi-byte characters |
| 50 | + $this->body .= $safeBody; |
49 | 51 | } |
50 | 52 |
|
51 | 53 | public function bodyHasEos(): bool |
52 | 54 | { |
53 | | - return \str_ends_with($this->body, "\r\n.\r\n"); |
| 55 | + // More robust check for end of stream marker |
| 56 | + // This handles potential encoding issues with multi-byte characters |
| 57 | + return \mb_substr($this->body, -5) === "\r\n.\r\n"; |
54 | 58 | } |
55 | 59 |
|
56 | 60 | public function getBody(): string |
57 | 61 | { |
58 | | - return \str_replace("\r\n.\r\n", '', $this->body); |
| 62 | + // Remove the end of stream marker in a way that's safe for multi-byte strings |
| 63 | + if ($this->bodyHasEos()) { |
| 64 | + return \mb_substr($this->body, 0, \mb_strlen($this->body) - 5); |
| 65 | + } |
| 66 | + |
| 67 | + return $this->body; |
59 | 68 | } |
60 | 69 |
|
61 | 70 | public function parse(): \Modules\Smtp\Application\Mail\Message |
62 | 71 | { |
63 | | - return (new Parser())->parse($this->getBody()); |
| 72 | + return ParserFactory::getInstance()->create()->parse($this->getBody(), $this->recipients); |
64 | 73 | } |
65 | 74 | } |
0 commit comments