This repository has been archived on 2023-08-20. You can view files and clone it, but cannot push or open issues or pull requests.
symfony/src/Symfony/Component/Mailer/Transport/AbstractTransport.php

118 lines
3.2 KiB
PHP
Raw Normal View History

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;
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;
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();
}
/**
* 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 {
$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, (string) $this);
2019-02-06 11:34:55 +00:00
$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);
}
}