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

    下面是一个使用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']} maildot.gifdot.gifdot.gif..\n " ;


ganji_get_test_mail(
$item .   " \n " ;

echo   " Get { $item ['host']} maili finisheddot.gifdot.gif..\n " ;
echo   " ===============================================\n " ;
echo   " ===============================================\n " ;

}
?>

 

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

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

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值