Без опису

IdnAddressEncoder.php 1.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. <?php
  2. /*
  3. * This file is part of the Symfony package.
  4. *
  5. * (c) Fabien Potencier <fabien@symfony.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Symfony\Component\Mime\Encoder;
  11. /**
  12. * An IDN email address encoder.
  13. *
  14. * Encodes the domain part of an address using IDN. This is compatible will all
  15. * SMTP servers.
  16. *
  17. * Note: It leaves the local part as is. In case there are non-ASCII characters
  18. * in the local part then it depends on the SMTP Server if this is supported.
  19. *
  20. * @author Christian Schmidt
  21. */
  22. final class IdnAddressEncoder implements AddressEncoderInterface
  23. {
  24. /**
  25. * Encodes the domain part of an address using IDN.
  26. */
  27. public function encodeString(string $address): string
  28. {
  29. $i = strrpos($address, '@');
  30. if (false !== $i) {
  31. $local = substr($address, 0, $i);
  32. $domain = substr($address, $i + 1);
  33. if (preg_match('/[^\x00-\x7F]/', $domain)) {
  34. $address = sprintf('%s@%s', $local, idn_to_ascii($domain, \IDNA_DEFAULT | \IDNA_USE_STD3_RULES | \IDNA_CHECK_BIDI | \IDNA_CHECK_CONTEXTJ | \IDNA_NONTRANSITIONAL_TO_ASCII, \INTL_IDNA_VARIANT_UTS46));
  35. }
  36. }
  37. return $address;
  38. }
  39. }