How do I generate long valid email address?

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)

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');

您可以使用此功能来生成符合最多64个字符本地部分和最多255个字符域部分的随机电子邮件地址。

     <?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());

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:

  • if the entire email address is longer than 320 symbols
  • 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?[^x5Cx22]x22?)
        ){255,}
      )
      ### check if the part before @ is no more than 64 symbols
      (?!
        (?:
          (?:x22?x5C[x00-x7E]x22?)
          |
          (?:x22?[^x5Cx22]x22?)
        ){65,}
        @
      )
      (?:
        (?:[x21x23-x27x2Ax2Bx2Dx2F-x39x3Dx3Fx5E-x7E]+)
        |
        (?:x22(?:[x01-x08x0Bx0Cx0E-x1Fx21x23-x5Bx5D-x7F]|(?:x5C[x00-x7F]))*x22)
      )
      (?:
        .
        (?:
          (?:[x21x23-x27x2Ax2Bx2Dx2F-x39x3Dx3Fx5E-x7E]+)
          |
          (?:
            x22
            (?:
              [x01-x08x0Bx0Cx0E-x1Fx21x23-x5Bx5D-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)
    
    链接地址: http://www.djcxy.com/p/92920.html

    上一篇: HTML5输入类型电子邮件如何在没有顶级域名的情况下运行

    下一篇: 我如何生成长的有效电子邮件地址?