Skip to content

Commit 15c20c6

Browse files
committed
[Mailer] added support ffor debug info when using SMTP
1 parent cb3c237 commit 15c20c6

File tree

2 files changed

+31
-14
lines changed

2 files changed

+31
-14
lines changed

src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php

Lines changed: 15 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,6 @@ public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentM
135135
*/
136136
public function executeCommand(string $command, array $codes): string
137137
{
138-
$this->getLogger()->debug(sprintf('Email transport "%s" sent command "%s"', __CLASS__, trim($command)));
139138
$this->stream->write($command);
140139
$response = $this->getFullResponse();
141140
$this->assertResponseCode($response, $codes);
@@ -145,18 +144,22 @@ public function executeCommand(string $command, array $codes): string
145144

146145
protected function doSend(SentMessage $message): void
147146
{
148-
$envelope = $message->getEnvelope();
149-
$this->doMailFromCommand($envelope->getSender()->toString());
150-
foreach ($envelope->getRecipients() as $recipient) {
151-
$this->doRcptToCommand($recipient->toString());
152-
}
153-
154-
$this->executeCommand("DATA\r\n", [354]);
155-
foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
156-
$this->stream->write($chunk);
147+
try {
148+
$envelope = $message->getEnvelope();
149+
$this->doMailFromCommand($envelope->getSender()->toString());
150+
foreach ($envelope->getRecipients() as $recipient) {
151+
$this->doRcptToCommand($recipient->toString());
152+
}
153+
154+
$this->executeCommand("DATA\r\n", [354]);
155+
foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
156+
$this->stream->write($chunk);
157+
}
158+
$this->stream->flush();
159+
$this->executeCommand("\r\n.\r\n", [250]);
160+
} finally {
161+
$message->appendDebug($this->stream->getDebug());
157162
}
158-
$this->stream->flush();
159-
$this->executeCommand("\r\n.\r\n", [250]);
160163
}
161164

162165
protected function doHeloCommand(): void
@@ -237,8 +240,6 @@ private function assertResponseCode(string $response, array $codes): void
237240
list($code) = sscanf($response, '%3d');
238241
$valid = \in_array($code, $codes);
239242

240-
$this->getLogger()->debug(sprintf('Email transport "%s" received response "%s" (%s).', __CLASS__, trim($response), $valid ? 'ok' : 'error'));
241-
242243
if (!$valid) {
243244
throw new TransportException(sprintf('Expected response code "%s" but got code "%s", with message "%s".', implode('/', $codes), $code, trim($response)), $code);
244245
}

src/Symfony/Component/Mailer/Transport/Smtp/Stream/AbstractStream.php

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,14 @@ abstract class AbstractStream
2828
protected $in;
2929
protected $out;
3030

31+
private $debug = '';
32+
3133
public function write(string $bytes): void
3234
{
35+
foreach (explode("\n", trim($bytes)) as $line) {
36+
$this->debug .= sprintf("> %s\n", $line);
37+
}
38+
3339
$bytesToWrite = \strlen($bytes);
3440
$totalBytesWritten = 0;
3541
while ($totalBytesWritten < $bytesToWrite) {
@@ -74,9 +80,19 @@ public function readLine(): string
7480
}
7581
}
7682

83+
$this->debug .= sprintf("< %s", $line);
84+
7785
return $line;
7886
}
7987

88+
public function getDebug(): string
89+
{
90+
$debug = $this->debug;
91+
$this->debug = '';
92+
93+
return $debug;
94+
}
95+
8096
public static function replace(string $from, string $to, iterable $chunks): \Generator
8197
{
8298
if ('' === $from) {

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy