@@ -569,15 +569,20 @@ directly from GitHub and save it in ``assets/css``.
569569Signing and Encrypting Messages
570570-------------------------------
571571
572- It's possible to sign and/or encrypt email messages applying the ` S/MIME `_
573- standard to increase their integrity/security. Both options can be combined to
574- encrypt a signed message and/or to sign an encrypted message.
572+ It's possible to sign and/or encrypt email messages to increase their
573+ integrity/security. Both options can be combined to encrypt a signed message
574+ and/or to sign an encrypted message.
575575
576576Before signing/encrypting messages, make sure to have:
577577
578578* The `OpenSSL PHP extension `_ properly installed and configured;
579579* A valid `S/MIME `_ security certificate.
580580
581+ .. tip ::
582+
583+ When using OpenSSL to generate certificates, make sure to add the
584+ ``-addtrust emailProtection `` command option.
585+
581586Signing Messages
582587~~~~~~~~~~~~~~~~
583588
@@ -586,7 +591,19 @@ of the message (including attachments). This hash is added as an attachment so
586591the recipient can validate the integrity of the received message. However, the
587592contents of the original message are still readable for mailing agents not
588593supporting signed messages, so you must also encrypt the message if you want to
589- hide its contents::
594+ hide its contents.
595+
596+ You can sign messages using either ``S/MIME `` or ``DKIM ``. In both cases, the
597+ certificate and private key must be `PEM encoded `_, and can be either created
598+ using for example OpenSSL or obtained at an official Certificate Authority (CA).
599+ The email recipient must have the CA certificate in the list of trusted issuers
600+ in order to verify the signature.
601+
602+ S/MIME Signer
603+ .............
604+
605+ `S/MIME `_ is a standard for public key encryption and signing of MIME data. It
606+ requires using both a certificate and a private key:
590607
591608 use Symfony\C omponent\M ime\C rypto\S MimeSigner;
592609 use Symfony\C omponent\M ime\E mail;
@@ -603,22 +620,51 @@ hide its contents::
603620 $signedEmail = $signer->sign($email);
604621 // now use the Mailer component to send this $signedEmail instead of the original email
605622
606- The certificate and private key must be `PEM encoded `_, and can be either
607- created using for example OpenSSL or obtained at an official Certificate
608- Authority (CA). The email recipient must have the CA certificate in the list of
609- trusted issuers in order to verify the signature.
610-
611- .. tip ::
612-
613- When using OpenSSL to generate certificates, make sure to add the
614- ``-addtrust emailProtection `` command option.
615-
616623.. tip ::
617624
618625 The ``SMimeSigner `` class defines other optional arguments to pass
619626 intermediate certificates and to configure the signing process using a
620627 bitwise operator options for :phpfunction: `openssl_pkcs7_sign ` PHP function.
621628
629+ DKIM Signer
630+ ...........
631+
632+ `DKIM `_ is an email authentication method that affixes a digital signature,
633+ linked to a domain name, to each outgoing email messages. It requires a private
634+ key but not a certificate::
635+
636+ use Symfony\Component\Mime\Crypto\DkimSigner;
637+ use Symfony\Component\Mime\Email;
638+
639+ $email = (new Email())
640+ ->from('hello@example.com')
641+ // ...
642+ ->html('...');
643+
644+ // first argument: string with the contents or the absolute path of the private key
645+ // second and third arguments: the domain name and "selector" used to perform a DNS lookup
646+ // (the selector is a string used to point to a specific DKIM public key record in your DNS)
647+ $signer = new DkimSigner('/path/to/private-key.key', 'example.com', 'sf');
648+ // if the private key has a passphrase, pass it as the fourth argument
649+ // new DkimSigner('/path/to/private-key.key', 'example.com', 'sf', [], 'the-passphrase');
650+
651+ $signedEmail = $signer->sign($email);
652+ // now use the Mailer component to send this $signedEmail instead of the original email
653+
654+ // DKIM signer provides many config options and a helper object to configure them
655+ use Symfony\Component\Mime\Crypto\DkimOptions;
656+
657+ $signedEmail = $signer->sign($email, (new DkimOptions())
658+ ->bodyCanon('relaxed')
659+ ->headerCanon('relaxed')
660+ ->headersToIgnore(['Message-ID'])
661+ ->toArray()
662+ );
663+
664+ .. versionadded :: 5.2
665+
666+ The DKIM signer was introduced in Symfony 5.2.
667+
622668Encrypting Messages
623669~~~~~~~~~~~~~~~~~~~
624670
@@ -824,5 +870,6 @@ a specific address, instead of the *real* address:
824870.. _`Markdown syntax` : https://commonmark.org/
825871.. _`Inky` : https://get.foundation/emails/docs/inky.html
826872.. _`S/MIME` : https://en.wikipedia.org/wiki/S/MIME
873+ .. _`DKIM` : `https://en.wikipedia.org/wiki/DomainKeys_Identified_Mail
827874.. _ `OpenSSL PHP extension`: https://www.php.net/manual/en/book.openssl.php
828875.. _`PEM encoded` : https://en.wikipedia.org/wiki/Privacy-Enhanced_Mail
0 commit comments