Source of: mail_check.php (Download Source)
Last Modified: Fri, 16 Feb 2007 13:38:18 UTC

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
<?php
/***************************************************************
 * Copyright (C) 2003 Patrick Preuster <sica@megami.de>
 *
 * This is free software. You can redistribute and modify
 * it under the terms of the GNU General Public License.
 *
 * $Id: mail_check.php,v 1.5 2003-09-19 13:01:37 progman Exp $
 *
 ***************************************************************/

/* Usage: validate_mailaddr("email1", "email2", "email3"...);
 *
 * returns true if all mail addresses are valid, false if one
 * of them is not valid
 */

function validate_mailaddr() {
    for($i = 0; $i < func_num_args(); $i++) {
        $addr = trim(func_get_arg($i));
        $parts = explode('@', $addr);

        if((!is_array($parts)) or (count($parts) != 2)) {
            return false;
        }
        if(!preg_match('/^[0-9a-z_]([-_.]?[0-9a-z])*$/i', $parts[0])) {
            return false;
        }
        if(is_numeric($parts[1])) {
            $parts[1] = long2ip($parts[1]);
        }
        if(preg_match('/^(\d+)\.(\d+)\.(\d+)\.(\d+)$/', $parts[1], $matches)) {
            foreach($matches as $part) {
                if($part < 0 || $part > 255) {
                    return false;
                }
            }
        }
        elseif(preg_match('/^([0-9a-z][-.0-9a-z]*\\.[a-z]{2,4}[.]?)$/i', $parts[1], $matches)) {
            $host = $matches[1];
            if(!getmxrr($host, $tmp_mx_hosts) or !checkdnsrr($host,'ANY')) {
                return false;
            }
            // fix for verisign sitefinder
            if(gethostbyname($host) == '64.94.110.11') {
                return false;
            }
        }
        else {
            return false;
        }
    }
    return true;
}
?>