root/trunk/validateMailbox.php

Revision 10, 4.1 kB (checked in by bobe, 3 years ago)

ajout utilisation extension fileinfo si dispo + divers mots clés manquants

  • Property svn:keywords set to Author Date Id Revision
Line 
1 <?php
2 /**
3  * Copyright (c) 2002-2006 Aurélien Maille
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18  *
19  * @package Wamailer
20  * @author  Bobe <wascripts@phpcodeur.net>
21  * @link    http://phpcodeur.net/wascripts/wamailer/
22  * @license http://www.gnu.org/copyleft/lesser.html
23  * @version $Id$
24  */
25
26 /**
27  * Vérifie si une adresse email N’EST PAS valide (domaine et compte).
28  * Ceci est différent d’une vérification de validité.
29  * Le serveur SMTP peut très bien répondre par un 250 ok pour une adresse
30  * email non existante, les erreurs d’adressage étant traitées ultérieurement
31  * au niveau du serveur POP.
32  *
33  * Appels possibles à cette fonction :
34  *
35  * $result = validateMailBox('username@domain.tld');
36  * $result = validateMailBox('username@domain.tld', $results);
37  * $result = validateMailBox(array('username1@domain.tld',
38  *     'username2@domain.tld', 'username@otherdomain.tld'), $results);
39  *
40  * @param mixed $emailList      Adresse email complète ou tableau d’adresses email
41  * @param array $return_errors  Passage par référence. Retour d’erreur sous
42  *                              forme de tableau :
43  *                              array('address1@domain.tld' => 'msg error...',
44  *                                    'address2@domain.tld' => 'msg error...')
45  *
46  * @return boolean
47  */
48 function validateMailbox($emailList, &$return_errors = null)
49 {
50     if( !class_exists('Mailer_SMTP') ) {
51         require dirname(__FILE__) . '/smtp.class.php';
52     }
53     
54     if( !is_array($emailList) ) {
55         $emailList = array($emailList);
56     }
57     else {
58         $emailList = array_unique($emailList);
59     }
60     
61     $domainList = $return_errors = array();
62     
63     foreach( $emailList as $email ) {
64         if( strpos($email, '@') ) {
65             list($mailbox, $domain) = explode('@', $email);
66             
67             if( !isset($domainList[$domain]) ) {
68                 $domainList[$domain] = array();
69             }
70             
71             array_push($domainList[$domain], $mailbox);
72         }
73         else {
74             $return_errors[$email] = 'Invalid syntax';
75         }
76     }
77     
78     foreach( $domainList as $domain => $mailboxList ) {
79         $mxhosts = array();
80         if( function_exists('getmxrr') ) {
81             $result = getmxrr($domain, $hosts, $weight);
82             
83             for( $i = 0, $m = count($hosts); $i < $m; $i++ ) {
84                 array_push($mxhosts, array($weight[$i], $hosts[$i]));
85             }
86         }
87         else {
88             exec(sprintf('nslookup -type=mx %s', escapeshellcmd($domain)), $lines);
89             
90             $regexp = '/^' . preg_quote($domain) . '\s+(?:(?i)MX\s+)?'
91                 . '(preference\s*=\s*([0-9]+),\s*)?'
92                 . 'mail\s+exchanger\s*=\s*(?(1)|([0-9]+)\s+)([^ ]+?)\.?$/';
93             
94             foreach( $lines as $value ) {
95                 if( preg_match($regexp, $value, $match) ) {
96                     array_push($mxhosts, array(
97                         $match[3] === '' ? $match[2] : $match[3],
98                         $match[4]
99                     ));
100                 }
101             }
102             
103             $result = (count($mxhosts) > 0);
104         }
105         
106         if( !$result ) {
107             array_push($mxhosts, array(0, $domain));
108         }
109         
110         array_multisort($mxhosts);
111         
112         $smtp = new Mailer_SMTP();
113         
114         foreach( $mxhosts as $record ) {
115             try {
116                 $smtp->connect($record[1]);
117                 if( $smtp->from('wamailer@' . $domain) ) {
118                     foreach( $mailboxList as $mailbox ) {
119                         $email = $mailbox . '@' . $domain;
120                         
121                         if( !$smtp->to($email, true) ) {
122                             $return_errors[$email] = $smtp->responseData;
123                         }
124                     }
125                 }
126                 
127                 $smtp->quit();
128                 break;
129             }
130             
131             //
132             // Code temporaire à remplacer
133             //
134             catch( Exception $e ) {
135                 if( !$result ) {
136                     foreach( $mailboxList as $mailbox ) {
137                         $return_errors[$mailbox . '@' . $domain] = $e->getMessage();
138                     }
139                     break;
140                 }
141             }
142         }
143     }
144     
145     return (count($return_errors) == 0);
146 }
147
148 ?>
149
Note: See TracBrowser for help on using the browser.