Skip to content

Commit 51ab8b1

Browse files
committed
[Mime] deprecate attach/embed methods in favor of Email::addPart()
1 parent 7e56765 commit 51ab8b1

File tree

3 files changed

+10
-12
lines changed

3 files changed

+10
-12
lines changed

Mime/NotificationEmail.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
use Symfony\Component\ErrorHandler\Exception\FlattenException;
1515
use Symfony\Component\Mime\Header\Headers;
1616
use Symfony\Component\Mime\Part\AbstractPart;
17+
use Symfony\Component\Mime\Part\DataPart;
1718
use Twig\Extra\CssInliner\CssInlinerExtension;
1819
use Twig\Extra\Inky\InkyExtension;
1920
use Twig\Extra\Markdown\MarkdownExtension;
@@ -134,7 +135,7 @@ public function exception(\Throwable|FlattenException $exception): static
134135
$exceptionAsString = $this->getExceptionAsString($exception);
135136

136137
$this->context['exception'] = true;
137-
$this->attach($exceptionAsString, 'exception.txt', 'text/plain');
138+
$this->addPart(new DataPart($exceptionAsString, 'exception.txt', 'text/plain'));
138139
$this->importance(self::IMPORTANCE_URGENT);
139140

140141
if (!$this->getSubject()) {

Mime/WrappedTemplatedEmail.php

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
namespace Symfony\Bridge\Twig\Mime;
1313

1414
use Symfony\Component\Mime\Address;
15+
use Symfony\Component\Mime\Part\BodyFile;
16+
use Symfony\Component\Mime\Part\DataPart;
1517
use Twig\Environment;
1618

1719
/**
@@ -38,23 +40,17 @@ public function toName(): string
3840
public function image(string $image, string $contentType = null): string
3941
{
4042
$file = $this->twig->getLoader()->getSourceContext($image);
41-
if ($path = $file->getPath()) {
42-
$this->message->embedFromPath($path, $image, $contentType);
43-
} else {
44-
$this->message->embed($file->getCode(), $image, $contentType);
45-
}
43+
$body = $file->getPath() ? new BodyFile($file->getPath()) : $file->getCode();
44+
$this->message->addPart((new DataPart($body, $image, $contentType))->asInline());
4645

4746
return 'cid:'.$image;
4847
}
4948

5049
public function attach(string $file, string $name = null, string $contentType = null): void
5150
{
5251
$file = $this->twig->getLoader()->getSourceContext($file);
53-
if ($path = $file->getPath()) {
54-
$this->message->attachFromPath($path, $name, $contentType);
55-
} else {
56-
$this->message->attach($file->getCode(), $name, $contentType);
57-
}
52+
$body = $file->getPath() ? new BodyFile($file->getPath()) : $file->getCode();
53+
$this->message->addPart(new DataPart($body, $name, $contentType));
5854
}
5955

6056
/**

Tests/Mime/TemplatedEmailTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use PHPUnit\Framework\TestCase;
1515
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
16+
use Symfony\Component\Mime\Part\DataPart;
1617
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
1718
use Symfony\Component\Serializer\Encoder\JsonEncoder;
1819
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
@@ -58,7 +59,7 @@ public function testSymfonySerialize()
5859
$e->textTemplate('email.txt.twig');
5960
$e->htmlTemplate('email.html.twig');
6061
$e->context(['foo' => 'bar']);
61-
$e->attach('Some Text file', 'test.txt');
62+
$e->addPart(new DataPart('Some Text file', 'test.txt'));
6263
$expected = clone $e;
6364

6465
$expectedJson = <<<EOF

0 commit comments

Comments
 (0)