2019-02-06 11:34:55 +00:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This file is part of the Symfony package.
|
|
|
|
*
|
|
|
|
* (c) Fabien Potencier <fabien@symfony.com>
|
|
|
|
*
|
|
|
|
* For the full copyright and license information, please view the LICENSE
|
|
|
|
* file that was distributed with this source code.
|
|
|
|
*/
|
|
|
|
|
|
|
|
namespace Symfony\Component\Mailer\Transport;
|
|
|
|
|
|
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
use Psr\Log\NullLogger;
|
|
|
|
use Symfony\Component\EventDispatcher\EventDispatcher;
|
2019-05-31 14:32:37 +01:00
|
|
|
use Symfony\Component\Mailer\DelayedSmtpEnvelope;
|
2019-02-06 11:34:55 +00:00
|
|
|
use Symfony\Component\Mailer\Event\MessageEvent;
|
|
|
|
use Symfony\Component\Mailer\Exception\TransportException;
|
|
|
|
use Symfony\Component\Mailer\SentMessage;
|
|
|
|
use Symfony\Component\Mailer\SmtpEnvelope;
|
|
|
|
use Symfony\Component\Mime\Address;
|
|
|
|
use Symfony\Component\Mime\RawMessage;
|
2019-06-08 13:53:03 +01:00
|
|
|
use Symfony\Contracts\EventDispatcher\EventDispatcherInterface;
|
2019-02-06 11:34:55 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @author Fabien Potencier <fabien@symfony.com>
|
|
|
|
*/
|
|
|
|
abstract class AbstractTransport implements TransportInterface
|
|
|
|
{
|
|
|
|
private $dispatcher;
|
|
|
|
private $logger;
|
|
|
|
private $rate = 0;
|
|
|
|
private $lastSent = 0;
|
|
|
|
|
|
|
|
public function __construct(EventDispatcherInterface $dispatcher = null, LoggerInterface $logger = null)
|
|
|
|
{
|
|
|
|
$this->dispatcher = $dispatcher ?: new EventDispatcher();
|
|
|
|
$this->logger = $logger ?: new NullLogger();
|
|
|
|
}
|
|
|
|
|
2019-08-04 04:52:49 +01:00
|
|
|
abstract public function getName(): string;
|
|
|
|
|
2019-02-06 11:34:55 +00:00
|
|
|
/**
|
|
|
|
* Sets the maximum number of messages to send per second (0 to disable).
|
|
|
|
*/
|
|
|
|
public function setMaxPerSecond(float $rate): self
|
|
|
|
{
|
|
|
|
if (0 >= $rate) {
|
|
|
|
$rate = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->rate = $rate;
|
|
|
|
$this->lastSent = 0;
|
|
|
|
|
|
|
|
return $this;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function send(RawMessage $message, SmtpEnvelope $envelope = null): ?SentMessage
|
|
|
|
{
|
|
|
|
$message = clone $message;
|
|
|
|
if (null !== $envelope) {
|
|
|
|
$envelope = clone $envelope;
|
|
|
|
} else {
|
|
|
|
try {
|
2019-05-31 14:32:37 +01:00
|
|
|
$envelope = new DelayedSmtpEnvelope($message);
|
2019-02-06 11:34:55 +00:00
|
|
|
} catch (\Exception $e) {
|
|
|
|
throw new TransportException('Cannot send message without a valid envelope.', 0, $e);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$event = new MessageEvent($message, $envelope);
|
|
|
|
$this->dispatcher->dispatch($event);
|
|
|
|
$envelope = $event->getEnvelope();
|
|
|
|
if (!$envelope->getRecipients()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
$message = new SentMessage($event->getMessage(), $envelope);
|
|
|
|
$this->doSend($message);
|
|
|
|
|
|
|
|
$this->checkThrottling();
|
|
|
|
|
|
|
|
return $message;
|
|
|
|
}
|
|
|
|
|
|
|
|
abstract protected function doSend(SentMessage $message): void;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param Address[] $addresses
|
|
|
|
*
|
|
|
|
* @return string[]
|
|
|
|
*/
|
|
|
|
protected function stringifyAddresses(array $addresses): array
|
|
|
|
{
|
2019-06-13 12:03:18 +01:00
|
|
|
return array_map(function (Address $a) {
|
2019-02-06 11:34:55 +00:00
|
|
|
return $a->toString();
|
|
|
|
}, $addresses);
|
|
|
|
}
|
|
|
|
|
|
|
|
protected function getLogger(): LoggerInterface
|
|
|
|
{
|
|
|
|
return $this->logger;
|
|
|
|
}
|
|
|
|
|
|
|
|
private function checkThrottling()
|
|
|
|
{
|
|
|
|
if (0 == $this->rate) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$sleep = (1 / $this->rate) - (microtime(true) - $this->lastSent);
|
|
|
|
if (0 < $sleep) {
|
|
|
|
$this->logger->debug(sprintf('Email transport "%s" sleeps for %.2f seconds', __CLASS__, $sleep));
|
|
|
|
usleep($sleep * 1000000);
|
|
|
|
}
|
|
|
|
$this->lastSent = microtime(true);
|
|
|
|
}
|
|
|
|
}
|