Skip to content

Commit 7cdbd20

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

File tree

17 files changed

+92
-100
lines changed

17 files changed

+92
-100
lines changed

src/Symfony/Bridge/Twig/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()) {

src/Symfony/Bridge/Twig/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
/**

src/Symfony/Bridge/Twig/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

src/Symfony/Bundle/FrameworkBundle/Tests/Functional/Bundle/TestBundle/Controller/EmailController.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
use Symfony\Component\Mailer\MailerInterface;
1616
use Symfony\Component\Mime\Address;
1717
use Symfony\Component\Mime\Email;
18+
use Symfony\Component\Mime\Part\DataPart;
1819

1920
class EmailController
2021
{
@@ -25,15 +26,15 @@ public function indexAction(MailerInterface $mailer)
2526
->addCc('cc@symfony.com')
2627
->text('Bar!')
2728
->html('<p>Foo</p>')
28-
->attach(file_get_contents(__FILE__), 'foobar.php')
29+
->addPart(new DataPart(file_get_contents(__FILE__), 'foobar.php'))
2930
);
3031

3132
$mailer->send((new Email())->to('fabien@symfony.com', 'thomas@symfony.com')->from('fabien@symfony.com')->subject('Foo')
3233
->addReplyTo(new Address('me@symfony.com', 'Fabien Potencier'))
3334
->addCc('cc@symfony.com')
3435
->text('Bar!')
3536
->html('<p>Foo</p>')
36-
->attach(file_get_contents(__FILE__), 'foobar.php')
37+
->addPart(new DataPart(file_get_contents(__FILE__), 'foobar.php'))
3738
);
3839

3940
return new Response();

src/Symfony/Component/Mailer/Bridge/Infobip/Tests/Transport/InfobipApiTransportTest.php

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
use Symfony\Component\Mailer\SentMessage;
2020
use Symfony\Component\Mime\Address;
2121
use Symfony\Component\Mime\Email;
22+
use Symfony\Component\Mime\Part\DataPart;
2223
use Symfony\Contracts\HttpClient\ResponseInterface;
2324

2425
class InfobipApiTransportTest extends TestCase
@@ -206,8 +207,8 @@ public function testSendEmailWithAttachmentsShouldCalledInfobipWithTheRightParam
206207
{
207208
$email = $this->basicValidEmail()
208209
->text('foobar')
209-
->attach('some attachment', 'attachment.txt', 'text/plain')
210-
->embed('some inline attachment', 'inline.txt', 'text/plain')
210+
->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain'))
211+
->addPart((new DataPart('some inline attachment', 'inline.txt', 'text/plain'))->asInline())
211212
;
212213

213214
$this->transport->send($email);
@@ -320,8 +321,8 @@ public function testSendEmailWithAttachmentsWithSuccess()
320321
{
321322
$email = $this->basicValidEmail()
322323
->text('foobar')
323-
->attach('some attachment', 'attachment.txt', 'text/plain')
324-
->embed('some inline attachment', 'inline.txt', 'text/plain')
324+
->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain'))
325+
->addPart((new DataPart('some inline attachment', 'inline.txt', 'text/plain'))->asInline())
325326
;
326327

327328
$sentMessage = $this->transport->send($email);

src/Symfony/Component/Mailer/Bridge/Infobip/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,9 @@
2727
"require-dev": {
2828
"symfony/http-client": "^6.1"
2929
},
30+
"conflict": {
31+
"symfony/mime": "<6.2"
32+
},
3033
"autoload": {
3134
"psr-4": {
3235
"Symfony\\Component\\Mailer\\Bridge\\Infobip\\": ""

src/Symfony/Component/Mailer/Bridge/Sendgrid/Tests/Transport/SendgridApiTransportTest.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
use Symfony\Component\Mailer\Header\TagHeader;
1919
use Symfony\Component\Mime\Address;
2020
use Symfony\Component\Mime\Email;
21+
use Symfony\Component\Mime\Part\DataPart;
2122
use Symfony\Contracts\HttpClient\HttpClientInterface;
2223
use Symfony\Contracts\HttpClient\ResponseInterface;
2324

@@ -107,7 +108,7 @@ public function testLineBreaksInEncodedAttachment()
107108
$email->from('foo@example.com')
108109
->to('bar@example.com')
109110
// even if content doesn't include new lines, the base64 encoding performed later may add them
110-
->attach('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod', 'lorem.txt');
111+
->addPart(new DataPart('Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod', 'lorem.txt'));
111112

112113
$response = $this->createMock(ResponseInterface::class);
113114

src/Symfony/Component/Mailer/Bridge/Sendgrid/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"require-dev": {
2323
"symfony/http-client": "^5.4|^6.0"
2424
},
25+
"conflict": {
26+
"symfony/mime": "<6.2"
27+
},
2528
"autoload": {
2629
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Sendgrid\\": "" },
2730
"exclude-from-classmap": [

src/Symfony/Component/Mailer/Bridge/Sendinblue/Tests/Transport/SendinblueApiTransportTest.php

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -130,7 +130,6 @@ public function testSend()
130130
$transport = new SendinblueApiTransport('ACCESS_KEY', $client);
131131
$transport->setPort(8984);
132132

133-
$dataPart = new DataPart('body');
134133
$mail = new Email();
135134
$mail->subject('Hello!')
136135
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
@@ -140,7 +139,7 @@ public function testSend()
140139
->addCc('foo@bar.fr')
141140
->addBcc('foo@bar.fr')
142141
->addReplyTo('foo@bar.fr')
143-
->attachPart($dataPart)
142+
->addPart(new DataPart('body'))
144143
;
145144

146145
$message = $transport->send($mail);

src/Symfony/Component/Mailer/Bridge/Sendinblue/composer.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@
2222
"require-dev": {
2323
"symfony/http-client": "^5.4|^6.0"
2424
},
25+
"conflict": {
26+
"symfony/mime": "<6.2"
27+
},
2528
"autoload": {
2629
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Sendinblue\\": "" },
2730
"exclude-from-classmap": [

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy