Skip to content

Commit 4e8a9ff

Browse files
committed
[Mime] Documented how to sign and encrypt email messages
1 parent 6340c5e commit 4e8a9ff

File tree

1 file changed

+94
-0
lines changed

1 file changed

+94
-0
lines changed

mailer.rst

Lines changed: 94 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -541,6 +541,97 @@ This makes use of the :ref:`css Twig namespace <mailer-css-namespace>` we create
541541
earlier. You could, for example, `download the foundation-emails.css file`_
542542
directly from GitHub and save it in ``assets/css``.
543543

544+
Signing and Encrypting Messages
545+
-------------------------------
546+
547+
.. versionadded:: 4.4
548+
549+
The option to sign and/or encrypt messages was introduced in Symfony 4.4.
550+
551+
It's possible to sign and/or encrypt email messages applying the `S/MIME`_
552+
standard to increase their integrity/security. Both options can be combined (to
553+
encrypt a signed message and to sign an encrypted message) and they require to
554+
have the `OpenSSL PHP extension`_ properly installed and configured.
555+
556+
Signing Messages
557+
~~~~~~~~~~~~~~~~
558+
559+
When signing a message, a cryptographic hash is generated for the entire content
560+
of the message (including attachments). This hash is added as an attachment so
561+
the recipient can validate the integrity of the received message. However, the
562+
contents of the original message are still readable for mailing agents not
563+
supporting signed messages, so you must also encrypt the message if you want to
564+
hide its contents::
565+
566+
use Symfony\Component\Mime\Crypto\SMimeSigner;
567+
use Symfony\Component\Mime\Email;
568+
569+
$email = (new Email())
570+
571+
// ...
572+
->html('...');
573+
574+
// if the certificate has a passphrase, pass it as the third argument
575+
// new SMimeSigner('/path/to/certificate.crt', '/path/to/certificate-private-key.key', 'the-passphrase');
576+
$signer = new SMimeSigner('/path/to/certificate.crt', '/path/to/certificate-private-key.key');
577+
// now use the Mailer component to send this $signedEmail instead of the original email
578+
$signedEmail = $signer->sign($email);
579+
580+
The certificate and private key must be `PEM encoded`_, and can be either
581+
created using for example OpenSSL or obtained at an official Certificate
582+
Authority (CA). The email recipient must have the CA certificate in the list of
583+
trusted issuers in order to verify the signature.
584+
585+
.. tip::
586+
587+
When using OpenSSL to generate certificates, make sure to add the
588+
``-addtrust emailProtection`` command option.
589+
590+
.. tip::
591+
592+
The ``SMimeSigner`` class defines other optional arguments to pass
593+
intermediate certificates and to configure the signing process using a
594+
bitwise operator options for :phpfunction:`openssl_pkcs7_sign` PHP function.
595+
596+
Encrypting Messages
597+
~~~~~~~~~~~~~~~~~~~
598+
599+
When encrypting a message, the entire message (including attachments) is
600+
encrypted using a certificate. Therefore, only the recipients that have the
601+
corresponding private key can read the original message contents::
602+
603+
use Symfony\Component\Mime\Crypto\SMimeEncrypter;
604+
use Symfony\Component\Mime\Email;
605+
606+
$email = (new Email())
607+
608+
// ...
609+
->html('...');
610+
611+
$encrypter = new SMimeEncrypter('/path/to/certificate.crt');
612+
// now use the Mailer component to send this $encryptedEmail instead of the original email
613+
$encryptedEmail = $encrypter->encrypt($email);
614+
615+
You can pass more than one certificate to the ``SMimeEncrypter()`` constructor
616+
and it will select the appropriate certificate depending on the ``To`` option::
617+
618+
$firstEmail = (new Email())
619+
// ...
620+
621+
622+
$secondEmail = (new Email())
623+
// ...
624+
625+
626+
$encrypter = new SMimeEncrypter([
627+
// key = email recipient; value = path to the certificate file
628+
'[email protected]' => '/path/to/first-certificate.crt',
629+
'[email protected]' => '/path/to/second-certificate.crt',
630+
]);
631+
632+
$firstEncryptedEmail = $encrypter->encrypt($firstEmail);
633+
$secondEncryptedEmail = $encrypter->encrypt($secondEmail);
634+
544635
Sending Messages Async
545636
----------------------
546637

@@ -647,3 +738,6 @@ environment:
647738
.. _`league/html-to-markdown`: https://github.com/thephpleague/html-to-markdown
648739
.. _`Markdown syntax`: https://commonmark.org/
649740
.. _`Inky`: https://foundation.zurb.com/emails.html
741+
.. _`S/MIME`: https://en.wikipedia.org/wiki/S/MIME
742+
.. _`OpenSSL PHP extension`: https://php.net/manual/en/book.openssl.php
743+
.. _`PEM encoded`: https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail

0 commit comments

Comments
 (0)