Bonjours,
je voudrais savoir s'il existe une solution PHP (ou autre) pour tester la validité d'une adresse mail ?
merci à tous.
++
~ iclic @ gauch,iclic, iclic @ droate, iclic, iclic
et ya pas de bôg môsieu ! ~
|
|
|
|
Répondre à bacchuss
|
Le seul moyen que j'ai trouvé est de se connecter au serveur smtp du domail de l'email et de faire un debut d'envoie de mail, au momentde valider l'adresse on auras une reponse du serveur sur l'existance ou non de cette adresse.
|
Tu trouveras du code et des explications intéressantes
|
Est ce que ca marche pour vous ???
function checkEmail($email) {
// checks proper syntax
if(preg_match("/^( [a-zA-Z0-9] )+( [a-zA-Z0-9\._-] )*@( [a-zA-Z0-9_-] )+( [a-zA-Z0-9\._-] +)+$/" , $email)) {
// gets domain name
list($username,$domain)=split('@',$email);
// checks for if MX records in the DNS
if(!customCheckDnsrr($domain)) {
return false;
}
// attempts a socket connection to mail server
if(!fsockopen($domain,25,$errno,$errstr,30)) {
return false;
}
return true;
}
return false;
}
function customCheckDnsrr($host,$recType='') {
if(!empty($host)) {
if($recType=='') $recType="MX";
exec("nslookup -type=$recType $host",$output);
foreach($output as $line) {
if(preg_match("/^$host/", $line)) {
return true;
}
}
return false;
}
return false;
}
et pour l'appelFunction ControleContact($email,$nomorganisation,$service,$fonction){
$msg="";
if(empty($nomorganisation)):
$msg .= "Le champs <B>Nom de l'organisation</B> est obligatoire<BR>";
endif;
if(!is_string($nomorganisation))
$msg .= "Le champs <B>Nom de l'organisation</B> doit être une chaine de caractére<BR>";
if(empty($service))
$msg .= "Au moins une case <B>service</B> doit être cochée<BR>";
if(empty($fonction))
$msg .= "Une <B>fonction</B> doit être séléctionnée<BR>";
if(empty($email))
$msg .="Le champ <B>email</B> est obligatoire<BR>";
else
/*
** C'est là que commence réellement l'appel
*/
$email = trim($email);
if(!checkEmail($email)) {
echo 'Invalid email address!';
$msg .='Invalid email address!';
}
else {
echo 'Email address is valid';
}
/*
** C'est là que fini réellement l'appel
*/
return ($msg);
}
..:::P@ ... a tras, a PoOf ou a chou :::.. |
Voui, il y a des erreurs dans le code. Il y a des espaces inutiles dans l'expression régulière. Je me rappelle que j'ai du corriger quelques erreurs dans le code.
<?php
/**
* Check validity of e-mail
*
* @param string email
* @return boolean
*/
function checkemail($email)
{
define('TIMEOUT', '5');
define('SMTP_PORT', '25');
// see http://www.devshed.com/c/a/PHP/Email-Address-Verification-with-PHP/2/
// checks proper syntax
if (!preg_match("/^([a-zA-Z0-9])+([a-zA-Z0-9\._-])*@([a-zA-Z0-9_-])+([a-zA-Z0-9\._-]+)+$/" , $email))
{
//printf('%s: %d<br />', __FILE__, __LINE__);
return false;
}
// gets domain name
list($username,$domain)=split('@',$email);
// checks for if MX records in the DNS
$mxhosts = array();
if(!getmxrr($domain, $mxhosts)) {
// no mx records, ok to check domain
if (!@fsockopen($domain, SMTP_PORT, $errno, $errstr, TIMEOUT))
{
.....
Johan Gates gave you the windows. GNU gave us the whole house.(Alexandrin) |
En faisant des recherches sur fsockopen j'ai trouvé ce script qui aprés l'avoir testé me va trés bien ... si ca peut servir :D
function validate_email($email){
$mailparts=explode("@",$email);
$hostname = $mailparts[1];
// validate email address syntax
$exp = "^[a-z\'0-9]+([._-][a-z\'0-9]+)*@([a-z0-9]+([._-][a-z0-9]+))+$";
$b_valid_syntax=eregi($exp, $email);
// get mx addresses by getmxrr
$b_mx_avail=getmxrr( $hostname, $mx_records, $mx_weight );
$b_server_found=0;
if($b_valid_syntax && $b_mx_avail){
// copy mx records and weight into array $mxs
$mxs=array();
for($i=0;$i<count($mx_records);$i++){
$em1 = array_pop($mx_weight);
$em2 = array_pop($mx_records);
$mxs[$em1]=$em2;
}
// sort array mxs to get servers with highest prio
ksort ($mxs, SORT_NUMERIC );
reset ($mxs);
while (list ($mx_weight, $mx_host) = each ($mxs) ) {
if($b_server_found == 0){
//try connection on port 25
$fp = @fsockopen($mx_host,25, $errno, $errstr, 2);
if($fp){
$ms_resp="";
// say HELO to mailserver
$ms_resp.=send_command($fp, "HELO microsoft.com");
// initialize sending mail
$ms_resp.=send_command($fp, "MAIL FROM:<support@microsoft.com>");
// try receipent address, will return 250 when ok..
$rcpt_text=send_command($fp, "RCPT TO:<".$email.">");
$ms_resp.=$rcpt_text;
if(substr( $rcpt_text, 0, 3) == "250")
$b_server_found=1;
// quit mail server connection
$ms_resp.=send_command($fp, "QUIT");
fclose($fp);
}
}
}
}
return $b_server_found;
}
function send_command($fp, $out){
fwrite($fp, $out . "\r\n");
return get_data($fp);
}
function get_data($fp){
$s="";
stream_set_timeout($fp, 2);
for($i=0;$i<2;$i++)
$s.=fgets($fp, 1024);
return $s;
}
// support windows platforms
if (!function_exists ('getmxrr') ) {
function getmxrr($hostname, &$mxhosts, &$mxweight) {
if (!is_array ($mxhosts) ) {
$mxhosts = array ();
}
if (!empty ($hostname) ) {
$output = "";
@exec ("nslookup.exe -type=MX $hostname.", $output);
$imx=-1;
foreach ($output as $line) {
$imx++;
$parts = "";
if (preg_match ("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/", $line, $parts) ) {
$mxweight[$imx] = $parts[1];
$mxhosts[$imx] = $parts[2];
}
}
return ($imx!=-1);
}
return false;
}
}
et l'appelle toujours : Function ControleContact($email,$nomorganisation,$service,$fonction){
$msg="";
$email = trim($email);
if(empty($nomorganisation)):
$msg .= "Le champs <B>Nom de l'organisation</B> est obligatoire<BR>";
endif;
if(!is_string($nomorganisation))
$msg .= "Le champs <B>Nom de l'organisation</B> doit être une chaine de caractére<BR>";
if(empty($service))
$msg .= "Au moins une case <B>service</B> doit être cochée<BR>";
if(empty($fonction))
$msg .= "Une <B>fonction</B> doit être séléctionnée<BR>";
if(empty($email))
$msg .="Le champ <B>email</B> est obligatoire<BR>";
elseif(!ereg("^(.+)@(.+)\\.(.+)$",$email))
$msg .="Le champ <B>email</B> est obligatoire 22<BR>";
elseif(validate_email($email) == 0) {
echo 'Invalid email address!';
$msg .='Invalid email address!';
}
else {
echo 'Email address is valid';
}
return ($msg);
}
..:::P@ ... a tras, a PoOf ou a chou :::.. |
J'ai des souci avec la fonction getmxrr voici son code ...
// support windows platforms
if (!function_exists ('getmxrr') ) {
function getmxrr($hostname, &$mxhosts) {
if (!is_array ($mxhosts) ) {
$mxhosts = array ();
}
if (!empty ($hostname) ) {
echo "$hostname n'est pas vide !!<br>";
$output = "";
@exec ("nslookup.exe -type=MX $hostname.", $output);
$imx=-1;
foreach ($output as $line) {
$imx++;
$parts = "";
if (preg_match ("/^$hostname\tMX preference = ([0-9]+), mail exchanger = (.*)$/", $line, $parts) ) {
// $mxweight[$imx] = $parts[1];
$mxhosts[$imx] = $parts[2];
}
}
return ($imx!=-1);
}
return false;
}
}
il me renvoie quasi tout le temps false et va savoir pourquoi avec la même adresse si la trouve vraie !!! si quelqu'un pouvait m'aider ... merci :D ..:::P@ ... a tras, a PoOf ou a chou :::.. L'imagination est plus importante que la connaissance. Albert Einstein |
Bonjour,
|