PHPMailer::punyencodeAddress PHP Method

punyencodeAddress() public method

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 has characters not allowed in an IDN)
See also: PHPMailer::$CharSet
public punyencodeAddress ( string $address ) : string
$address string The email address to convert
return string The encoded address in ASCII form
    public function punyencodeAddress($address)
    {
        // Verify we have required functions, CharSet, and at-sign.
        if ($this->idnSupported() and !empty($this->CharSet) and ($pos = strrpos($address, '@')) !== false) {
            $domain = substr($address, ++$pos);
            // Verify CharSet string is a valid one, and domain properly encoded in this CharSet.
            if ($this->has8bitChars($domain) and @mb_check_encoding($domain, $this->CharSet)) {
                $domain = mb_convert_encoding($domain, 'UTF-8', $this->CharSet);
                if (($punycode = defined('INTL_IDNA_VARIANT_UTS46') ? idn_to_ascii($domain, 0, INTL_IDNA_VARIANT_UTS46) : idn_to_ascii($domain)) !== false) {
                    return substr($address, 0, $pos) . $punycode;
                }
            }
        }
        return $address;
    }

Usage Example

Exemplo n.º 1
0
 /**
  * Tests CharSet and Unicode -> ASCII conversions for addresses with IDN.
  */
 public function testConvertEncoding()
 {
     if (!$this->Mail->idnSupported()) {
         $this->markTestSkipped('intl and/or mbstring extensions are not available');
     }
     $this->Mail->clearAllRecipients();
     $this->Mail->clearReplyTos();
     // This file is UTF-8 encoded. Create a domain encoded in "iso-8859-1".
     $domain = '@' . mb_convert_encoding('françois.ch', 'ISO-8859-1', 'UTF-8');
     $this->Mail->addAddress('test' . $domain);
     $this->Mail->addCC('test+cc' . $domain);
     $this->Mail->addBCC('test+bcc' . $domain);
     $this->Mail->addReplyTo('test+replyto' . $domain);
     // Queued addresses are not returned by get*Addresses() before send() call.
     $this->assertEmpty($this->Mail->getToAddresses(), 'Bad "to" recipients');
     $this->assertEmpty($this->Mail->getCcAddresses(), 'Bad "cc" recipients');
     $this->assertEmpty($this->Mail->getBccAddresses(), 'Bad "bcc" recipients');
     $this->assertEmpty($this->Mail->getReplyToAddresses(), 'Bad "reply-to" recipients');
     // Clear queued BCC recipient.
     $this->Mail->clearBCCs();
     $this->buildBody();
     $this->assertTrue($this->Mail->send(), $this->Mail->ErrorInfo);
     // Addresses with IDN are returned by get*Addresses() after send() call.
     $domain = $this->Mail->punyencodeAddress($domain);
     $this->assertEquals([['test' . $domain, '']], $this->Mail->getToAddresses(), 'Bad "to" recipients');
     $this->assertEquals([['test+cc' . $domain, '']], $this->Mail->getCcAddresses(), 'Bad "cc" recipients');
     $this->assertEmpty($this->Mail->getBccAddresses(), 'Bad "bcc" recipients');
     $this->assertEquals(['test+replyto' . $domain => ['test+replyto' . $domain, '']], $this->Mail->getReplyToAddresses(), 'Bad "reply-to" addresses');
 }