Skip to content

Commit

Permalink
[Mailer] Do not ping the SMTP server before sending every message
Browse files Browse the repository at this point in the history
  • Loading branch information
micheh authored and fabpot committed Feb 7, 2020
1 parent cd2dec3 commit 2817810
Show file tree
Hide file tree
Showing 2 changed files with 126 additions and 1 deletion.
Expand Up @@ -12,8 +12,12 @@
namespace Symfony\Component\Mailer\Tests\Transport\Smtp;

use PHPUnit\Framework\TestCase;
use Symfony\Component\Mailer\Envelope;
use Symfony\Component\Mailer\Transport\Smtp\SmtpTransport;
use Symfony\Component\Mailer\Transport\Smtp\Stream\AbstractStream;
use Symfony\Component\Mailer\Transport\Smtp\Stream\SocketStream;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\RawMessage;

class SmtpTransportTest extends TestCase
{
Expand All @@ -25,4 +29,95 @@ public function testToString()
$t = new SmtpTransport((new SocketStream())->setHost('127.0.0.1')->setPort(2525)->disableTls());
$this->assertEquals('smtp://127.0.0.1:2525', (string) $t);
}

public function testSendDoesNotPingBelowThreshold(): void
{
$stream = new DummyStream();
$envelope = new Envelope(new Address('sender@example.org'), [new Address('recipient@example.org')]);

$transport = new SmtpTransport($stream);
$transport->send(new RawMessage('Message 1'), $envelope);
$transport->send(new RawMessage('Message 2'), $envelope);
$transport->send(new RawMessage('Message 3'), $envelope);

$this->assertNotContains("NOOP\r\n", $stream->getCommands());
}

public function testSendDoesPingAboveThreshold(): void
{
$stream = new DummyStream();
$envelope = new Envelope(new Address('sender@example.org'), [new Address('recipient@example.org')]);

$transport = new SmtpTransport($stream);
$transport->setPingThreshold(1);

$transport->send(new RawMessage('Message 1'), $envelope);
$transport->send(new RawMessage('Message 2'), $envelope);

$this->assertNotContains("NOOP\r\n", $stream->getCommands());

$stream->clearCommands();
sleep(1);

$transport->send(new RawMessage('Message 3'), $envelope);
$this->assertContains("NOOP\r\n", $stream->getCommands());
}
}

class DummyStream extends AbstractStream
{
/**
* @var string
*/
private $nextResponse;

/**
* @var string[]
*/
private $commands;

public function initialize(): void
{
$this->nextResponse = '220 localhost';
}

public function write(string $bytes, $debug = true): void
{
$this->commands[] = $bytes;

if (0 === strpos($bytes, 'DATA')) {
$this->nextResponse = '354 Enter message, ending with "." on a line by itself';
} elseif (0 === strpos($bytes, 'QUIT')) {
$this->nextResponse = '221 Goodbye';
} else {
$this->nextResponse = '250 OK';
}
}

public function readLine(): string
{
return $this->nextResponse;
}

public function flush(): void
{
}

/**
* @return string[]
*/
public function getCommands(): array
{
return $this->commands;
}

public function clearCommands(): void
{
$this->commands = [];
}

protected function getReadConnectionDescription(): string
{
return 'null';
}
}
32 changes: 31 additions & 1 deletion src/Symfony/Component/Mailer/Transport/Smtp/SmtpTransport.php
Expand Up @@ -35,6 +35,8 @@ class SmtpTransport extends AbstractTransport
private $restartThreshold = 100;
private $restartThresholdSleep = 0;
private $restartCounter;
private $pingThreshold = 100;
private $lastMessageTime = 0;
private $stream;
private $domain = '[127.0.0.1]';

Expand Down Expand Up @@ -66,6 +68,28 @@ public function setRestartThreshold(int $threshold, int $sleep = 0): self
return $this;
}

/**
* Sets the minimum number of seconds required between two messages, before the server is pinged.
* If the transport wants to send a message and the time since the last message exceeds the specified threshold,
* the transport will ping the server first (NOOP command) to check if the connection is still alive.
* Otherwise the message will be sent without pinging the server first.
*
* Do not set the threshold too low, as the SMTP server may drop the connection if there are too many
* non-mail commands (like pinging the server with NOOP).
*
* By default, the threshold is set to 100 seconds.
*
* @param int $seconds The minimum number of seconds between two messages required to ping the server
*
* @return $this
*/
public function setPingThreshold(int $seconds): self
{
$this->pingThreshold = $seconds;

return $this;
}

/**
* Sets the name of the local domain that will be used in HELO.
*
Expand Down Expand Up @@ -160,7 +184,10 @@ public function executeCommand(string $command, array $codes): string

protected function doSend(SentMessage $message): void
{
$this->ping();
if (microtime(true) - $this->lastMessageTime > $this->pingThreshold) {
$this->ping();
}

if (!$this->started) {
$this->start();
}
Expand All @@ -183,6 +210,8 @@ protected function doSend(SentMessage $message): void
$e->appendDebug($this->stream->getDebug());

throw $e;
} finally {
$this->lastMessageTime = microtime(true);
}
}

Expand Down Expand Up @@ -213,6 +242,7 @@ private function start(): void
$this->assertResponseCode($this->getFullResponse(), [220]);
$this->doHeloCommand();
$this->started = true;
$this->lastMessageTime = 0;

$this->getLogger()->debug(sprintf('Email transport "%s" started', __CLASS__));
}
Expand Down

0 comments on commit 2817810

Please sign in to comment.