[PHP] 使用POP3协议收取邮件

时间:2022-12-13 00:28:47

    下面是一个使用pop3 协议来收邮件的方法

 

<?
/* *
*用于收取邮箱中的信件,目前只支持pop3协议
* @filename function_getmail.php
* @touch date Tue 22 Apr 2009 20:49:12 AM CST
* @package Get_Ganji_test_mail
* @author zhangyufeng
* @version 1.0.0
* @copyright (c) 2009, zhangyufeng@staff.ganji.com
*/


// function ganji_get_test_mail($host, $port, $user, $password, $checkmail, $saveFile)

function  ganji_get_test_mail( $array_values )
{
        
        
$host         =   $array_values [ ' host ' ];
        
$port         =   $array_values [ ' port ' ];
        
$user         =   $array_values [ ' user ' ];
        
$password     =   $array_values [ ' password ' ];
        
$checkmail    =   $array_values [ ' checkmail ' ];
        
$saveFile     =   $array_values [ ' saveFile ' ];
        
$msg          =   '' ;
        
$return_msg   =   '' ;
        
// ini_set('memory_limit', '80M');
         if ( ! ( $sock   =   fsockopen ( gethostbyname ( $host ) , $port , $errno , $errstr )))
                
exit ( $errno . ' ' . $errstr );
        
set_socket_blocking ( $sock , true );

        
$command   =   " USER  " . $user . " \r\n " ;
        
fwrite ( $sock , $command );
$msg   =   fgets ( $sock );
echo   $msg ;
        
$command   =   " PASS  " . $password . " \r\n " ;
        
fwrite ( $sock , $command );
$msg   =   fgets ( $sock );
echo   $msg ;


        
$command   =   " stat\r\n " ;
        
fwrite ( $sock , $command );
        
$return_msg   =   fgets ( $sock );

$msg   =   fgets ( $sock );
echo   $msg ;

        
$command   =   " LIST\r\n " ;
        
fwrite ( $sock , $command );
        
$all_mails   =   array ();
        
while ( true )
        {
                
$msg   =   fgets ( $sock );       
                
if ( ! preg_match ( ' /^\+OK/ '   ,   $msg &&   ! preg_match ( ' /^\./ '   ,   $msg ))
                {
                        
$msg   =   preg_replace ( ' /\ .*\r\n/ '   ,   ''   ,   $msg );
                        
array_push ( $all_mails , $msg );
                }
                
if ( preg_match ( ' /^\./ ' , $msg ))
                        
break ;
        }

        
$ganji_mails   =   array ();
        
foreach ( $all_mails   as   $item )
        {
            
fwrite ( $sock ,   " TOP  $item  0\r\n " );
            
while ( true )
            {
                
$msg   =   fgets ( $sock );
                 
array_push ( $ganji_mails   ,   $item );
                
if ( preg_match ( ' /^\./ ' , $msg ))
                        
break ;
            }
        }
        
$mail_content         =   '' ;
        
$array_ganji_mails    =   array ();
        
foreach ( $ganji_mails   as   $item )
        {
            
fwrite ( $sock ,   " RETR  $item \r\n " );
            
while ( true )
            {
                
$msg             =   fgets ( $sock );
                
$mail_content   .=   $msg ;
                
if ( preg_match ( ' /^\./ ' ,   $msg ))
                {
                        
array_push ( $array_ganji_mails ,   iconv_mime_decode_headers ( $mail_content ,   0 ,   " ISO-8859-1 " ));
                        
$mail_content   =   '' ;
                        
break ;
                }
            }
        }
    
$command   =   " QUIT\r\n " ;
        
fwrite ( $sock , $command );
        
$msg   =   fgets ( $sock );
        
file_put_contents ( $saveFile ,  json_encode( $array_ganji_mails ));
        
// echo $msg;
         return   $return_msg ;

 

 

下面是使用方法,

 

<? php
include ( ' function_getmail.php ' );
$checkmail    =   ' javazyf@gmail.com ' ;
$array_all    =   array ();

$array_sohu   =   array (
        
" host "        =>   ' pop3.sohu.com ' ,
        
" port "        =>   110 ,  
        
" user "        =>   ' ganjizyf ' ,
        
" password "    =>   ' 111221111 ' ,
        
" saveFile "    =>   ' result/R_sohu.com ' ,
        
" checkmail "   =>   $checkmail     
);

$array_163   =   array (
        
" host "        =>   ' pop.163.com ' ,
        
" port "        =>   110 ,  
        
" user "        =>   ' franksin ' ,
        
" password "    =>   ' 1111111111111 ' ,
        
" saveFile "    =>   ' result/R_163.com ' ,
        
" checkmail "   =>   $checkmail     
);
$array_qq   =   array (
        
" host "        =>   ' pop.qq.com ' ,
        
" port "        =>   110 ,  
        
" user "        =>   ' zhang.y.f ' ,
        
" password "    =>   ' 111111111111 ' ,
        
" saveFile "    =>   ' result/R_qq.com ' ,
        
" checkmail "   =>   $checkmail     
);
$array_21cn   =   array (
        
" host "        =>   ' pop.21cn.com ' ,
        
" port "        =>   110 ,  
        
" user "        =>   ' ganjizyf ' ,
        
" password "    =>   ' 1111111111111 ' ,
        
" saveFile "    =>   ' result/R_21cn.com ' ,
        
" checkmail "   =>   $checkmail     
);
$array_tom   =   array (
        
" host "        =>   ' pop.tom.com ' ,
        
" port "        =>   110 ,  
        
" user "        =>   ' ganjizyf ' ,
        
" password "    =>   ' 11111111111111111 ' ,
        
" saveFile "    =>   ' result/R_tom.com ' ,
        
" checkmail "   =>   $checkmail     
);

$array_sina   =   array (
        
" host "        =>   ' pop.sina.com ' ,
        
" port "        =>   110 ,  
        
" user "        =>   ' ganjizyf ' ,
        
" password "    =>   ' test0122225 ' ,
        
" saveFile "    =>   ' result/R_sina.com ' ,
        
" checkmail "   =>   $checkmail
);

$array_gmail   =   array (
        
" host "        =>   ' ssl://pop.gmail.com ' ,
        
" port "        =>   995 ,
        
" user "        =>   ' ganjizyf@gmail.com ' ,
        
" password "    =>   ' test0152220 ' ,
        
" saveFile "    =>   ' result/R_gmail.com ' ,
        
" checkmail "   =>   $checkmail
);

// array_push($array_all, $array_sohu);
//array_push($array_all, $array_163);
//array_push($array_all, $array_qq);
//array_push($array_all, $array_21cn);
//array_push($array_all, $array_tom);

array_push ( $array_all ,   $array_sina );
array_push ( $array_all ,   $array_gmail );


foreach ( $array_all   as   $item )
{
echo   " ===============================================\n " ;
echo   " ===============================================\n " ;
echo   " ===============================================\n " ;
echo   " Start get { $item ['host']} mail[PHP] 使用POP3协议收取邮件[PHP] 使用POP3协议收取邮件[PHP] 使用POP3协议收取邮件..\n " ;


ganji_get_test_mail(
$item .   " \n " ;

echo   " Get { $item ['host']} maili finished[PHP] 使用POP3协议收取邮件[PHP] 使用POP3协议收取邮件..\n " ;
echo   " ===============================================\n " ;
echo   " ===============================================\n " ;

}
?>

 

是不是很简单,就把邮件写入到文本文件中了,不过由于是测试,所以没有把每个邮件单独存储,而且使用了json格式,然后分析的话就方便一些了,呵呵。

不过hotmail如何收取呢?这个问题目前我没有找到办法,哪位高手知道请不吝赐教,先谢谢啦。