动网论坛,站长建站首选,国内使用量最多的论坛软件 动网论坛官方技术讨论区 站长工具 申请属于您自己的免费论坛
首页 | 新闻资讯 | 网站运营 | 网络编程 | 数据库 | 服务器 | 网页设计 | 图像媒体 | 网络应用 | 搜索优化 | 资源下载 | 动网主机 | DVBOX
    本站内  互联网 ASP论坛  ASP.Net论坛  PHP论坛
  
   PHP → 阅读文章

 一个简单的IMAP类和应用

作者来源: 
阅读 数 200 人次 , 2006-3-29 4:11:00 


<?php
//by alpha.z
//05/21/2000

class myimap
{
var $username="";
var $userpwd="";
var $hostname="";
var $port=0;
var $connection=0; //是否连接
var $state="disconnected"; //连接状态
var $greeting="";
var $must_update=0;
var $instream=0;

function open()
{
if ($this->port==110)
$this->instream=imap_open("{$this->hostname/pop3:110}inbox",$this->username,$this->userpwd);
else
$this->instream=imap_open("{$this->hostname/imap:143}inbox",$this->username,$this->userpwd);

if ($this->instream)
{
echo "用户:$this->username 的信箱连接成功。<br>";
return $instream;
}
else
{
echo "用户:$this->username 的信箱连接失败。<br>";
return 0;
}
}

function close()
{
if(imap_close($this->instream))
{
echo "<hr>已经与服务器 $this->hostname 断开连接。";
return 1;
}
else
{
echo "<hr>与服务器 $this->hostname 断开连接失败。";
return 0;
}
}

function checkmailbox()
{
$mboxinfo=@imap_mailboxmsginfo($this->instream);
//$mboxinfo=imap_check($this->instream);
if ($mboxinfo)
if ($mboxinfo->nmsgs>0)
{
echo "您的收件箱里共有邮件数:".$mboxinfo->nmsgs."<br>";
echo "未读邮件数:".$mboxinfo->unread."<br>";
//echo "新邮件数:".$mboxinfo->rescent."<br>";
echo "总共占用空间:".$mboxinfo->size."字节<br>";
echo "最新邮件日期:".$mboxinfo->date."<br><hr>";
}
else
{
echo "您的信箱里没有邮件。<br><hr>";
}
else
{
echo '<font color="red">错误:无法获取收件箱的信息。</font>';
}

echo '<table border="1">';
$sortby="sortdate";
$sort_reverse=1;
$sorted = imap_sort($this->instream, $sortby, $sort_reverse, se_uid);

for ($i=1;$i<=$mboxinfo->nmsgs;$i++)
{
$msgheader = @imap_header($this->instream, imap_msgno($this->instream, $sorted[$i-1]));
//日期
if (isset($msgheader->date))
{
$date = $msgheader->date;
if (ord($date) > 64)
$date = substr($date, 5);
while (strstr(' ', $date))
{
$date = str_replace(' ', ' ', $date);
}
}

if (isset($msgheader->from[0]))
{
$from = $msgheader->from[0];
if (isset($from->personal))
{
$frm = trim($from->personal);
}
else
if (isset($from->mailbox) && isset($from->host))
{
$frm = $from->mailbox . '@' . $from->host;
}
else
if (isset($msgheader->fromaddress))
$frm = trim($h->fromaddress);
}
else
if (isset($msgheader->fromaddress))
$frm = trim($msgheader->fromaddress);

if (isset($msgheader->toaddress))
$to = trim($msgheader->toaddress);
else
$to = "未知";
 

if (isset($msgheader->subject))
$sub = trim($this->decode_mime_string($msgheader->subject));
if (isset($msgheader->size))
$msg_size = ($msgheader->size > 1024) ? sprintf("%.0f kb", $msgheader->size / 1024) : $msgheader->size;
if (strlen($frm) > 50)
$frm = substr($frm, 0, 50) . '...';
if (strlen($sub) > 50)
$sub = substr($sub, 0, 50) . '...';

echo '<tr>';
echo '<td align="center"><input type="checkbox" name="check"></td><td>'.$frm.'</td><td><a href="showbody_imap.php?usr='.$this->username.'&pwd='.$this->userpwd.'&msg='.$i.'">'.$sub.'</a></td><td>'.$date.'</td><td>'.$msg_size.'</td>';
echo '</tr>';
}
echo "</table>";
}

function decode_mime_string ($string)
{
$pos = strpos($string, '=?');
if (!is_int($pos))
{
return $string;
}

$preceding = substr($string, 0, $pos); // save any preceding text

$search = substr($string, $pos+2, 75); // the mime header spec says this is the longest a single encoded word can be
$d1 = strpos($search, '?');
if (!is_int($d1))
{
return $string;
}

$charset = substr($string, $pos+2, $d1);
$search = substr($search, $d1+1);

$d2 = strpos($search, '?');
if (!is_int($d2))
{
return $string;
}

$encoding = substr($search, 0, $d2);
$search = substr($search, $d2+1);

$end = strpos($search, '?=');
if (!is_int($end))
{
return $string;
}

$encoded_text = substr($search, 0, $end);
$rest = substr($string, (strlen($preceding . $charset . $encoding . $encoded_text)+6));

switch ($encoding)
{
case 'q':
case 'q':
$encoded_text = str_replace('_', '%20', $encoded_text);
$encoded_text = str_replace('=', '%', $encoded_text);
$decoded = urldecode($encoded_text);
break;

case 'b':
case 'b':
$decoded = urldecode(base64_decode($encoded_text));
break;

default:
$decoded = '=?' . $charset . '?' . $encoding . '?' . $encoded_text . '?=';
break;
}

return $preceding . $decoded . decode_mime_string($rest);
}


function display_toaddress ($user, $server, $from)
{
return is_int(strpos($from, $this->get_barefrom($user, $server)));
}

function get_barefrom($user, $server)
{
$barefrom = "$user@$real_server";

return $barefrom;
}

function get_structure($msg_num)
{
$structure=imap_fetchstructure($this->instream,$msg_num);
//echo gettype($structure);
return $structure;
}

function proc_structure($msg_part, $part_no, $msg_num)
{
if ($msg_part->ifdisposition)
{
// see if it has a disposition
// the only thing i know of that this
// would be used for would be an attachment
// lets check anyway
if ($msg_part->disposition == "attachment")
{
// if it is an attachment, then we let people download it
// first see if they sent a filename
$att_name = "unknown";
for ($lcv = 0; $lcv < count($msg_part->parameters); $lcv++)
{
$param = $msg_part->parameters[$lcv];

if ($param->attribute == "name")
{
$att_name = $param->value;
break;
}
}

// you could give a link to download the attachment here....
echo '<a href="'.$att_name.'">'.$att_name.'</a><br>';
$fp=fopen(".\\$att_name","w+");
fputs($fp,imap_base64(imap_fetchbody($this->instream,$msg_num,$part_no)));
fclose($fp);
}
else
{
// i guess it is used for something besides attachments????
}
}
else
{
// not an attachment, lets see what this part is...
switch ($msg_part->type)
{
case typetext:
$mime_type = "text";
break;
case typemultipart:
$mime_type = "multipart";
// hey, why not use this function to deal with all the parts
// of this multipart part :)
for ($i = 0; $i < count($msg_part->parts); $i++)
{
if ($part_no != "")
{
$part_no = $part_no.".";
}
for ($i = 0; $i < count($msg_part->parts); $i++)
{
$this->proc_structure($msg_part->parts[$i], $part_no.($i + 1), $msg_num);
}
}
break;
case typemessage:
$mime_type = "message";
break;
case typeapplication:
$mime_type = "application";
break;
case typeaudio:
$mime_type = "audio";
break;
case typeimage:
$mime_type = "image";
break;
case typevideo:
$mime_type = "video";
break;
case typemodel:
$mime_type = "model";
break;
default:
$mime_type = "unknown";
// hmmm....
}

$full_mime_type = $mime_type."/".$msg_part->subtype;
//echo $full_mime_type.'<hr>';

// decide what you what to do with this part
// if you want to show it, figure out the encoding and echo away
switch ($msg_part->encoding)
{
case encbase64:
// use imap_base64 to decode
$fp=fopen(".\\$att_name","w+");
fputs($fp,imap_base64(imap_fetchbody($this->instream,$msg_num,$part_no)));
fclose($fp);
break;
case encquotedprintable:
// use imap_qprint to decode
//echo ereg_replace("\n","<br>",imap_qprint(imap_fetchbody($this->instream,$msg_num,$part_no)));
echo '<pre>'.imap_qprint(imap_fetchbody($this->instream,$msg_num,$part_no)).'</pre>';
break;
case encother:
// not sure if this needs decoding at all
break;
default:
}
}
}


};

?>

use_imap.php (前面那个应该是 imap_new.php)

<html>
<head>
<style type="text/css">
<!--
td { font-size:9pt}
a {font-szie:9pt}
body {font-size:9pt}
input {font-size:9pt}
select {font-size:9pt}
-->
</style>
<title>php webmail system</title>
</head>
<body bgcolor="#ffffff">

<?php
include("imap_new.php");
$imap=new myimap;
$imap->hostname="192.168.100.13";
$imap->port=110;
$imap->username="alpha";
$imap->userpwd="enter";
$imap->open();
$imap->checkmailbox();
//$imap->getmailstructure($imap->instream,1);
@$imap->close();

?>

</body>
</html>

showbody_imap.php

<html>
<head>
<title>biz2biz webmail system</title>
<style type="text/css">
<!--
td { font-size:9pt}
a {font-szie:9pt}
body {font-size:9pt}
input {font-size:9pt}
select {font-size:9pt}
-->
</style>
</head>
<body>
<?php
include("imap_new.php");
$imap=new myimap;
$imap->hostname="192.168.100.13";
$imap->port=110;
$imap->username=$usr;
$imap->userpwd=$pwd;
$imap->open();
$mail_structure=$imap->get_structure($msg);
//echo gettype($mail_structure->parts);
//echo count($mail_structure->parts);
$imap->proc_structure($mail_structure,"",$msg);

@$imap->close();
?>
</body>
</html>

 
 收藏本文  打印本文  论坛讨论  关闭窗口
· 上一篇:用PHP连mysql和oracle数据库性能比较
· 下一篇:php和js交互一例
· require()和include()的区别
· MySQL中文模糊检索问题的解决方法
· 一生成柱状图、横柱图、折线图的类
· 在Linux下安装PHP,APACHE,MYSQL,PERL的方法
· PHP应用技巧七则


关于本站 | 联系我们 | 业务合作 | 客户案例 | 诚聘英才 | 广告合作 | 收藏本站
海口动网先锋网络科技有限公司版权所有
Copyright © 2000 - 2006 Cndw.Com
中华人民共和国电信与信息服务业务经营许可证编号 琼 ICP 020077