Files
InvoiceShelf/app/Rules/IdnEmail.php
Darko Gjorgjijoski 39c9179888 Support internationalized domain names (IDN) in email validation
Add IdnEmail validation rule that converts IDN domains to Punycode
via idn_to_ascii() before validating with FILTER_VALIDATE_EMAIL.
Applied to all email fields: customers, members, profiles, admin
users, customer portal profiles, and mail configuration.

Includes unit tests for standard emails, IDN emails, and invalid
inputs.

Fixes #388
2026-04-06 23:55:29 +02:00

47 lines
1.1 KiB
PHP

<?php
namespace App\Rules;
use Closure;
use Illuminate\Contracts\Validation\ValidationRule;
/**
* Validates email addresses including those with internationalized
* domain names (IDN) like user@münchen.de or michel@exempleé.fr.
*
* Converts the domain part to Punycode (ASCII) before validation.
*/
class IdnEmail implements ValidationRule
{
public function validate(string $attribute, mixed $value, Closure $fail): void
{
if (! is_string($value) || $value === '') {
return;
}
$parts = explode('@', $value);
if (count($parts) !== 2) {
$fail('The :attribute must be a valid email address.');
return;
}
[$local, $domain] = $parts;
if (function_exists('idn_to_ascii') && $domain !== '') {
$ascii = idn_to_ascii($domain, IDNA_DEFAULT, INTL_IDNA_VARIANT_UTS46);
if ($ascii !== false) {
$domain = $ascii;
}
}
$normalized = $local.'@'.$domain;
if (! filter_var($normalized, FILTER_VALIDATE_EMAIL)) {
$fail('The :attribute must be a valid email address.');
}
}
}