Dashboard sipadu mbip
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

IdnAddressEncoder.php 1.6KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556
  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. use Symfony\Component\Mime\Exception\AddressEncoderException;
  12. /**
  13. * An IDN email address encoder.
  14. *
  15. * Encodes the domain part of an address using IDN. This is compatible will all
  16. * SMTP servers.
  17. *
  18. * This encoder does not support email addresses with non-ASCII characters in
  19. * local-part (the substring before @). To send to such addresses, use
  20. * Utf8AddressEncoder together with SmtpUtf8Handler. Your outbound SMTP server must support
  21. * the SMTPUTF8 extension.
  22. *
  23. * @author Christian Schmidt
  24. *
  25. * @experimental in 4.3
  26. */
  27. final class IdnAddressEncoder implements AddressEncoderInterface
  28. {
  29. /**
  30. * Encodes the domain part of an address using IDN.
  31. *
  32. * @throws AddressEncoderException If local-part contains non-ASCII characters
  33. */
  34. public function encodeString(string $address): string
  35. {
  36. $i = strrpos($address, '@');
  37. if (false !== $i) {
  38. $local = substr($address, 0, $i);
  39. $domain = substr($address, $i + 1);
  40. if (preg_match('/[^\x00-\x7F]/', $local)) {
  41. throw new AddressEncoderException(sprintf('Non-ASCII characters not supported in local-part os "%s".', $address));
  42. }
  43. if (preg_match('/[^\x00-\x7F]/', $domain)) {
  44. $address = sprintf('%s@%s', $local, idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46));
  45. }
  46. }
  47. return $address;
  48. }
  49. }