IdnAddressEncoder.php 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  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, 0, \INTL_IDNA_VARIANT_UTS46));
  35. }
  36. }
  37. return $address;
  38. }
  39. }