PHPMailer\PHPMailer

PHPMailer::punyencodeAddress()publicWP 1.0

Converts IDN in given email address to its ASCII form, also known as punycode, if possible. Important: Address must be passed in same encoding as currently set in PHPMailer::$CharSet. This function silently returns unmodified address if:

  • No conversion is necessary (i.e. domain name is not an IDN, or is already in ASCII form)
  • Conversion to punycode is impossible (e.g. required PHP functions are not available) or fails for any reason (e.g. domain contains characters not allowed in an IDN).

Метод класса: PHPMailer{}

Хуков нет.

Возвращает

Строку. The encoded address in ASCII form

Использование

$PHPMailer = new PHPMailer();
$PHPMailer->punyencodeAddress( $address );
$address(строка) (обязательный)
The email address to convert

Заметки

  • Смотрите: PHPMailer::$CharSet

Код PHPMailer::punyencodeAddress() WP 6.5.2

public function punyencodeAddress($address)
{
    //Verify we have required functions, CharSet, and at-sign.
    $pos = strrpos($address, '@');
    if (
        !empty($this->CharSet) &&
        false !== $pos &&
        static::idnSupported()
    ) {
        $domain = substr($address, ++$pos);
        //Verify CharSet string is a valid one, and domain properly encoded in this CharSet.
        if ($this->has8bitChars($domain) && @mb_check_encoding($domain, $this->CharSet)) {
            //Convert the domain from whatever charset it's in to UTF-8
            $domain = mb_convert_encoding($domain, self::CHARSET_UTF8, $this->CharSet);
            //Ignore IDE complaints about this line - method signature changed in PHP 5.4
            $errorcode = 0;
            if (defined('INTL_IDNA_VARIANT_UTS46')) {
                //Use the current punycode standard (appeared in PHP 7.2)
                $punycode = idn_to_ascii(
                    $domain,
                    \IDNA_DEFAULT | \IDNA_USE_STD3_RULES | \IDNA_CHECK_BIDI |
                        \IDNA_CHECK_CONTEXTJ | \IDNA_NONTRANSITIONAL_TO_ASCII,
                    \INTL_IDNA_VARIANT_UTS46
                );
            } elseif (defined('INTL_IDNA_VARIANT_2003')) {
                //Fall back to this old, deprecated/removed encoding
                // phpcs:ignore PHPCompatibility.Constants.RemovedConstants.intl_idna_variant_2003Deprecated
                $punycode = idn_to_ascii($domain, $errorcode, \INTL_IDNA_VARIANT_2003);
            } else {
                //Fall back to a default we don't know about
                // phpcs:ignore PHPCompatibility.ParameterValues.NewIDNVariantDefault.NotSet
                $punycode = idn_to_ascii($domain, $errorcode);
            }
            if (false !== $punycode) {
                return substr($address, 0, $pos) . $punycode;
            }
        }
    }

    return $address;
}