Skip to content

Commit fded3cd

Browse files
committed
[Mailer] added support ffor debug info when using SMTP
1 parent d2f33d2 commit fded3cd

File tree

2 files changed

+33
-14
lines changed

2 files changed

+33
-14
lines changed

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

+14-13
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-
}
147+
try {
148+
$envelope = $message->getEnvelope();
149+
$this->doMailFromCommand($envelope->getSender()->toString());
150+
foreach ($envelope->getRecipients() as $recipient) {
151+
$this->doRcptToCommand($recipient->toString());
152+
}
153153

154-
$this->executeCommand("DATA\r\n", [354]);
155-
foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
156-
$this->stream->write($chunk);
154+
$this->executeCommand("DATA\r\n", [354]);
155+
foreach (AbstractStream::replace("\r\n.", "\r\n..", $message->toIterable()) as $chunk) {
156+
$this->stream->write($chunk, false);
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

+19-1
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,16 @@ abstract class AbstractStream
2828
protected $in;
2929
protected $out;
3030

31-
public function write(string $bytes): void
31+
private $debug = '';
32+
33+
public function write(string $bytes, $debug = true): void
3234
{
35+
if ($debug) {
36+
foreach (explode("\n", trim($bytes)) as $line) {
37+
$this->debug .= sprintf("> %s\n", $line);
38+
}
39+
}
40+
3341
$bytesToWrite = \strlen($bytes);
3442
$totalBytesWritten = 0;
3543
while ($totalBytesWritten < $bytesToWrite) {
@@ -74,9 +82,19 @@ public function readLine(): string
7482
}
7583
}
7684

85+
$this->debug .= sprintf('< %s', $line);
86+
7787
return $line;
7888
}
7989

90+
public function getDebug(): string
91+
{
92+
$debug = $this->debug;
93+
$this->debug = '';
94+
95+
return $debug;
96+
}
97+
8098
public static function replace(string $from, string $to, iterable $chunks): \Generator
8199
{
82100
if ('' === $from) {

0 commit comments

Comments
 (0)