Description
Symfony version(s) affected
6.2 and other 6.*
Description
Trying to send a lot of Laravel 10 email notifications in loop, Sendmail transport runs out of memory:
Allowed memory size of 5242880000 bytes exhausted (tried to allocate 2673869000 bytes)
at vendor/symfony/mailer/Transport/Smtp/Stream/AbstractStream.php:37
33▕ public function write(string $bytes, bool $debug = true): void
34▕ {
35▕ if ($debug) {
36▕ foreach (explode("\n", trim($bytes)) as $line) {
➜ 37▕ $this->debug .= sprintf("> %s\n", $line);
38▕ }
39▕ }
40▕
41▕ $bytesToWrite = \strlen($bytes);
It seems the issue comes from vendor/symfony/mailer/Transport/SendmailTransport.php attempting to store in memory whole mail bodies as debugging information and not even clearing it when called in loop. E.g. SmtpTransport.php doesn't store the whole mail body in memory. I don't think production framework should do either by default.
How to reproduce
Call $user->notify($notification); in loop where notification uses MailMessage with /usr/sbin/sendmail -t -i -odq mailer.
Possible Solution
vendor/symfony/mailer/Transport/SendmailTransport.php
@@ -113,7 +113,7 @@
$this->stream->setCommand($command);
$this->stream->initialize();
foreach ($chunks as $chunk) {
- $this->stream->write($chunk);
+ $this->stream->write($chunk, false);
}
$this->stream->flush();
$this->stream->terminate();
Additional Context
No response