本文实例讲述了php通过smtp邮件验证登陆的方法。分享给大家供大家参考,具体如下:
内网的系统为了统一账号,都采用用邮件账号登陆的方式,所以有了以下程序
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
|
/**
* 通过邮件 验证登陆
* 这里要明白的是用户名是 带域名的:aaa@163.com
*/
function valideEmailLogin( $user , $pass , $smtp_server = 'smtp.163.com' , $port =25)
{
$handle = fsockopen ( $smtp_server , $port );
if (! $handle )
return false;
$mes = fgets ( $handle );
//echo $mes;
if (! $mes ){
fclose( $handle );
return false;
}
$status = explode ( " " , $mes );
if ( $status [0] != 220) { //链接服务器失败
fclose( $handle );
return false;
}
fwrite( $handle , 'HELO mystore' . "\r\n" ); //表明身份,这里的mystore是随便写的
$mes = fgets ( $handle );
//echo $mes;
if (! $mes ){
fclose( $handle );
return false;
}
$status = explode ( " " , $mes );
if ( $status [0] != 250) { //服务器HELO失败
fclose( $handle );
return false;
}
fwrite( $handle , 'AUTH LOGIN' . "\r\n" );
$mes = fgets ( $handle );
//echo $mes;
if (! $mes ){
fclose( $handle );
return false;
}
$status = explode ( " " , $mes );
if ( $status [0] != 334) { //请求验证登陆失败
fclose( $handle );
return false;
}
fwrite( $handle , base64_encode ( $user ). "\r\n" );
$mes = fgets ( $handle );
//echo $mes;
if (! $mes ){
fclose( $handle );
return false;
}
$status = explode ( " " , $mes );
if ( $status [0] != 334) { //验证用户名失败
fclose( $handle );
return false;
}
fputs ( $handle , base64_encode ( $pass ). "\r\n" );
$mes = fgets ( $handle );
//echo $mes;
if (! $mes ){
fclose( $handle );
return false;
}
$status = explode ( " " , $mes );
fclose( $handle );
if ( $status [0] != 235) { //验证密码失败
return false;
} else {
return true;
}
}
|
希望本文所述对大家PHP程序设计有所帮助。