Author Topic: [Mod] check if mail-address is valid (doesn't work) (now may be working :-))  (Read 24791 times)

0 Members and 1 Guest are viewing this topic.

Offline effemmess

  • Sr. Member
  • ****
  • Posts: 304
    • View Profile
UPDATE 10.07.2013
10 years later some additions - see below

UPDATE 17.11.2003
Now it works with all providers...
:) :) :)
==============================================

Hi,

I edited the function check_email() in functions.php to check if a email-address is valid or not... With this extended version of this function many invalid mail-entries will be rejected in registration-form :) (but not all, because yahoo,... :( ).

I donīt know, if this mod works if you configured your 4images to use smtp. I think, ther must be some edits for this... Try it out and tell me your results (and your solution! ;) ).

to do:
replace in .../4images/includes/functions.php:
Code: [Select]
function check_email($email) {
...
}



with:

Code: [Select]
function check_email($email) {
  $state=0;
  $helo_server=gethostbyaddr(gethostbyname($_SERVER["HTTP_HOST"]));
  $from=$_SERVER["SERVER_ADMIN"];
  if (preg_match('/^[-!#$%&\'*+\\.\/0-9=?A-Z^_`{|}~]+@([-0-9A-Z]+\.)+([0-9A-Z]){2,4}$/i', $email) && !mailcheck($email, 2)) {
    list($mailbox,$domain) = explode('@',$email,2);
    // find preferred mailserver
    if(getmxrr($domain,$mailhosts,$pref)){
      array_multisort($mailhosts,$pref);
    }
    $mailhosts[]=$domain;
    foreach($mailhosts as $hosts){
      if($connection) {
        fputs($connection, "QUIT\r\n");
        fclose ($connection);
      }
      $mailserver =  $hosts;
      // Open an SMTP connection
      $connection = @fsockopen ($mailserver, 25, $errno, $errstr, 1);
      if (!$connection) continue;
      socket_set_timeout($connection, 3);
      $res=get_lines($connection);
      if(checkres($res) != "220") continue;

      // Introduce ourselves
      $sendstring="EHLO ".$helo_server."\r\n";
      fputs($connection,$sendstring);
      $res=get_lines($connection);
      if(checkres($res) != "250"){
        $sendstring="HELO ".$helo_server."\r\n";
        fputs($connection,$sendstring);
        $res=get_lines($connection);
      }
      if(checkres($res) != "250") continue;
      
      // Envelope from
      $sendstring="MAIL FROM: <".$from.">\r\n";
      fputs($connection,$sendstring);
      $res=get_lines($connection);
      if(checkres($res) != "250") continue;

      // Envelope to
      $sendstring="RCPT TO: <".trim($email).">\r\n";
      fputs($connection,$sendstring);
      $res=get_lines($connection);
      if(checkres($res) != "250") continue;

/*
    // Verify address
    $sendstring="VRFY <".$mailbox.">\r\n";
    print_res($sendstring,"Q");
    fputs($connection,$sendstring);
    $res=get_lines($connection);
    print_res($res,"A");
    if(checkres($res) != "250") continue;

    // The message
    $sendstring="DATA\r\n";
    print_res($sendstring,"Q");
    fputs($connection,$sendstring);
    $res=get_lines($connection);
    print_res($res,"A");
    if(checkres($res) != "354") continue;

    // Send To:, From:, Subject:, other headers, blank line, message, and finish
    // with a period on its own line.
    $sendstring="To: ".trim($_POST['p_email'])."\r\nFrom: $from\r\nSubject: $subject\r\n$headers\r\n\r\n$message\r\n.\r\n";
    print_res($sendstring,"Q");
    fputs($connection,$sendstring);
    $res=get_lines($connection);
    print_res($res,"A");
    if(checkres($res) != "250") continue;
*/
  
      // Say bye bye
      $sendstring="QUIT\r\n";
      fputs($connection,$sendstring);
      $res=get_lines($connection);
      if(checkres($res) != "250" && checkres($res) != "221") continue;
      fclose ($connection);
      // It worked! So break out of the loop which tries all the mail exchangers.
      $state=1;
      break;
    }      
  }
  return $state;
}

function mailcheck($Addr, $Level, $Timeout = 15000) {
    
//  Valid Top-Level Domains
    $gTLDs = "com:net:org:edu:gov:mil:int:arpa:aero:biz:coop:info:museum:name:";
    $CCs   = "ad:ae:af:ag:ai:al:am:an:ao:aq:ar:as:at:au:aw:az:ba:bb:bd:be:bf:".
             "bg:bh:bi:bj:bm:bn:bo:br:bs:bt:bv:bw:by:bz:ca:cc:cf:cd:cg:ch:ci:".
             "ck:cl:cm:cn:co:cr:cs:cu:cv:cx:cy:cz:de:dj:dk:dm:do:dz:ec:ee:eg:".
             "eh:er:es:et:fi:fj:fk:fm:fo:fr:fx:ga:gb:gd:ge:gf:gh:gi:gl:gm:gn:".
             "gp:gq:gr:gs:gt:gu:gw:gy:hk:hm:hn:hr:ht:hu:id:ie:il:in:io:iq:ir:".
             "is:it:jm:jo:jp:ke:kg:kh:ki:km:kn:kp:kr:kw:ky:kz:la:lb:lc:li:lk:".
             "lr:ls:lt:lu:lv:ly:ma:mc:md:mg:mh:mk:ml:mm:mn:mo:mp:mq:mr:ms:mt:".
             "mu:mv:mw:mx:my:mz:na:nc:ne:nf:ng:ni:nl:no:np:nr:nt:nu:nz:om:pa:".
             "pe:pf:pg:ph:pk:pl:pm:pn:pr:pt:pw:py:qa:re:ro:ru:rw:sa:sb:sc:sd:".
             "se:sg:sh:si:sj:sk:sl:sm:sn:so:sr:st:su:sv:sy:sz:tc:td:tf:tg:th:".
             "tj:tk:tm:tn:to:tp:tr:tt:tv:tw:tz:ua:ug:uk:um:us:uy:uz:va:vc:ve:".
             "vg:vi:vn:vu:wf:ws:ye:yt:yu:za:zm:zr:zw:";
    
//  The countries can have their own 'TLDs', e.g. mydomain.com.au
    $cTLDs = "com:net:org:edu:gov:mil:co:ne:or:ed:go:mi:aero:biz:coop:info:museum:name:";

    $fail = 0;
    
//  Shift the address to lowercase to simplify checking and trim
    $Addr = trim(strtolower($Addr));
    
//  Check for spaces
    if (ereg(" ", $Addr)) $fail = 1;

//  Split the Address into user and domain parts
    $UD = explode("@", $Addr);
    if (sizeof($UD) != 2 || !$UD[0]) $fail = 1;

//  Split the domain part into its Levels
    $Levels = explode(".", $UD[1]); $sLevels = sizeof($Levels);
    if (!$Levels[0] || !$Levels[1]) $fail = 1;

//  Get the TLD, strip off trailing ] } ) > and check the length
    $tld = $Levels[$sLevels-1];
    $tld = ereg_replace("[>)}]$|]$", "", $tld);
    if (strlen($tld) < 2
    || (strlen($tld) > 3 && !ereg(":$tld:", ":arpa:aero:coop:info:museum:name:"))) $fail = 1;

    $Level--;

//  If the string after the last dot isn't in the generic TLDs or country codes, it's invalid.
    if ($Level && !$fail) {
    $Level--;
    if (!ereg($tld.":", $gTLDs) && !ereg($tld.":", $CCs)) $fail = 2;
    }

//  If it's a country code, check for a country TLD; add on the domain name.
    if ($Level && !$fail) {
    $cd = $sLevels - 2; $domain = $Levels[$cd].".".$tld;
    if (ereg($Levels[$cd].":", $cTLDs)) { $cd--; $domain = $Levels[$cd].".".$domain; }
    }
    echo $fail;
    return $fail;
} //Mailcheck

 


Have fun!

[EDITED by V@no]
this mod is missing get_lines() function, because of that it doesn't work.

[EDITED by effemmess]
Here are some functions I used in some scripts, also the missing get_lines() function. I donīt know if it now works, at that time it worked for me. But now I havenīt a 4images-system, so I canīt test it.  :oops:

Code: [Select]
function get_lines($fp){
  $res = "";
  while($data = fgets($fp,515)) {
    $res .= $data;
    if(substr($data,3,1) == " ") { break; }
  }
return $res;
}

function checkres($res){
return substr($res,0,3);
}

function print_res($res,$typus){
  if ($typus=="A") {
    printf ("<b>Antwort:</b><br>");
  } elseif ($typus=="Q") {
    printf ("<b>Frage:</b><br>");
  }
  printf ($res."<br />");
return;
}


Have fun!
« Last Edit: July 10, 2013, 12:53:44 AM by effemmess »

Offline JensF

  • Addicted member
  • ******
  • Posts: 1.028
    • View Profile
    • http://www.terraristik-galerie.de
Re: [Mod] check if mail-address is valid
« Reply #1 on: June 29, 2005, 12:03:08 AM »
Hi,

I have insert this mod and i have a question to this.

When i give a mail adress called test@hansi.de they say me that is a wrong mail.

But it give a Domain called www.hansi.de and the admin can have the mail test@hansi.de. But the Mod say its a wrong mail....

And i when i test some other Mails (valid or not) i become a white page with a 0 on the left top corner...
Mit freundlichem Gruß
Jens Funk



-> Sorry for my bad English <-

Offline manyaq

  • Newbie
  • *
  • Posts: 11
    • View Profile
Re: [Mod] check if mail-address is valid
« Reply #2 on: April 19, 2007, 03:58:10 PM »
Hi,

I have insert this mod and i have a question to this.

When i give a mail adress called test@hansi.de they say me that is a wrong mail.

But it give a Domain called www.hansi.de and the admin can have the mail test@hansi.de. But the Mod say its a wrong mail....

And i when i test some other Mails (valid or not) i become a white page with a 0 on the left top corner...
Code: [Select]
function mailcheck($Addr, $Level, $Timeout = 15000) {
   
//  Valid Top-Level Domains
    $gTLDs = "com:net:org:edu:gov:mil:int:arpa:aero:biz:coop:info:museum:name:";
    $CCs   = "ad:ae:af:ag:ai:al:am:an:ao:aq:ar:as:at:au:aw:az:ba:bb:bd:be:bf:".
             "bg:bh:bi:bj:bm:bn:bo:br:bs:bt:bv:bw:by:bz:ca:cc:cf:cd:cg:ch:ci:".
             "ck:cl:cm:cn:co:cr:cs:cu:cv:cx:cy:cz:de:dj:dk:dm:do:dz:ec:ee:eg:".
             "eh:er:es:et:fi:fj:fk:fm:fo:fr:fx:ga:gb:gd:ge:gf:gh:gi:gl:gm:gn:".
             "gp:gq:gr:gs:gt:gu:gw:gy:hk:hm:hn:hr:ht:hu:id:ie:il:in:io:iq:ir:".
             "is:it:jm:jo:jp:ke:kg:kh:ki:km:kn:kp:kr:kw:ky:kz:la:lb:lc:li:lk:".
             "lr:ls:lt:lu:lv:ly:ma:mc:md:mg:mh:mk:ml:mm:mn:mo:mp:mq:mr:ms:mt:".
             "mu:mv:mw:mx:my:mz:na:nc:ne:nf:ng:ni:nl:no:np:nr:nt:nu:nz:om:pa:".
             "pe:pf:pg:ph:pk:pl:pm:pn:pr:pt:pw:py:qa:re:ro:ru:rw:sa:sb:sc:sd:".
             "se:sg:sh:si:sj:sk:sl:sm:sn:so:sr:st:su:sv:sy:sz:tc:td:tf:tg:th:".
             "tj:tk:tm:tn:to:tp:tr:tt:tv:tw:tz:ua:ug:uk:um:us:uy:uz:va:vc:ve:".
             "vg:vi:vn:vu:wf:ws:ye:yt:yu:za:zm:zr:zw:";
   
//  The countries can have their own 'TLDs', e.g. mydomain.com.au
    $cTLDs = "com:net:org:edu:gov:mil:co:ne:or:ed:go:mi:aero:biz:coop:info:museum:name:";

    $fail = 0;
   
//  Shift the address to lowercase to simplify checking and trim
    $Addr = trim(strtolower($Addr));
   
//  Check for spaces
    if (ereg(" ", $Addr)) $fail = 1;

//  Split the Address into user and domain parts
    $UD = explode("@", $Addr);
    if (sizeof($UD) != 2 || !$UD[0]) $fail = 1;

//  Split the domain part into its Levels
    $Levels = explode(".", $UD[1]); $sLevels = sizeof($Levels);
    if (!$Levels[0] || !$Levels[1]) $fail = 1;

//  Get the TLD, strip off trailing ] } ) > and check the length
    $tld = $Levels[$sLevels-1];
    $tld = ereg_replace("[>)}]$|]$", "", $tld);
    if (strlen($tld) < 2
    || (strlen($tld) > 3 && !ereg(":$tld:", ":arpa:aero:coop:info:museum:name:"))) $fail = 1;

    $Level--;

//  If the string after the last dot isn't in the generic TLDs or country codes, it's invalid.
    if ($Level && !$fail) {
    $Level--;
    if (!ereg($tld.":", $gTLDs) && !ereg($tld.":", $CCs)) $fail = 2;
    }

//  If it's a country code, check for a country TLD; add on the domain name.
    if ($Level && !$fail) {
    $cd = $sLevels - 2; $domain = $Levels[$cd].".".$tld;
    if (ereg($Levels[$cd].":", $cTLDs)) { $cd--; $domain = $Levels[$cd].".".$domain; }
    }
    echo $fail;
    return $fail;
} //Mailcheck

 
change the code with below
Code: [Select]
function mailcheck($Addr, $Level, $Timeout = 15000) {
   
//  Valid Top-Level Domains
    $gTLDs = "com:net:org:edu:gov:mil:int:arpa:aero:biz:coop:info:museum:name:de:";
    $CCs   = "ad:ae:af:ag:ai:al:am:an:ao:aq:ar:as:at:au:aw:az:ba:bb:bd:be:bf:".
             "bg:bh:bi:bj:bm:bn:bo:br:bs:bt:bv:bw:by:bz:ca:cc:cf:cd:cg:ch:ci:".
             "ck:cl:cm:cn:co:cr:cs:cu:cv:cx:cy:cz:de:dj:dk:dm:do:dz:ec:ee:eg:".
             "eh:er:es:et:fi:fj:fk:fm:fo:fr:fx:ga:gb:gd:ge:gf:gh:gi:gl:gm:gn:".
             "gp:gq:gr:gs:gt:gu:gw:gy:hk:hm:hn:hr:ht:hu:id:ie:il:in:io:iq:ir:".
             "is:it:jm:jo:jp:ke:kg:kh:ki:km:kn:kp:kr:kw:ky:kz:la:lb:lc:li:lk:".
             "lr:ls:lt:lu:lv:ly:ma:mc:md:mg:mh:mk:ml:mm:mn:mo:mp:mq:mr:ms:mt:".
             "mu:mv:mw:mx:my:mz:na:nc:ne:nf:ng:ni:nl:no:np:nr:nt:nu:nz:om:pa:".
             "pe:pf:pg:ph:pk:pl:pm:pn:pr:pt:pw:py:qa:re:ro:ru:rw:sa:sb:sc:sd:".
             "se:sg:sh:si:sj:sk:sl:sm:sn:so:sr:st:su:sv:sy:sz:tc:td:tf:tg:th:".
             "tj:tk:tm:tn:to:tp:tr:tt:tv:tw:tz:ua:ug:uk:um:us:uy:uz:va:vc:ve:".
             "vg:vi:vn:vu:wf:ws:ye:yt:yu:za:zm:zr:zw:";
   
//  The countries can have their own 'TLDs', e.g. mydomain.com.au
    $cTLDs = "com:net:org:edu:gov:mil:co:ne:or:ed:go:mi:aero:biz:coop:info:museum:name:";

    $fail = 0;
   
//  Shift the address to lowercase to simplify checking and trim
    $Addr = trim(strtolower($Addr));
   
//  Check for spaces
    if (ereg(" ", $Addr)) $fail = 1;

//  Split the Address into user and domain parts
    $UD = explode("@", $Addr);
    if (sizeof($UD) != 2 || !$UD[0]) $fail = 1;

//  Split the domain part into its Levels
    $Levels = explode(".", $UD[1]); $sLevels = sizeof($Levels);
    if (!$Levels[0] || !$Levels[1]) $fail = 1;

//  Get the TLD, strip off trailing ] } ) > and check the length
    $tld = $Levels[$sLevels-1];
    $tld = ereg_replace("[>)}]$|]$", "", $tld);
    if (strlen($tld) < 2
    || (strlen($tld) > 3 && !ereg(":$tld:", ":arpa:aero:coop:info:museum:name:"))) $fail = 1;

    $Level--;

//  If the string after the last dot isn't in the generic TLDs or country codes, it's invalid.
    if ($Level && !$fail) {
    $Level--;
    if (!ereg($tld.":", $gTLDs) && !ereg($tld.":", $CCs)) $fail = 2;
    }

//  If it's a country code, check for a country TLD; add on the domain name.
    if ($Level && !$fail) {
    $cd = $sLevels - 2; $domain = $Levels[$cd].".".$tld;
    if (ereg($Levels[$cd].":", $cTLDs)) { $cd--; $domain = $Levels[$cd].".".$domain; }
    }
    echo $fail;
    return $fail;
} //Mailcheck

 

i think just the problem is $gTLDs
 $gTLDs = "com:net:org:edu:gov:mil:int:arpa:aero:biz:coop:info:museum:name:
u have to add just de: at the end
$gTLDs = "com:net:org:edu:gov:mil:int:arpa:aero:biz:coop:info:museum:name:de:

Offline Sebas Bonito

  • Sr. Member
  • ****
  • Posts: 271
  • Sebas Bonito
    • View Profile
Re: [Mod] check if mail-address is valid
« Reply #3 on: July 22, 2009, 04:11:03 PM »
After making the registration it will only show "0" on the page.
I've used a email adress like "...@web.de" and I still added the code from JensF.

Offline GaYan

  • Sr. Member
  • ****
  • Posts: 301
  • ♫ | G2 | ♫
    • View Profile
    • Ziramagic
Re: [Mod] check if mail-address is valid
« Reply #4 on: July 22, 2009, 04:15:14 PM »
Coool Bro.it works  :lol:
I'm Back :)

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: [Mod] check if mail-address is valid
« Reply #5 on: July 22, 2009, 04:56:29 PM »
Coool Bro.it works  :lol:
It does?
the mod uses get_lines() function, which doesn't exist...not sure how it's working for you...
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Sebas Bonito

  • Sr. Member
  • ****
  • Posts: 271
  • Sebas Bonito
    • View Profile
Re: [Mod] check if mail-address is valid
« Reply #6 on: July 22, 2009, 05:34:40 PM »
May this MOD needs SMTP?

Offline V@no

  • If you don't tell me what to do, I won't tell you where you should go :)
  • Global Moderator
  • 4images Guru
  • *****
  • Posts: 17.849
  • mmm PHP...
    • View Profile
    • 4images MODs Demo
Re: [Mod] check if mail-address is valid
« Reply #7 on: July 23, 2009, 02:25:06 AM »
no, that function is not PHP native...must be left out from the original code effemmess used as reference.

This mod does not work. Until it corrected moved out from mods section.
Your first three "must do" before you ask a question:
Please do not PM me asking for help unless you've been specifically asked to do so. Such PMs will be deleted without answer. (forum rule #6)
Extension for Firefox/Thunderbird: Master Password+    Back/Forward History Tweaks (restartless)    Cookies Manager+    Fit Images (restartless for Thunderbird)

Offline Sebas Bonito

  • Sr. Member
  • ****
  • Posts: 271
  • Sebas Bonito
    • View Profile
Re: [Mod] check if mail-address is valid
« Reply #8 on: July 23, 2009, 02:27:26 AM »
Now moved out from mods section.

Good, thx!