Chinaunix首页 | 论坛 | 博客
  • 博客访问: 2105425
  • 博文数量: 194
  • 博客积分: 6450
  • 博客等级: 准将
  • 技术积分: 2085
  • 用 户 组: 普通用户
  • 注册时间: 2005-06-06 13:39
文章分类

全部博文(194)

文章存档

2013年(38)

2012年(11)

2011年(1)

2010年(1)

2009年(4)

2008年(13)

2007年(18)

2006年(63)

2005年(45)

我的朋友

分类:

2005-12-29 15:04:56

使用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);
阅读(1571) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~