@@ -536,6 +536,98 @@ This makes use of the :ref:`css Twig namespace <mailer-css-namespace>` we create
536536earlier. You could, for example, `download the foundation-emails.css file `_
537537directly from GitHub and save it in ``assets/css ``.
538538
539+ Signing and Encrypting Messages
540+ -------------------------------
541+
542+ .. versionadded :: 4.4
543+
544+ The option to sign and/or encrypt messages was introduced in Symfony 4.4.
545+
546+ It's possible to sign and/or encrypt email messages applying the `S/MIME `_
547+ standard to increase their integrity/security. Both options can be combined (to
548+ encrypt a signed message and to sign an encrypted message) and they require to
549+ have the `OpenSSL PHP extension `_ properly installed and configured.
550+
551+ Signing Messages
552+ ~~~~~~~~~~~~~~~~
553+
554+ When signing a message, a cryptographic hash is generated for the entire content
555+ of the message (including attachments). This hash is added as an attachment so
556+ the recipient can validate the integrity of the received message. However, the
557+ contents of the original message are still readable for mailing agents not
558+ supporting signed messages, so you must also encrypt the message if you want to
559+ hide its contents::
560+
561+ use Symfony\Component\Mime\Crypto\SMimeSigner;
562+ use Symfony\Component\Mime\Email;
563+
564+ $email = (new Email())
565+ ->from('hello@example.com')
566+ // ...
567+ ->html('...');
568+
569+ $signer = new SMimeSigner('/path/to/certificate.crt', '/path/to/certificate-private-key.key');
570+ // if the private key has a passphrase, pass it as the third argument
571+ // new SMimeSigner('/path/to/certificate.crt', '/path/to/certificate-private-key.key', 'the-passphrase');
572+
573+ $signedEmail = $signer->sign($email);
574+ // now use the Mailer component to send this $signedEmail instead of the original email
575+
576+ The certificate and private key must be `PEM encoded `_, and can be either
577+ created using for example OpenSSL or obtained at an official Certificate
578+ Authority (CA). The email recipient must have the CA certificate in the list of
579+ trusted issuers in order to verify the signature.
580+
581+ .. tip ::
582+
583+ When using OpenSSL to generate certificates, make sure to add the
584+ ``-addtrust emailProtection `` command option.
585+
586+ .. tip ::
587+
588+ The ``SMimeSigner `` class defines other optional arguments to pass
589+ intermediate certificates and to configure the signing process using a
590+ bitwise operator options for :phpfunction: `openssl_pkcs7_sign ` PHP function.
591+
592+ Encrypting Messages
593+ ~~~~~~~~~~~~~~~~~~~
594+
595+ When encrypting a message, the entire message (including attachments) is
596+ encrypted using a certificate. Therefore, only the recipients that have the
597+ corresponding private key can read the original message contents::
598+
599+ use Symfony\Component\Mime\Crypto\SMimeEncrypter;
600+ use Symfony\Component\Mime\Email;
601+
602+ $email = (new Email())
603+ ->from('hello@example.com')
604+ // ...
605+ ->html('...');
606+
607+ $encrypter = new SMimeEncrypter('/path/to/certificate.crt');
608+ $encryptedEmail = $encrypter->encrypt($email);
609+ // now use the Mailer component to send this $encryptedEmail instead of the original email
610+
611+ You can pass more than one certificate to the ``SMimeEncrypter() `` constructor
612+ and it will select the appropriate certificate depending on the ``To `` option::
613+
614+ $firstEmail = (new Email())
615+ // ...
616+ ->to('jane@example.com');
617+
618+ $secondEmail = (new Email())
619+ // ...
620+ ->to('john@example.com');
621+
622+ $encrypter = new SMimeEncrypter([
623+ // key = email recipient; value = path to the certificate file
624+ 'jane@example.com' => '/path/to/first-certificate.crt',
625+ 'john@example.com' => '/path/to/second-certificate.crt',
626+ ]);
627+
628+ $firstEncryptedEmail = $encrypter->encrypt($firstEmail);
629+ $secondEncryptedEmail = $encrypter->encrypt($secondEmail);
630+
539631Sending Messages Async
540632----------------------
541633
@@ -642,3 +734,6 @@ environment:
642734.. _`league/html-to-markdown` : https://github.com/thephpleague/html-to-markdown
643735.. _`Markdown syntax` : https://commonmark.org/
644736.. _`Inky` : https://foundation.zurb.com/emails.html
737+ .. _`S/MIME` : https://en.wikipedia.org/wiki/S/MIME
738+ .. _`OpenSSL PHP extension` : https://php.net/manual/en/book.openssl.php
739+ .. _`PEM encoded` : https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail
0 commit comments