diff --git a/src/Symfony/Component/Mime/RawMessage.php b/src/Symfony/Component/Mime/RawMessage.php index 62f3491ba12e4..484ffb0073f6a 100644 --- a/src/Symfony/Component/Mime/RawMessage.php +++ b/src/Symfony/Component/Mime/RawMessage.php @@ -18,7 +18,8 @@ */ class RawMessage { - private iterable|string $message; + /** @var iterable|string|resource */ + private $message; private bool $isGeneratorClosed; public function __construct(iterable|string $message) @@ -26,12 +27,23 @@ public function __construct(iterable|string $message) $this->message = $message; } + public function __destruct() + { + if (\is_resource($this->message)) { + fclose($this->message); + } + } + public function toString(): string { if (\is_string($this->message)) { return $this->message; } + if (\is_resource($this->message)) { + return stream_get_contents($this->message, -1, 0); + } + $message = ''; foreach ($this->message as $chunk) { $message .= $chunk; @@ -53,10 +65,19 @@ public function toIterable(): iterable return; } + if (\is_resource($this->message)) { + rewind($this->message); + while ($line = fgets($this->message)) { + yield $line; + } + + return; + } + if ($this->message instanceof \Generator) { - $message = ''; + $message = fopen('php://temp', 'w+'); foreach ($this->message as $chunk) { - $message .= $chunk; + fwrite($message, $chunk); yield $chunk; } $this->isGeneratorClosed = !$this->message->valid();