This repository has been archived by the owner on Nov 17, 2021. It is now read-only.
Basic IDN support #1044
Closed
Closed
Basic IDN support #1044
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
a713ba7
IDN-encode email domain
c960657 9193afd
Fix PHP 7.2 deprecation warning
c960657 93696b5
PHPDoc and type hints
c960657 dd1fa4e
Describe dependencies
c960657 aa1cf8b
Note about IDN domains
c960657 bbcc550
Remove redundant PHPdoc
c960657 ac50a90
Swap parameter order and add getAddress()
c960657 f63a393
Update changelog
c960657 File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SwiftMailer. | ||
* (c) 2018 Christian Schmidt | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* Email address encoder. | ||
* | ||
* @author Christian Schmidt | ||
*/ | ||
interface Swift_AddressEncoder | ||
{ | ||
/** | ||
* Encodes an email address. | ||
* | ||
* @throws Swift_AddressEncoderException If the email cannot be represented in | ||
* the encoding implemented by this class. | ||
*/ | ||
public function encodeString(string $address): string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SwiftMailer. | ||
* (c) 2018 Christian Schmidt | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* An IDN email address encoder. | ||
* | ||
* @author Christian Schmidt | ||
*/ | ||
class Swift_AddressEncoder_IdnAddressEncoder implements Swift_AddressEncoder | ||
{ | ||
/** | ||
* Encodes the domain part of an address using IDN. | ||
* | ||
* @throws Swift_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 Swift_AddressEncoderException('Non-ASCII characters not supported in local-part', $address); | ||
} | ||
|
||
$address = sprintf('%s@%s', $local, $this->idnToAscii($domain)); | ||
} | ||
|
||
return $address; | ||
} | ||
|
||
/** | ||
* IDN-encodes a UTF-8 string to ASCII. | ||
* | ||
* @param string $string | ||
* | ||
* @return string | ||
*/ | ||
protected function idnToAscii(string $string): string | ||
{ | ||
if (function_exists('idn_to_ascii')) { | ||
return idn_to_ascii($string, 0, INTL_IDNA_VARIANT_UTS46); | ||
} | ||
|
||
if (class_exists('TrueBV\Punycode')) { | ||
$punycode = new \TrueBV\Punycode(); | ||
return $punycode->encode($string); | ||
} | ||
|
||
throw new Swift_SwiftException('No IDN encoder found (install the intl extension or the true/punycode package'); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
<?php | ||
|
||
/* | ||
* This file is part of SwiftMailer. | ||
* (c) 2018 Christian Schmidt | ||
* | ||
* For the full copyright and license information, please view the LICENSE | ||
* file that was distributed with this source code. | ||
*/ | ||
|
||
/** | ||
* AddressEncoderException when the specified email address is in a format that | ||
* cannot be encoded by a given address encoder. | ||
* | ||
* @author Christian Schmidt | ||
*/ | ||
class Swift_AddressEncoderException extends Swift_RfcComplianceException | ||
{ | ||
/** The address that could not be encoded */ | ||
protected $address; | ||
|
||
public function __construct(string $message, string $address) | ||
{ | ||
parent::__construct($message); | ||
$this->address = $address; | ||
} | ||
|
||
/** | ||
* Returns the address that could not be encoded. | ||
*/ | ||
public function getAddress(): string | ||
{ | ||
return $this->address; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The arguments probably need to be the other way round (or in the constructor)