8000 [Mime] deprecate attach/embed methods in favor of Email::addPart() · symfony/symfony@7cdbd20 · GitHub
[go: up one dir, main page]

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": [

src/Symfony/Component/Mailer/Tests/Transport/Smtp/SmtpTransportTest.php

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
use Symfony\Component\Mime\Address;
2222
use Symfony\Component\Mime\Email;
2323
use Symfony\Component\Mime\Exception\InvalidArgumentException;
24+
use Symfony\Component\Mime\Part\BodyFile;
25+
use Symfony\Component\Mime\Part\DataPart;
2426
use Symfony\Component\Mime\RawMessage;
2527

2628
/**
@@ -103,7 +105,7 @@ public function testSendInvalidMessage()
103105
$message = new Email();
104106
$message->to('recipient@example.org');
105107
$message->from('sender@example.org');
106-
$message->attachFromPath('/does_not_exists');
108+
$message->addPart(new DataPart(new BodyFile('/does_not_exists')));
107109

108110
try {
109111
$transport->send($message);

src/Symfony/Component/Mailer/composer.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
},
3232
"conflict": {
3333
"symfony/http-kernel": "<5.4",
34-
"symfony/messenger": "<6.2"
34+
"symfony/messenger": "<6.2",
35+
"symfony/mime": "<6.2"
3536
},
3637
"autoload": {
3738
"psr-4": { "Symfony\\Component\\Mailer\\": "" },

src/Symfony/Component/Mime/Email.php

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -327,15 +327,15 @@ public function getHtmlCharset(): ?string
327327
*/
328328
public function attach($body, string $name = null, string $contentType = null): static
329329
{
330-
return $this->attachPart(new DataPart($body, $name, $contentType));
330+
return $this->addPart(new DataPart($body, $name, $contentType));
331331
}
332332

333333
/**
334334
* @return $this
335335
*/
336336
public function attachFromPath(string $path, string $name = null, string $contentType = null): static
337337
{
338-
return $this->attachPart(new DataPart(new BodyFile($path), $name, $contentType));
338+
return $this->addPart(new DataPart(new BodyFile($path), $name, $contentType));
339339
}
340340

341341
/**
@@ -345,21 +345,33 @@ public function attachFromPath(string $path, string $name = null, string $conten
345345
*/
346346
public function embed($body, string $name = null, string $contentType = null): static
347347
{
348-
return $this->attachPart((new DataPart($body, $name, $contentType))->asInline());
348+
return $this->addPart((new DataPart($body, $name, $contentType))->asInline());
349349
}
350350

351351
/**
352352
* @return $this
353353
*/
354354
public function embedFromPath(string $path, string $name = null, string $contentType = null): static
355355
{
356-
return $this->attachPart((new DataPart(new BodyFile($path), $name, $contentType))->asInline());
356+
return $this->addPart((new DataPart(new BodyFile($path), $name, $contentType))->asInline());
357357
}
358358

359359
/**
360360
* @return $this
361+
*
362+
* @deprecated since Symfony 6.2, use addPart() instead
361363
*/
362364
public function attachPart(DataPart $part): static
365+
{
366+
@trigger_deprecation('symfony/mime', '6.2', 'The "%s()" method is deprecated, use "addPart()" instead.', __METHOD__);
367+
368+
return $this->addPart($part);
369+
}
370+
371+
/**
372+
* @return $this
373+
*/
374+
public function addPart(DataPart $part): static
363375
{
364376
$this->cachedBody = null;
365377
$this->attachments[] = $part;

src/Symfony/Component/Mime/MessageConverter.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public static function toEmail(Message $message): Email
5858
throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
5959
}
6060

61-
return self::attachParts($email, \array_slice($parts, 1));
61+
return self::addParts($email, \array_slice($parts, 1));
6262
}
6363

6464
throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
@@ -104,17 +104,17 @@ private static function createEmailFromRelatedPart(Message $message, RelatedPart
104104
throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
105105
}
106106

107-
return self::attachParts($email, \array_slice($parts, 1));
107+
return self::addParts($email, \array_slice($parts, 1));
108108
}
109109

110-
private static function attachParts(Email $email, array $parts): Email
110+
private static function addParts(Email $email, array $parts): Email
111111
{
112112
foreach ($parts as $part) {
113113
if (!$part instanceof DataPart) {
114114
throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($email)));
115115
}
116116

117-
$email->attachPart($part);
117+
$email->addPart($part);
118118
}
119119

120120
return $email;

src/Symfony/Component/Mime/Tests/Crypto/SMimeSignerTest.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
use Symfony\Component\Mime\Email;
1717
use Symfony\Component\Mime\Header\Headers;
1818
use Symfony\Component\Mime\Message;
19+
use Symfony\Component\Mime\Part\DataPart;
1920
use Symfony\Component\Mime\Part\TextPart;
2021

2122
/**
@@ -120,8 +121,8 @@ public function testSignedMessageWithAttachments()
120121
);
121122
$message->html('html content <img src="cid:test.gif">');
122123
$message->text('text content');
123-
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test', 'r'));
124-
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
124+
$message->addPart(new DataPart(fopen(__DIR__.'/../Fixtures/mimetypes/test', 'r')));
125+
$message->addPart(new DataPart(fopen(__DIR__.'/../Fixtures/mimetypes/test.gif', 'r'), 'test.gif'));
125126

126127
$signer = new SMimeSigner($this->samplesDir.'sign.crt', $this->samplesDir.'sign.key');
127128

0 commit comments

Comments
 (0)
0