3

What is the maximum length of characters allowed when using PHP's FILTER_VALIDATE_EMAIL?

I am testing my script to test my function of maximum length allowed(200), but when I use an email over 200 characters PHP's FILTER_VALIDATE_EMAIL returns false.

aksu
  • 5,221
  • 5
  • 24
  • 39
James Walker
  • 795
  • 2
  • 6
  • 22

2 Answers2

13

If the email is really valid then it should work fine with your 200+ character email data. Are you sure you are providing a valid email?

Conditions for a valid email:

The maximum length of an email address is 254 characters.

Every email address is composed of two parts. The local part comes before the '@' sign, and the domain part follows it. In "user@example.com", the local part is "user", and the domain part is "example.com".

The local part must not exceed 64 characters and

the domain part cannot be longer than 255 characters;

the total combined length of all characters (including '@' and punctuation) must not exceed 254 characters, however.

In sum, an email address can be 254 characters long at most. When you create an address, make sure your user name has less than 65 characters (provided the domain is 188 characters long at most).

gurudeb
  • 1,856
  • 22
  • 29
2

Looks like the 'domain can be up to 254 characters' part is not true. Here is my test data

<?php
$str = 'garahasgcdotrrriefvdqvegtartfhpnlwizanrhcqirnqllicyjhttvdylvkqccpljzrgledgqpjbttgwbqwgtkytkjxtufqcermdywzthceowyurrlmvvbiljraidwnmznazymwcjdozfaauzcnlwsxqfzgpzbsru1234567890ukxzqsvowzhnwnzzdzokhcffkpdvwycpcmxdknjvaakqjgjkvtqctpkhvgmeytsypkrfozeljsochfjvlszrwutiq';

$local = substr($str, 0, 64);
$domain = substr($str, 0, 63);
$com =  substr($str, 0, 63);

$email = $local. '@' . $domain . '.' . $com;
$is_valid = filter_var($email , FILTER_VALIDATE_EMAIL);

Above code returns TRUE. But if I try to increase $domain or $com, it fails. And of course I can't increase $local as per above comment.

So totally, it accepts only 192 characters.

Update:

Answering my own answer/question above:

Looks like the 254 character for domain rule needs further explanation.

Looks like PHP used to allow more than 63 character to a sub domain until PHP 5.3.2. When https://bugs.php.net/bug.php?id=49576 went in as part of PHP 5.3.3 release, it changed this behaviour.

So We can have the subdomain of max length of 63 characters. However the all total length of domain part can go up to 254.

e.g

local[64]@subdomain[63].subdomain[63].subdomain[63] is valid, but not local[64]@subdomain[65].subdomain[63]

Each dot in domain part is sub-domain which can have max of 63 characters.

Here is a sample code that shows the above theory(worked till 5.3.2 and stopped working from 5.3.3) - http://3v4l.org/tnUR9#v5214

vijaycs85
  • 197
  • 3
  • 15