Skip to content

Commit eca3116

Browse files
committed
minor #17372 Use addPart instead of embed* or attach*. (hiddewie)
This PR was squashed before being merged into the 6.2 branch. Discussion ---------- Use `addPart` instead of `embed*` or `attach*`. Fixes #17303 Ref #17353 Ref symfony/symfony#47711 Ref symfony/symfony#47462 This PR contains the documentation for simplifications for adding parts to `Email`s. Commits ------- ebbffc5 Use `addPart` instead of `embed*` or `attach*`.
2 parents 262baba + ebbffc5 commit eca3116

File tree

1 file changed

+26
-11
lines changed

1 file changed

+26
-11
lines changed

mailer.rst

+26-11
Original file line numberDiff line numberDiff line change
@@ -446,24 +446,31 @@ result of rendering some template) or PHP resources::
446446
File Attachments
447447
~~~~~~~~~~~~~~~~
448448

449-
Use the ``attachFromPath()`` method to attach files that exist on your file system::
449+
Use the ``addPart()`` method with a ``BodyFile`` to add files that exist on your file system::
450450

451451
$email = (new Email())
452452
// ...
453-
->attachFromPath('/path/to/documents/terms-of-use.pdf')
453+
->addPart(new DataPart(new BodyFile('/path/to/documents/terms-of-use.pdf')))
454454
// optionally you can tell email clients to display a custom name for the file
455-
->attachFromPath('/path/to/documents/privacy.pdf', 'Privacy Policy')
455+
->addPart(new DataPart(new BodyFile('/path/to/documents/privacy.pdf', 'Privacy Policy')))
456456
// optionally you can provide an explicit MIME type (otherwise it's guessed)
457-
->attachFromPath('/path/to/documents/contract.doc', 'Contract', 'application/msword')
457+
->addPart(new DataPart(new BodyFile('/path/to/documents/contract.doc', 'Contract', 'application/msword')))
458458
;
459459

460-
Alternatively you can use the ``attach()`` method to attach contents from a stream::
460+
Alternatively you can attach contents from a stream by passing it directly to the ``DataPart`` ::
461461

462462
$email = (new Email())
463463
// ...
464-
->attach(fopen('/path/to/documents/contract.doc', 'r'))
464+
->addPart(new DataPart(fopen('/path/to/documents/contract.doc', 'r')))
465465
;
466466

467+
.. deprecated:: 6.2
468+
469+
In Symfony versions previous to 6.2, the methods ``attachFromPath`` and ``attach``
470+
could be used to add attachments. These methods have been deprecated and replaced with
471+
``addPart``.
472+
473+
467474
Embedding Images
468475
~~~~~~~~~~~~~~~~
469476

@@ -472,25 +479,27 @@ instead of adding them as attachments. When using Twig to render the email
472479
contents, as explained :ref:`later in this article <mailer-twig-embedding-images>`,
473480
the images are embedded automatically. Otherwise, you need to embed them manually.
474481

475-
First, use the ``embed()`` or ``embedFromPath()`` method to add an image from a
482+
First, use the ``addPart()`` method to add an image from a
476483
file or stream::
477484

478485
$email = (new Email())
479486
// ...
480487
// get the image contents from a PHP resource
481-
->embed(fopen('/path/to/images/logo.png', 'r'), 'logo', 'image/png')
488+
->addPart((new DataPart(fopen('/path/to/images/logo.png', 'r'), 'logo', 'image/png'))->asInline())
482489
// get the image contents from an existing file
483-
->embedFromPath('/path/to/images/signature.gif', 'footer-signature', 'image/gif')
490+
->addPart((new DataPart(new BodyFile('/path/to/images/signature.gif', 'footer-signature', 'image/gif')))->asInline())
484491
;
485492

493+
Use the ``asInline()`` method to embed the content instead of attaching it.
494+
486495
The second optional argument of both methods is the image name ("Content-ID" in
487496
the MIME standard). Its value is an arbitrary string used later to reference the
488497
images inside the HTML contents::
489498

490499
$email = (new Email())
491500
// ...
492-
->embed(fopen('/path/to/images/logo.png', 'r'), 'logo', 'image/png')
493-
->embedFromPath('/path/to/images/signature.gif', 'footer-signature', 'image/gif')
501+
->addPart((new DataPart(fopen('/path/to/images/logo.png', 'r'), 'logo', 'image/png'))->asInline())
502+
->addPart((new DataPart(new BodyFile('/path/to/images/signature.gif', 'footer-signature', 'image/gif')))->asInline())
494503

495504
// reference images using the syntax 'cid:' + "image embed name"
496505
->html('<img src="cid:logo"> ... <img src="cid:footer-signature"> ...')
@@ -503,6 +512,12 @@ images inside the HTML contents::
503512

504513
The support of embedded images as HTML backgrounds was introduced in Symfony 6.1.
505514

515+
.. deprecated:: 6.2
516+
517+
In Symfony versions previous to 6.2, the methods ``embedFromPath`` and ``embed``
518+
could be used to embed images. These methods have been deprecated and replaced with
519+
``addPart`` together with inline ``DataPart``s.
520+
506521
.. _mailer-configure-email-globally:
507522
508523
Configuring Emails Globally

0 commit comments

Comments
 (0)