Skip to content

[Mime] support overwriting form encoding #49587

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: 7.4
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 7 additions & 3 deletions src/Symfony/Component/Mime/Part/DataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,12 @@ class DataPart extends TextPart
private $mediaType;
private $cid;

private $formEncoding;

/**
* @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, string $formEncoding = null)
{
unset($this->_parent);

Expand All @@ -42,16 +44,18 @@ public function __construct($body, string $filename = null, string $contentType

parent::__construct($body, null, $subtype, $encoding);

$this->formEncoding = $formEncoding ?? '8bit';

if (null !== $filename) {
$this->filename = $filename;
$this->setName($filename);
}
$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, string $encoding = null): self
{
return new self(new File($path), $name, $contentType);
return new self(new File($path), $name, $contentType, $encoding, $encoding);
}

/**
Expand Down
29 changes: 24 additions & 5 deletions src/Symfony/Component/Mime/Part/Multipart/FormDataPart.php
Original file line number Diff line number Diff line change
Expand Up @@ -94,16 +94,35 @@ private function preparePart(string $name, string|TextPart $value): TextPart

private function configurePart(string $name, TextPart $part): TextPart
{
static $r;

$r ??= new \ReflectionProperty(TextPart::class, 'encoding');

$part->setDisposition('form-data');
$part->setName($name);
// HTTP does not support \r\n in header values
$part->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
$r->setValue($part, '8bit');
if ($part instanceof DataPart) {
// data part may specify base64 encoding
$this->configureDataPartEncoding($part);
} else {
$this->configureTextPartEncoding($part);
}

return $part;
}

private function configureDataPartEncoding(DataPart $part)
{
static $r;

$r ??= new \ReflectionProperty(DataPart::class, 'formEncoding');
$formEncoding = $r->getValue($part);

$this->configureTextPartEncoding($part, $formEncoding);
}

private function configureTextPartEncoding(TextPart $part, $encoding = '8bit')
{
static $r;

$r ??= new \ReflectionProperty(TextPart::class, 'encoding');
$r->setValue($part, $encoding);
}
}
1 change: 1 addition & 0 deletions src/Symfony/Component/Mime/Tests/EmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,7 @@ public function testSymfonySerialize()
{
"filename": "test.txt",
"mediaType": "application",
"formEncoding": "8bit",
"body": "Some Text file",
"charset": null,
"subtype": "octet-stream",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,27 @@ public function testConstructor()
$this->assertEquals([$t, $b, $c], $f->getParts());
}

public function testConstructorWithBase64Encoding()
{
$r = new \ReflectionProperty(TextPart::class, 'encoding');

$d = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif', null, null, '8bit');
$e = DataPart::fromPath($file = __DIR__.'/../../Fixtures/mimetypes/test.gif', null, null, 'base64');
$f = new FormDataPart([
'foo' => clone $d,
'bar' => clone $e
]);
$d->setDisposition('form-data');
$d->setName('foo');
$d->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
$r->setValue($d, '8bit');
$e->setDisposition('form-data');
$e->setName('bar');
$e->getHeaders()->setMaxLineLength(\PHP_INT_MAX);
$r->setValue($e, 'base64');
$this->assertEquals([$d, $e], $f->getParts());
}

public function testNestedArrayParts()
{
$p1 = new TextPart('content', 'utf-8', 'plain', '8bit');
Expand Down