2

What I mean:

$ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@gmail.com", FILTER_VALIDATE_EMAIL));'
Command line code:1:
string(74) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@gmail.com"
$ php -r 'var_dump(filter_var(str_repeat("a", 65) . "@gmail.com", FILTER_VALIDATE_EMAIL));'
Command line code:1:
bool(false)

Is there a restriction on length of a mailbox name? I need an email of more than 255 characters long (for testing).

UPD Let me explain what I need this for. I'd like to ensure in my tests that user can't specify email address longer than 255 characters. That's why I need a long email address.

UPD Following Justinas suggestion:

$ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@" . str_repeat("g", 63) . ".com", FILTER_VALIDATE_EMAIL));'
Command line code:1:
string(132) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg.com"
$ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@" . str_repeat("g", 64) . ".com", FILTER_VALIDATE_EMAIL));'
Command line code:1:
bool(false)
x-yuri
  • 16,722
  • 15
  • 114
  • 161
  • keeping your email address to not more than 127 characters is sane enough - that is.... – Poiz Oct 28 '16 at 05:38

3 Answers3

1

Source

The format of email addresses is local-part@domain where the local part may be up to 64 characters long and the domain may have a maximum of 255 characters


To generate some long string you can use something as simple as str_repeat

var_dump(str_repeat('a', 64).'@'.str_repeat('g', 255).'.com');
Justinas
  • 41,402
  • 5
  • 66
  • 96
  • It works with `str_repeat('g', 63)` at most, see my updated question. – x-yuri Oct 28 '16 at 06:47
  • @x-yuri Aren't you messing up with `'` in your code? – Justinas Oct 28 '16 at 07:51
  • @x-yuri Also answer to your question: Yes, there is limit to email address. And since you generate way too long address it leads to `false` – Justinas Oct 28 '16 at 07:58
  • Thanks, I compiled last "update" from different places, but forgot to fix the quotes. As for "email address limit", from the link you provided it must be 254 symbols. But `64 + 1 + 64 + 4 = 133` is far less than that. – x-yuri Oct 28 '16 at 09:56
  • Judging from [`php/php-src`](https://github.com/php/php-src/blob/376dcaa/ext/filter/logical_filters.c#L621), maximum email address length is 320 characters. Which means it doesn't match `regexp1`. – x-yuri Oct 28 '16 at 10:10
1

You may use this Function to generate random email addresses conforming to a maximum of 64 characters Local-Part and a maximum of 255 characters Domain Part.

     <?php

        function generateEmailAddress($maxLenLocal=64, $maxLenDomain=255){
            $numeric        =  '0123456789';
            $alphabetic     = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz';
            $extras         = '.-_';
            $all            = $numeric . $alphabetic . $extras;
            $alphaNumeric   = $alphabetic . $numeric;
            $alphaNumericP  = $alphabetic . $numeric . "-";
            $randomString   = '';

            // GENERATE 1ST 4 CHARACTERS OF THE LOCAL-PART
            for ($i = 0; $i < 4; $i++) {
                $randomString .= $alphabetic[rand(0, strlen($alphabetic) - 1)];
            }
            // GENERATE A NUMBER BETWEEN 20 & 60
            $rndNum         = rand(20, $maxLenLocal-4);

            for ($i = 0; $i < $rndNum; $i++) {
                $randomString .= $all[rand(0, strlen($all) - 1)];
            }

            // ADD AN @ SYMBOL...
            $randomString .= "@";

            // GENERATE DOMAIN NAME - INITIAL 3 CHARS:
            for ($i = 0; $i < 3; $i++) {
                $randomString .= $alphabetic[rand(0, strlen($alphabetic) - 1)];
            }

            // GENERATE A NUMBER BETWEEN 15 & $maxLenDomain-7
            $rndNum2        = rand(15, $maxLenDomain-7);
            for ($i = 0; $i < $rndNum2; $i++) {
                $randomString .= $all[rand(0, strlen($all) - 1)];
            }
            // ADD AN DOT . SYMBOL...
            $randomString .= ".";

            // GENERATE TLD: 4
            for ($i = 0; $i < 4; $i++) {
                $randomString .= $alphaNumeric[rand(0, strlen($alphaNumeric) - 1)];
            }

            return $randomString;

        }


        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
        var_dump(generateEmailAddress());
Poiz
  • 7,611
  • 2
  • 15
  • 17
1

To cite a little more from the link Justinas provided:

The format of email addresses is local-part@domain where the local part may be up to 64 characters long and the domain may have a maximum of 255 characters—but the maximum of 256-character length of a forward or reverse path restricts the entire email address to be no more than 254 characters long.

Now if we look at the filter_var's source, we'll see that there are basically two checks there:

  1. if the entire email address is longer than 320 symbols
  2. if it matches the regex (unless we specify FILTER_FLAG_EMAIL_UNICODE flag)

The regex may be rewritten as follows:

/^
  ### check if total length is no more than 254 symbols
  (?!
    (?:
      (?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)
      |
      (?:\\x22?[^\\x5C\\x22]\\x22?)
    ){255,}
  )
  ### check if the part before @ is no more than 64 symbols
  (?!
    (?:
      (?:\\x22?\\x5C[\\x00-\\x7E]\\x22?)
      |
      (?:\\x22?[^\\x5C\\x22]\\x22?)
    ){65,}
    @
  )
  (?:
    (?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)
    |
    (?:\\x22(?:[\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]|(?:\\x5C[\\x00-\\x7F]))*\\x22)
  )
  (?:
    \\.
    (?:
      (?:[\\x21\\x23-\\x27\\x2A\\x2B\\x2D\\x2F-\\x39\\x3D\\x3F\\x5E-\\x7E]+)
      |
      (?:
        \\x22
        (?:
          [\\x01-\\x08\\x0B\\x0C\\x0E-\\x1F\\x21\\x23-\\x5B\\x5D-\\x7F]
          |
          (?:\\x5C[\\x00-\\x7F])
        )*
        \\x22
      )
    )
  )*
  @
  (?:
    (?:
      ### no segment must be longer than 64 symbols
      (?!.*[^.]{64,})
      (?:
        (?:
          (?:xn--)?
          [a-z0-9]+
          (?:-+[a-z0-9]+)*
          \\.
        ){1,126}
      ){1,}
      (?:
        (?:[a-z][a-z0-9]*)
        |
        (?:(?:xn--)[a-z0-9]+)
      )
      (?:-+[a-z0-9]+)*
    )
    |
    ### ip
    (?:
      \\[
      (?:
        (?:
          IPv6:
          (?:
            (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){7})
            |
            (?:
              (?!(?:.*[a-f0-9][:\\]]){7,})
              (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?
              ::
              (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,5})?
            )
          )
        )
        |
        (?:
          (?:
            IPv6:
            (?:
              (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){5}:)
              |
              (?:
                (?!(?:.*[a-f0-9]:){5,})
                (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3})?
                ::
                (?:[a-f0-9]{1,4}(?::[a-f0-9]{1,4}){0,3}:)?
              )
            )
          )?
          (?:
            (?:25[0-5])
            |
            (?:2[0-4][0-9])
            |
            (?:1[0-9]{2})
            |
            (?:[1-9]?[0-9])
          )
          (?:
            \\.
            (?:
              (?:25[0-5])
              |
              (?:2[0-4][0-9])
              |
              (?:1[0-9]{2})
              |
              (?:[1-9]?[0-9])
            )
          ){3}
        )
      )
      \\]
    )
  )
$/iD

Knowing that the best we can get is:

$ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@" . str_repeat(str_repeat("g", 63) . ".", 2) . str_repeat("g", 61), FILTER_VALIDATE_EMAIL));'
Command line code:1:
string(254) "aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa@ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg.ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg.ggggggggggggggggggggggggggggggggggggggggggggggggggggggggggggg"
$ php -r 'var_dump(filter_var(str_repeat("a", 64) . "@" . str_repeat(str_repeat("g", 63) . ".", 2) . str_repeat("g", 62), FILTER_VALIDATE_EMAIL));'
Command line code:1:
bool(false)
x-yuri
  • 16,722
  • 15
  • 114
  • 161