summaryrefslogtreecommitdiff
path: root/vendor/symfony/mime/Encoder/IdnAddressEncoder.php
diff options
context:
space:
mode:
Diffstat (limited to 'vendor/symfony/mime/Encoder/IdnAddressEncoder.php')
-rw-r--r--vendor/symfony/mime/Encoder/IdnAddressEncoder.php56
1 files changed, 56 insertions, 0 deletions
diff --git a/vendor/symfony/mime/Encoder/IdnAddressEncoder.php b/vendor/symfony/mime/Encoder/IdnAddressEncoder.php
new file mode 100644
index 0000000000..8936047ceb
--- /dev/null
+++ b/vendor/symfony/mime/Encoder/IdnAddressEncoder.php
@@ -0,0 +1,56 @@
+<?php
+
+/*
+ * This file is part of the Symfony package.
+ *
+ * (c) Fabien Potencier <fabien@symfony.com>
+ *
+ * For the full copyright and license information, please view the LICENSE
+ * file that was distributed with this source code.
+ */
+
+namespace Symfony\Component\Mime\Encoder;
+
+use Symfony\Component\Mime\Exception\AddressEncoderException;
+
+/**
+ * An IDN email address encoder.
+ *
+ * Encodes the domain part of an address using IDN. This is compatible will all
+ * SMTP servers.
+ *
+ * This encoder does not support email addresses with non-ASCII characters in
+ * local-part (the substring before @). To send to such addresses, use
+ * Utf8AddressEncoder together with SmtpUtf8Handler. Your outbound SMTP server must support
+ * the SMTPUTF8 extension.
+ *
+ * @author Christian Schmidt
+ *
+ * @experimental in 4.3
+ */
+final class IdnAddressEncoder implements AddressEncoderInterface
+{
+ /**
+ * Encodes the domain part of an address using IDN.
+ *
+ * @throws AddressEncoderException If local-part contains non-ASCII characters
+ */
+ public function encodeString(string $address): string
+ {
+ $i = strrpos($address, '@');
+ if (false !== $i) {
+ $local = substr($address, 0, $i);
+ $domain = substr($address, $i + 1);
+
+ if (preg_match('/[^\x00-\x7F]/', $local)) {
+ throw new AddressEncoderException(sprintf('Non-ASCII characters not supported in local-part os "%s".', $address));
+ }
+
+ if (preg_match('/[^\x00-\x7F]/', $domain)) {
+ $address = sprintf('%s@%s', $local, idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46));
+ }
+ }
+
+ return $address;
+ }
+}