使用fsockopen连接POP3邮箱
时间:2005-12-29 来源:manbuzhe0301
使用IMAP扩展,
有很多服务器会拒绝连接.
此方法感觉能更好的与邮箱服务器进行交互,
但编码量势必会较大.
示例代码,仅仅简单的显示某一邮件的相关信息.
//+OK 4 message(s) [442118 byte(s)]
//-ERR Unable to log on
$pop_server = "pop3.163.com";
$pop_user = "liuyiwei0301";
$pop_passwd = "********";
$msg_id = 2;
$mailserver=fsockopen($pop_server,110,&$errno,&$errstr,10);
if(!$mailserver){
print("Error: Unable to open socket to pop3 server.");
return false;
// Connection ok
}
// Welcome Message
$buffer=fgets($mailserver,512);
echo $buffer;
// USER
$buffer="USER $pop_user
";
fputs($mailserver,$buffer);
//fgets($mailserver,512);
// PASS
$buffer="PASS $pop_passwd
";
fputs($mailserver,$buffer);
// Authentification OK/ERR
//$buffer=fgets($mailserver,512);
$buffer="RETR $msg_id
";
fputs($mailserver,$buffer);
//$buffer=fgets($mailserver,512);
//echo $buffer;
for(;;) {
$buffer=fgets($mailserver,512);
if(strcasecmp(substr($buffer,0,5),"Date:")==0) {
$mail['date']=substr($buffer,6,strlen($buffer)-6);
}
if(strcasecmp(substr($buffer,0,5),"From:")==0) {
$mail['from'] = htmlspecialchars(substr($buffer,6,strlen($buffer)-6));
}
if(strcasecmp(substr($buffer,0,3),"To:")==0) {
$mail['to'] = htmlspecialchars(substr($buffer,4,strlen($buffer)-4));
}
if(strcasecmp(substr($buffer,0,8),"Subject:")==0) {
$mail['subject'] = imap_mime_header_decode(substr($buffer,9,strlen($buffer)-9));
}
if(strcmp($buffer,"
")==0 || strcmp($buffer,"
")==0 || strcmp($buffer,"
")==0) {
break;
}
}
$mail['text'] = '';
for(;;) {
$buffer=fgets($mailserver,128);
// GAAAAAAHHH! It should not break if regular mail line started with '.'
if(substr($buffer,0,1)==".") {
break;
}
//$mail['text'] .= htmlentities($buffer);
$mail['text'] .= $buffer;
}
echo "";
//print_r($mail['subject']);
echo "主题:".$mail['subject'][0]->text."";
echo "发件人:".$mail['from']."";
echo $mail['text'];
echo "";
fclose($mailserver);
有很多服务器会拒绝连接.
此方法感觉能更好的与邮箱服务器进行交互,
但编码量势必会较大.
示例代码,仅仅简单的显示某一邮件的相关信息.
//+OK 4 message(s) [442118 byte(s)]
//-ERR Unable to log on
$pop_server = "pop3.163.com";
$pop_user = "liuyiwei0301";
$pop_passwd = "********";
$msg_id = 2;
$mailserver=fsockopen($pop_server,110,&$errno,&$errstr,10);
if(!$mailserver){
print("Error: Unable to open socket to pop3 server.");
return false;
// Connection ok
}
// Welcome Message
$buffer=fgets($mailserver,512);
echo $buffer;
// USER
$buffer="USER $pop_user
";
fputs($mailserver,$buffer);
//fgets($mailserver,512);
// PASS
$buffer="PASS $pop_passwd
";
fputs($mailserver,$buffer);
// Authentification OK/ERR
//$buffer=fgets($mailserver,512);
$buffer="RETR $msg_id
";
fputs($mailserver,$buffer);
//$buffer=fgets($mailserver,512);
//echo $buffer;
for(;;) {
$buffer=fgets($mailserver,512);
if(strcasecmp(substr($buffer,0,5),"Date:")==0) {
$mail['date']=substr($buffer,6,strlen($buffer)-6);
}
if(strcasecmp(substr($buffer,0,5),"From:")==0) {
$mail['from'] = htmlspecialchars(substr($buffer,6,strlen($buffer)-6));
}
if(strcasecmp(substr($buffer,0,3),"To:")==0) {
$mail['to'] = htmlspecialchars(substr($buffer,4,strlen($buffer)-4));
}
if(strcasecmp(substr($buffer,0,8),"Subject:")==0) {
$mail['subject'] = imap_mime_header_decode(substr($buffer,9,strlen($buffer)-9));
}
if(strcmp($buffer,"
")==0 || strcmp($buffer,"
")==0 || strcmp($buffer,"
")==0) {
break;
}
}
$mail['text'] = '';
for(;;) {
$buffer=fgets($mailserver,128);
// GAAAAAAHHH! It should not break if regular mail line started with '.'
if(substr($buffer,0,1)==".") {
break;
}
//$mail['text'] .= htmlentities($buffer);
$mail['text'] .= $buffer;
}
echo "";
//print_r($mail['subject']);
echo "主题:".$mail['subject'][0]->text."";
echo "发件人:".$mail['from']."";
echo $mail['text'];
echo "";
fclose($mailserver);
相关阅读 更多 +