Skip to content
Prev Previous commit
Next Next commit
Added a section about serializing messages
  • Loading branch information
javiereguiluz committed Apr 3, 2019
commit 69ff0e20a86b0aaae7906ab4b1eee8e44c5c44c0
30 changes: 30 additions & 0 deletions components/mime.rst
Original file line number Diff line number Diff line change
Expand Up @@ -254,4 +254,34 @@ email multiparts::

$email = new Message($headers, $messageParts);

Serializing Email Messages
--------------------------

Email messages created with either the ``Email`` or ``Message`` classes can be
serialized because they are simple data objects::

$email = (new Email())
->from('fabien@symfony.com')
// ...
;

$serializedEmail = serialize($email);

If you want to store serialized email messages to recreate them later (e.g. to
include them in a message sent with the :doc:`Messenger component
</components/messenger>`) use the ``toString()`` utility method and the
:class:`Symfony\\Component\\Mime\\RawMessage` class::

use Symfony\Component\Mime\RawMessage;

// create the email and serialize it for later reuse
$email = (new Email())
->from('fabien@symfony.com')
// ...
;
$serializedEmail = $email->toString();

// later, recreate the original message to actually send it
$message = new RawMessage($serializedEmail);

.. _`MIME`: https://en.wikipedia.org/wiki/MIME