This commit is contained in:
2025-05-12 14:25:25 +02:00
parent ab2db755ef
commit 9e378ca2b7
2719 changed files with 46505 additions and 60181 deletions

View File

@@ -26,7 +26,7 @@ final class SMimeEncrypter extends SMime
* @param string|string[] $certificate The path (or array of paths) of the file(s) containing the X.509 certificate(s)
* @param int|null $cipher A set of algorithms used to encrypt the message. Must be one of these PHP constants: https://www.php.net/manual/en/openssl.ciphers.php
*/
public function __construct(string|array $certificate, int $cipher = null)
public function __construct(string|array $certificate, ?int $cipher = null)
{
if (!\extension_loaded('openssl')) {
throw new \LogicException('PHP extension "openssl" is required to use SMime.');

View File

@@ -31,7 +31,7 @@ final class SMimeSigner extends SMime
* @param string|null $extraCerts The path of the file containing intermediate certificates (in PEM format) needed by the signing certificate
* @param int|null $signOptions Bitwise operator options for openssl_pkcs7_sign() (@see https://secure.php.net/manual/en/openssl.pkcs7.flags.php)
*/
public function __construct(string $certificate, string $privateKey, string $privateKeyPassphrase = null, string $extraCerts = null, int $signOptions = null)
public function __construct(string $certificate, string $privateKey, ?string $privateKeyPassphrase = null, ?string $extraCerts = null, ?int $signOptions = null)
{
if (!\extension_loaded('openssl')) {
throw new \LogicException('PHP extension "openssl" is required to use SMime.');

View File

@@ -19,7 +19,7 @@ use Symfony\Component\Mime\Part\AbstractPart;
*/
class DraftEmail extends Email
{
public function __construct(Headers $headers = null, AbstractPart $body = null)
public function __construct(?Headers $headers = null, ?AbstractPart $body = null)
{
parent::__construct($headers, $body);

View File

@@ -120,6 +120,10 @@ class Email extends Message
*/
public function from(Address|string ...$addresses): static
{
if (!$addresses) {
throw new LogicException('"from()" must be called with at least one address.');
}
return $this->setListAddressHeaderBody('From', $addresses);
}
@@ -325,7 +329,7 @@ class Email extends Message
*
* @return $this
*/
public function attach($body, string $name = null, string $contentType = null): static
public function attach($body, ?string $name = null, ?string $contentType = null): static
{
return $this->addPart(new DataPart($body, $name, $contentType));
}
@@ -333,7 +337,7 @@ class Email extends Message
/**
* @return $this
*/
public function attachFromPath(string $path, string $name = null, string $contentType = null): static
public function attachFromPath(string $path, ?string $name = null, ?string $contentType = null): static
{
return $this->addPart(new DataPart(new File($path), $name, $contentType));
}
@@ -343,7 +347,7 @@ class Email extends Message
*
* @return $this
*/
public function embed($body, string $name = null, string $contentType = null): static
public function embed($body, ?string $name = null, ?string $contentType = null): static
{
return $this->addPart((new DataPart($body, $name, $contentType))->asInline());
}
@@ -351,7 +355,7 @@ class Email extends Message
/**
* @return $this
*/
public function embedFromPath(string $path, string $name = null, string $contentType = null): static
public function embedFromPath(string $path, ?string $name = null, ?string $contentType = null): static
{
return $this->addPart((new DataPart(new File($path), $name, $contentType))->asInline());
}
@@ -412,7 +416,7 @@ class Email extends Message
private function ensureBodyValid(): void
{
if (null === $this->text && null === $this->html && !$this->attachments) {
if (null === $this->text && null === $this->html && !$this->attachments && null === parent::getBody()) {
throw new LogicException('A message must have a text or an HTML part or attachments.');
}
}

View File

@@ -28,7 +28,7 @@ class FileinfoMimeTypeGuesser implements MimeTypeGuesserInterface
*
* @see http://www.php.net/manual/en/function.finfo-open.php
*/
public function __construct(string $magicFile = null)
public function __construct(?string $magicFile = null)
{
$this->magicFile = $magicFile;
}

View File

@@ -188,6 +188,20 @@ abstract class AbstractHeader implements HeaderInterface
$tokens[] = $encodedToken;
}
foreach ($tokens as $i => $token) {
// whitespace(s) between 2 encoded tokens
if (
0 < $i
&& isset($tokens[$i + 1])
&& preg_match('~^[\t ]+$~', $token)
&& $this->tokenNeedsEncoding($tokens[$i - 1])
&& $this->tokenNeedsEncoding($tokens[$i + 1])
) {
$tokens[$i - 1] .= $token.$tokens[$i + 1];
array_splice($tokens, $i, 2);
}
}
return $tokens;
}
@@ -237,7 +251,7 @@ abstract class AbstractHeader implements HeaderInterface
/**
* Generate a list of all tokens in the final header.
*/
protected function toTokens(string $string = null): array
protected function toTokens(?string $string = null): array
{
$string ??= $this->getBodyAsString();

View File

@@ -190,7 +190,7 @@ final class Headers
return array_shift($values);
}
public function all(string $name = null): iterable
public function all(?string $name = null): iterable
{
if (null === $name) {
foreach ($this->headers as $name => $collection) {

View File

@@ -85,7 +85,7 @@ final class ParameterizedHeader extends UnstructuredHeader
* This doesn't need to be overridden in theory, but it is for implementation
* reasons to prevent potential breakage of attributes.
*/
protected function toTokens(string $string = null): array
protected function toTokens(?string $string = null): array
{
$tokens = parent::toTokens(parent::getBodyAsString());

View File

@@ -57,6 +57,6 @@ final class PathHeader extends AbstractHeader
public function getBodyAsString(): string
{
return '<'.$this->address->toString().'>';
return '<'.$this->address->getEncodedAddress().'>';
}
}

View File

@@ -24,7 +24,7 @@ class Message extends RawMessage
private Headers $headers;
private ?AbstractPart $body;
public function __construct(Headers $headers = null, AbstractPart $body = null)
public function __construct(?Headers $headers = null, ?AbstractPart $body = null)
{
$this->headers = $headers ? clone $headers : new Headers();
$this->body = $body;
@@ -42,7 +42,7 @@ class Message extends RawMessage
/**
* @return $this
*/
public function setBody(AbstractPart $body = null): static
public function setBody(?AbstractPart $body = null): static
{
if (1 > \func_num_args()) {
trigger_deprecation('symfony/mime', '6.2', 'Calling "%s()" without any arguments is deprecated, pass null explicitly instead.', __METHOD__);
@@ -130,11 +130,11 @@ class Message extends RawMessage
*/
public function ensureValidity()
{
if (!$this->headers->has('To') && !$this->headers->has('Cc') && !$this->headers->has('Bcc')) {
if (!$this->headers->get('To')?->getBody() && !$this->headers->get('Cc')?->getBody() && !$this->headers->get('Bcc')?->getBody()) {
throw new LogicException('An email must have a "To", "Cc", or "Bcc" header.');
}
if (!$this->headers->has('From') && !$this->headers->has('Sender')) {
if (!$this->headers->get('From')?->getBody() && !$this->headers->get('Sender')?->getBody()) {
throw new LogicException('An email must have a "From" or a "Sender" header.');
}
@@ -146,7 +146,10 @@ class Message extends RawMessage
if ($this->headers->has('Sender')) {
$sender = $this->headers->get('Sender')->getAddress();
} elseif ($this->headers->has('From')) {
$sender = $this->headers->get('From')->getAddresses()[0];
if (!$froms = $this->headers->get('From')->getAddresses()) {
throw new LogicException('A "From" header must have at least one email address.');
}
$sender = $froms[0];
} else {
throw new LogicException('An email must have a "From" or a "Sender" header.');
}

View File

@@ -29,7 +29,7 @@ class DataPart extends TextPart
/**
* @param resource|string|File $body Use a File instance to defer loading the file until rendering
*/
public function __construct($body, string $filename = null, string $contentType = null, string $encoding = null)
public function __construct($body, ?string $filename = null, ?string $contentType = null, ?string $encoding = null)
{
if ($body instanceof File && !$filename) {
$filename = $body->getFilename();
@@ -47,7 +47,7 @@ class DataPart extends TextPart
$this->setDisposition('attachment');
}
public static function fromPath(string $path, string $name = null, string $contentType = null): self
public static function fromPath(string $path, ?string $name = null, ?string $contentType = null): self
{
return new self(new File($path), $name, $contentType);
}

View File

@@ -13,7 +13,6 @@ namespace Symfony\Component\Mime\Part\Multipart;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Part\AbstractMultipartPart;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\TextPart;
/**
@@ -26,7 +25,7 @@ final class FormDataPart extends AbstractMultipartPart
private array $fields = [];
/**
* @param array<string|array|DataPart> $fields
* @param array<string|array|TextPart> $fields
*/
public function __construct(array $fields = [])
{

View File

@@ -40,7 +40,7 @@ class TextPart extends AbstractPart
/**
* @param resource|string|File $body Use a File instance to defer loading the file until rendering
*/
public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', string $encoding = null)
public function __construct($body, ?string $charset = 'utf-8', string $subtype = 'plain', ?string $encoding = null)
{
parent::__construct();
@@ -123,7 +123,11 @@ class TextPart extends AbstractPart
public function getBody(): string
{
if ($this->body instanceof File) {
return file_get_contents($this->body->getPath());
if (false === $ret = @file_get_contents($this->body->getPath())) {
throw new InvalidArgumentException(error_get_last()['message']);
}
return $ret;
}
if (null === $this->seekable) {

View File

@@ -18,20 +18,35 @@ use Symfony\Component\Mime\Exception\LogicException;
*/
class RawMessage
{
private iterable|string|null $message = null;
/** @var iterable<string>|string|resource */
private $message;
private bool $isGeneratorClosed;
public function __construct(iterable|string $message)
/**
* @param iterable<string>|string|resource $message
*/
public function __construct(mixed $message)
{
$this->message = $message;
}
public function __destruct()
{
if (\is_resource($this->message)) {
fclose($this->message);
}
}
public function toString(): string
{
if (\is_string($this->message)) {
return $this->message;
}
if (\is_resource($this->message)) {
return stream_get_contents($this->message, -1, 0);
}
$message = '';
foreach ($this->message as $chunk) {
$message .= $chunk;
@@ -53,10 +68,19 @@ class RawMessage
return;
}
if (\is_resource($this->message)) {
rewind($this->message);
while ($line = fgets($this->message)) {
yield $line;
}
return;
}
if ($this->message instanceof \Generator) {
$message = '';
$message = fopen('php://temp', 'w+');
foreach ($this->message as $chunk) {
$message .= $chunk;
fwrite($message, $chunk);
yield $chunk;
}
$this->isGeneratorClosed = !$this->message->valid();

View File

@@ -20,7 +20,7 @@ final class EmailAttachmentCount extends Constraint
private int $expectedValue;
private ?string $transport;
public function __construct(int $expectedValue, string $transport = null)
public function __construct(int $expectedValue, ?string $transport = null)
{
$this->expectedValue = $expectedValue;
$this->transport = $transport;

View File

@@ -26,16 +26,17 @@
"league/html-to-markdown": "^5.0",
"phpdocumentor/reflection-docblock": "^3.0|^4.0|^5.0",
"symfony/dependency-injection": "^5.4|^6.0|^7.0",
"symfony/process": "^5.4|^6.4|^7.0",
"symfony/property-access": "^5.4|^6.0|^7.0",
"symfony/property-info": "^5.4|^6.0|^7.0",
"symfony/serializer": "^6.3.2|^7.0"
"symfony/serializer": "^6.4.3|^7.0.3"
},
"conflict": {
"egulias/email-validator": "~3.0.0",
"phpdocumentor/reflection-docblock": "<3.2.2",
"phpdocumentor/type-resolver": "<1.4.0",
"symfony/mailer": "<5.4",
"symfony/serializer": "<6.3.2"
"symfony/serializer": "<6.4.3|>7.0,<7.0.3"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Mime\\": "" },