如何生成长有效的电子邮件地址?

时间:2016-10-28 05:03:43

标签: php validation email email-address rfc822

我的意思是:

$ 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)

邮箱名称的长度是否有限制?我需要一封超过255个字符的电子邮件(用于测试)。

UPD 让我解释一下我的需要。我想在我的测试中确保用户无法指定长度超过255个字符的电子邮件地址。这就是为什么我需要一个很长的电子邮件地址。

UPD 遵循Justinas的建议:

$ 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)

3 个答案:

答案 0 :(得分:1)

Source

  

电子邮件地址的格式为local-part @ domain,其中本地部分最长可达64个字符最多可以 255个字符

要生成一些长字符串,您可以使用像str_repeat

这样简单的内容
var_dump(str_repeat('a', 64).'@'.str_repeat('g', 255).'.com');

答案 1 :(得分:1)

  

您可以使用此功能生成符合最多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());

答案 2 :(得分:1)

引用the link Justinas提供的更多内容:

  

电子邮件地址的格式为local-part @ domain,其中本地部分可能最多64个字符可能最多255个字符 - 但正向或反向路径的最大256个字符长度限制整个电子邮件地址 不超过254个字符长

现在,如果我们查看filter_var的{​​{3}},我们会看到基本上有两张支票:

  1. 如果整个电子邮件地址为source
  2. 如果匹配longer than 320 symbols(除非我们指定FILTER_FLAG_EMAIL_UNICODE标志)
  3. 正则表达式可以重写如下:

    /^
      ### 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
    

    知道我们能得到的最好结果是:

    $ 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)