8000 [Mailer] added support ffor debug info when using SMTP · symfony/symfony@b22dd6d · GitHub
[go: up one dir, main page]

Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit b22dd6d

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

File tree

2 files changed

+30
-13
lines changed

2 files changed

+30
-13
lines changed

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

Lines changed: 14 additions & 13 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-
}
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);
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)
0