[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;
/**
*用于收取邮箱中的信件,目前只支持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

..\n";
ganji_get_test_mail($item) . "\n";
echo "Get {$item['host']} maili finished
..\n";
echo "===============================================\n";
echo "===============================================\n";
}
?>
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



ganji_get_test_mail($item) . "\n";
echo "Get {$item['host']} maili finished


echo "===============================================\n";
echo "===============================================\n";
}
?>
是不是很简单,就把邮件写入到文本文件中了,不过由于是测试,所以没有把每个邮件单独存储,而且使用了json格式,然后分析的话就方便一些了,呵呵。
不过hotmail如何收取呢?这个问题目前我没有找到办法,哪位高手知道请不吝赐教,先谢谢啦。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 零经验选手,Compose 一天开发一款小游戏!
· 通过 API 将Deepseek响应流式内容输出到前端
· 因为Apifox不支持离线,我果断选择了Apipost!
2008-04-23 Team Foundation Server安装指南
2008-04-23 黑莓使用必看帖子大汇集(转载)