Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

[Mime] deprecate attach/embed methods in favor of Email::addPart() #47711

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

Merged
merged 1 commit into from
Oct 15, 2022
Merged
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
3 changes: 2 additions & 1 deletion 3 src/Symfony/Bridge/Twig/Mime/NotificationEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
use Symfony\Component\ErrorHandler\Exception\FlattenException;
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Part\AbstractPart;
use Symfony\Component\Mime\Part\DataPart;
use Twig\Extra\CssInliner\CssInlinerExtension;
use Twig\Extra\Inky\InkyExtension;
use Twig\Extra\Markdown\MarkdownExtension;
Expand Down Expand Up @@ -134,7 +135,7 @@ public function exception(\Throwable|FlattenException $exception): static
$exceptionAsString = $this->getExceptionAsString($exception);

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

if (!$this->getSubject()) {
Expand Down
16 changes: 6 additions & 10 deletions 16 src/Symfony/Bridge/Twig/Mime/WrappedTemplatedEmail.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
namespace Symfony\Bridge\Twig\Mime;

use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Part\BodyFile;
use Symfony\Component\Mime\Part\DataPart;
use Twig\Environment;

/**
Expand All @@ -38,23 +40,17 @@ public function toName(): string
public function image(string $image, string $contentType = null): string
fabpot marked this conversation as resolved.
Show resolved Hide resolved
{
$file = $this->twig->getLoader()->getSourceContext($image);
if ($path = $file->getPath()) {
$this->message->embedFromPath($path, $image, $contentType);
} else {
$this->message->embed($file->getCode(), $image, $contentType);
}
$body = $file->getPath() ? new BodyFile($file->getPath()) : $file->getCode();
$this->message->addPart((new DataPart($body, $image, $contentType))->asInline());

return 'cid:'.$image;
}

public function attach(string $file, string $name = null, string $contentType = null): void
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should be deprecated also I guess?

{
$file = $this->twig->getLoader()->getSourceContext($file);
if ($path = $file->getPath()) {
$this->message->attachFromPath($path, $name, $contentType);
} else {
$this->message->attach($file->getCode(), $name, $contentType);
}
$body = $file->getPath() ? new BodyFile($file->getPath()) : $file->getCode();
$this->message->addPart(new DataPart($body, $name, $contentType));
}

/**
Expand Down
3 changes: 2 additions & 1 deletion 3 src/Symfony/Bridge/Twig/Tests/Mime/TemplatedEmailTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

use PHPUnit\Framework\TestCase;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\PropertyInfo\Extractor\PhpDocExtractor;
use Symfony\Component\Serializer\Encoder\JsonEncoder;
use Symfony\Component\Serializer\Normalizer\ArrayDenormalizer;
Expand Down Expand Up @@ -58,7 +59,7 @@ public function testSymfonySerialize()
$e->textTemplate('email.txt.twig');
$e->htmlTemplate('email.html.twig');
$e->context(['foo' => 'bar']);
$e->attach('Some Text file', 'test.txt');
$e->addPart(new DataPart('Some Text file', 'test.txt'));
$expected = clone $e;

$expectedJson = <<<EOF
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
use Symfony\Component\Mailer\MailerInterface;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;

class EmailController
{
Expand All @@ -25,15 +26,15 @@ public function indexAction(MailerInterface $mailer)
->addCc('cc@symfony.com')
->text('Bar!')
->html('<p>Foo</p>')
->attach(file_get_contents(__FILE__), 'foobar.php')
->addPart(new DataPart(file_get_contents(__FILE__), 'foobar.php'))
);

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

return new Response();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
use Symfony\Component\Mailer\SentMessage;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Contracts\HttpClient\ResponseInterface;

class InfobipApiTransportTest extends TestCase
Expand Down Expand Up @@ -206,8 +207,8 @@ public function testSendEmailWithAttachmentsShouldCalledInfobipWithTheRightParam
{
$email = $this->basicValidEmail()
->text('foobar')
->attach('some attachment', 'attachment.txt', 'text/plain')
->embed('some inline attachment', 'inline.txt', 'text/plain')
->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain'))
->addPart((new DataPart('some inline attachment', 'inline.txt', 'text/plain'))->asInline())
;

$this->transport->send($email);
Expand Down Expand Up @@ -320,8 +321,8 @@ public function testSendEmailWithAttachmentsWithSuccess()
{
$email = $this->basicValidEmail()
->text('foobar')
->attach('some attachment', 'attachment.txt', 'text/plain')
->embed('some inline attachment', 'inline.txt', 'text/plain')
->addPart(new DataPart('some attachment', 'attachment.txt', 'text/plain'))
->addPart((new DataPart('some inline attachment', 'inline.txt', 'text/plain'))->asInline())
;

$sentMessage = $this->transport->send($email);
Expand Down
3 changes: 3 additions & 0 deletions 3 src/Symfony/Component/Mailer/Bridge/Infobip/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,9 @@
"require-dev": {
"symfony/http-client": "^6.1"
},
"conflict": {
"symfony/mime": "<6.2"
},
"autoload": {
"psr-4": {
"Symfony\\Component\\Mailer\\Bridge\\Infobip\\": ""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
use Symfony\Component\Mailer\Header\TagHeader;
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Contracts\HttpClient\HttpClientInterface;
use Symfony\Contracts\HttpClient\ResponseInterface;

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

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

Expand Down
3 changes: 3 additions & 0 deletions 3 src/Symfony/Component/Mailer/Bridge/Sendgrid/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"require-dev": {
"symfony/http-client": "^5.4|^6.0"
},
"conflict": {
"symfony/mime": "<6.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Sendgrid\\": "" },
"exclude-from-classmap": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,6 @@ public function testSend()
$transport = new SendinblueApiTransport('ACCESS_KEY', $client);
$transport->setPort(8984);

$dataPart = new DataPart('body');
$mail = new Email();
$mail->subject('Hello!')
->to(new Address('saif.gmati@symfony.com', 'Saif Eddin'))
Expand All @@ -140,7 +139,7 @@ public function testSend()
->addCc('foo@bar.fr')
->addBcc('foo@bar.fr')
->addReplyTo('foo@bar.fr')
->attachPart($dataPart)
->addPart(new DataPart('body'))
;

$message = $transport->send($mail);
Expand Down
3 changes: 3 additions & 0 deletions 3 src/Symfony/Component/Mailer/Bridge/Sendinblue/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
"require-dev": {
"symfony/http-client": "^5.4|^6.0"
},
"conflict": {
"symfony/mime": "<6.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Mailer\\Bridge\\Sendinblue\\": "" },
"exclude-from-classmap": [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
use Symfony\Component\Mime\Address;
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Exception\InvalidArgumentException;
use Symfony\Component\Mime\Part\BodyFile;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\RawMessage;

/**
Expand Down Expand Up @@ -103,7 +105,7 @@ public function testSendInvalidMessage()
$message = new Email();
$message->to('recipient@example.org');
$message->from('sender@example.org');
$message->attachFromPath('/does_not_exists');
$message->addPart(new DataPart(new BodyFile('/does_not_exists')));

try {
$transport->send($message);
Expand Down
3 changes: 2 additions & 1 deletion 3 src/Symfony/Component/Mailer/composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@
},
"conflict": {
"symfony/http-kernel": "<5.4",
"symfony/messenger": "<6.2"
"symfony/messenger": "<6.2",
"symfony/mime": "<6.2"
},
"autoload": {
"psr-4": { "Symfony\\Component\\Mailer\\": "" },
Expand Down
20 changes: 16 additions & 4 deletions 20 src/Symfony/Component/Mime/Email.php
Original file line number Diff line number Diff line change
Expand Up @@ -327,15 +327,15 @@ public function getHtmlCharset(): ?string
*/
public function attach($body, string $name = null, string $contentType = null): static
{
return $this->attachPart(new DataPart($body, $name, $contentType));
return $this->addPart(new DataPart($body, $name, $contentType));
}

/**
* @return $this
*/
public function attachFromPath(string $path, string $name = null, string $contentType = null): static
{
return $this->attachPart(new DataPart(new BodyFile($path), $name, $contentType));
return $this->addPart(new DataPart(new BodyFile($path), $name, $contentType));
}

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

/**
* @return $this
*/
public function embedFromPath(string $path, string $name = null, string $contentType = null): static
{
return $this->attachPart((new DataPart(new BodyFile($path), $name, $contentType))->asInline());
return $this->addPart((new DataPart(new BodyFile($path), $name, $contentType))->asInline());
}

/**
* @return $this
*
* @deprecated since Symfony 6.2, use addPart() instead
*/
public function attachPart(DataPart $part): static
{
@trigger_deprecation('symfony/mime', '6.2', 'The "%s()" method is deprecated, use "addPart()" instead.', __METHOD__);

return $this->addPart($part);
}

/**
* @return $this
*/
public function addPart(DataPart $part): static
{
$this->cachedBody = null;
$this->attachments[] = $part;
Expand Down
8 changes: 4 additions & 4 deletions 8 src/Symfony/Component/Mime/MessageConverter.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ public static function toEmail(Message $message): Email
throw new RuntimeException(sprintf('Unable to create an Email from an instance of "%s" as the body is too complex.', get_debug_type($message)));
}

return self::attachParts($email, \array_slice($parts, 1));
return self::addParts($email, \array_slice($parts, 1));
}

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

return self::attachParts($email, \array_slice($parts, 1));
return self::addParts($email, \array_slice($parts, 1));
}

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

$email->attachPart($part);
$email->addPart($part);
}

return $email;
Expand Down
5 changes: 3 additions & 2 deletions 5 src/Symfony/Component/Mime/Tests/Crypto/SMimeSignerTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
use Symfony\Component\Mime\Email;
use Symfony\Component\Mime\Header\Headers;
use Symfony\Component\Mime\Message;
use Symfony\Component\Mime\Part\DataPart;
use Symfony\Component\Mime\Part\TextPart;

/**
Expand Down Expand Up @@ -120,8 +121,8 @@ public function testSignedMessageWithAttachments()
);
$message->html('html content <img src="cid:test.gif">');
$message->text('text content');
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test', 'r'));
$message->attach(fopen(__DIR__.'/../Fixtures/mimetypes/test.gif', 'r'), 'test.gif');
$message->addPart(new DataPart(fopen(__DIR__.'/../Fixtures/mimetypes/test', 'r')));
$message->addPart(new DataPart(fopen(__DIR__.'/../Fixtures/mimetypes/test.gif', 'r'), 'test.gif'));

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

Expand Down
Loading
Morty Proxy This is a proxified and sanitized view of the page, visit original site.