root/cemail/include/validate_email.php

Revision 51, 3.4 kB (checked in by teiko, 4 years ago)

набросок cemail

Line 
1 <?php
2 /*
3     File : verifyemail_lib.php
4     Version : 1.1
5     Date :  November 20th, 2002
6     Author : Lars B. Jensen, lars.jensen@ljweb.com
7
8     Module Description
9     Module to verify if email is valid in three different levels.
10     Level 1 : Correct formatting of email
11     Level 2 : Server exists in DNS as MX record
12     Level 3 : Does the user exist on the email given
13     
14     !!! Warning !!!
15     Level 3 checking does take a considerable amount of time !!!
16
17
18     Note
19     Functions used in script is not implemented in windows versions of PHP !
20     
21     SMTP Reference
22     http://www.ietf.org/rfc/rfc0821.txt?number=821
23     
24
25     Public Functions
26     --------------------------------------------------------------
27     function verifyemail_validateemail($email)
28     function verifyemail_validatehost($email, $return_mxhost=0)
29     function verifyemail_validateexists($email)
30
31     Private Functions
32     --------------------------------------------------------------
33     function verifyemail_closesocket($socket)
34     function verifyemail_localhost()
35 */
36
37     function verifyemail_validateemail($email) {
38         if (!preg_match("/^([\w|\.|\-|_]+)@([\w||\-|_]+)\.([\w|\.|\-|_]+)$/i", $email)) {
39             return false;
40             exit;
41         }
42     
43         return true;
44     }
45
46
47     function verifyemail_validatehost($email, $return_mxhost=0) {
48         if (!verifyemail_validateemail($email)) {
49             return false;
50             exit;
51         }
52     
53         list($local,$domain) = explode("@",$email,2);
54         
55         $mxhosts = array();
56         if(!checkdnsrr($domain, "MX") || !getmxrr($domain, $mxhosts)) {
57             return false;
58             exit;
59         }
60
61         if ($return_mxhost) {
62             return $mxhosts;
63             exit;
64         }
65         
66         return true;
67     }
68
69
70     function verifyemail_validateexists($email) {
71         $mxhosts = verifyemail_validatehost($email, true);
72         
73         if (!is_array($mxhosts)) {
74             return false;
75             exit;
76         }
77
78         $found = false;
79         $localhost = verifyemail_localhost();
80
81         $mxsize = sizeof($mxhosts);
82         for($i=0; $i<$mxsize; $i++)    {
83             $socket = fsockopen($mxhosts[$i], 25);
84
85             if(!$socket) continue;
86
87             $foo = fgets($socket, 4096);
88
89             # 220 <domain> Service ready
90             if(!preg_match("/^220/i", $foo)) {
91                 verifyemail_closesocket($socket);
92                 continue;
93             }
94
95             fputs($socket, "HELO ".$localhost."\r\n");
96             $foo = fgets($socket);
97             while (preg_match("/^220/i", $foo)) {
98                 $foo = fgets($socket, 4096);
99             }
100
101             fputs($socket, "VRFY ".$email."\r\n");
102             $foo = fgets($socket, 4096);
103
104             # 250 Requested mail action okay, completed
105             if(preg_match("/^250/i", $foo)) {
106                 $found = true;
107                 verifyemail_closesocket($socket);
108
109                 break;
110             }
111
112             # 550 Requested action not taken: mailbox unavailable [E.g., mailbox not found, no access]
113             if(preg_match("/^550/i", $foo)) {
114                 verifyemail_closesocket($socket);
115                 continue;
116             }
117
118             fputs($socket, "MAIL FROM: <".$email.">\r\n");
119             $foo = fgets($socket, 4096);
120
121             fputs($socket, "RCPT TO: <".$email.">\r\n");
122             $foo = fgets($socket, 4096);
123
124             # 250 Requested mail action okay, completed
125             # 251 User not local; will forward to <forward-path>
126             if(preg_match("/^[250|251]/i", $foo)) {
127                 $found = true;
128                 verifyemail_closesocket($socket);
129
130                 break;
131             }
132         
133             verifyemail_closesocket($socket);
134         }
135
136         return $found;
137     }
138
139
140     function verifyemail_closesocket($socket) {
141         fputs($socket, "QUIT\r\n");
142         fclose($socket);
143         
144         return true;
145     }
146
147
148     function verifyemail_localhost() {
149         $localhost = getenv("SERVER_NAME");
150         if (!strlen($localhost)) $localhost = getenv("HOST");
151
152         return $localhost;
153     }
154 ?>
Note: See TracBrowser for help on using the browser.