php imap收邮件类代码

作者:袖梨 2022-06-24

-- php教程myadmin sql dump
-- version 2.11.6
-- http://www.phpm***yadmin.net
--
-- host: localhost
-- generation time: jun 29, 2010 at 09:27 am
-- server version: 5.0.51
-- php version: 5.2.6

set sql_mode="no_auto_value_on_zero";


/*!40101 set @old_character_set_client=@@character_set_client */;
/*!40101 set @old_character_set_results=@@character_set_results */;
/*!40101 set @old_collation_connection=@@collation_connection */;
/*!40101 set names utf8 */;

--
-- database: `test`
--

-- --------------------------------------------------------

--
-- table structure for table `maillist`
--

create table if not exists `maillist` (
`id` int(11) not null auto_increment,
`title` text collate utf8_unicode_ci not null,
`content` mediumtext collate utf8_unicode_ci not null,
`senduser` varchar(50) collate utf8_unicode_ci not null,
`sendmail` varchar(200) collate utf8_unicode_ci not null,
`sendtime` int(11) not null,
`accept_email` varchar(200) collate utf8_unicode_ci not null,
`cc_email` varchar(200) collate utf8_unicode_ci not null,
`bcc_email` varchar(200) collate utf8_unicode_ci not null,
`msessage_id` varchar(200) collate utf8_unicode_ci not null,
`msgno` int(11) not null comment '邮件的序号,为0表示已经下载结束',
`is_download` tinyint(4) not null default '0',
primary key (`id`)
) engine=myisam default charset=utf8 collate=utf8_unicode_ci auto_increment=1 ;

--
-- dumping data for table `maillist`
--


-- --------------------------------------------------------

--
-- table structure for table `mail_account`
--

create table if not exists `mail_account` (
`id` int(11) not null auto_increment,
`username` varchar(100) not null,
`password` varchar(50) not null,
`emailaddress` varchar(100) not null,
`mailserver` varchar(50) not null,
`servertype` varchar(10) not null,
`port` varchar(10) not null,
primary key (`id`)
) engine=myisam default charset=latin1 auto_increment=1 ;

--
-- dumping data for table `mail_account`
--


-- --------------------------------------------------------

--
-- table structure for table `mail_attach`
--

create table if not exists `mail_attach` (
`id` int(11) not null auto_increment,
`mail_id` int(11) not null,
`filename` varchar(200) collate utf8_unicode_ci not null,
`filename_tmp` varchar(200) collate utf8_unicode_ci not null,
`download_time` datetime not null,
primary key (`id`)
) engine=myisam default charset=utf8 collate=utf8_unicode_ci auto_increment=1 ;

--
-- dumping data for table `mail_attach`
--

config.php文件

$path = "d:/phpnow/htdocs/attach/";
$urlpath = "http://loc*alho*st*/attach/";

?>

list.php邮箱列表页面
header("content-type: text/html; charset=utf-8");
header('expires: '.date('d,d m y h:i:s',mktime(0,0,0,1,1,2000)).' gmt');
header('last-modified:'.gmdate('d,d m y h:i:s').' gmt');
header('cache-control: private, no-cache,must-revalidate');
header('pragma: no-cache');

include_once 'dbconfig.php';
$dblink = mysql教程_connect("localhost",$dbuser,$dbpasswd);
mysql_query("set names 'utf8'");
mysql_select_db("test");
$emailaddress = $_request['email'];
?>


list










$sql = "select * from maillist where accept_email='".$emailaddress."' order by sendtime desc";
$rlt = mysql_query($sql);
while ($row = mysql_fetch_array($rlt)){
$title = strips教程lashes($row['title']);
$senduser = stripslashes($row['senduser']);
$sendtime = $row['sendtime'];
$senddate = date("y-m-d h:i:s",$sendtime+3600*8);
$msessage_id = $row['msessage_id'];
$msgno = $row['msgno'];
$msgflag = "未读";
if(!$msgno){
$msgflag = "已读";
}
echo "";
echo "";
echo "";
echo "";
//echo "";
echo "";
echo "";
echo "";
}
?>
$title$senduser$senddate$msgflag

receuv.php收邮件页面
set_time_limit(0);
include_once 'imap.php';
include_once 'dbconfig.php';
$dblink = mysql_connect("localhost",$dbuser,$dbpasswd);
mysql_query("set names 'utf8'");
mysql_select_db("test");

$emailaddress = $_request['email'];

$sql = "select * from mail_account where emailaddress='".$emailaddress."'";
$rlt = mysql_query($sql);
$row = mysql_fetch_array($rlt);

$username = $row['username'];
$password = $row['password'];
$emailaddress = $row['emailaddress'];
$mailserver = $row['mailserver'];
$servertype = $row['servertype'];
$port = $row['port'];


$mail = new imap($username,$password,$emailaddress,$mailserver,$servertype,$port,false);
$mail->open();
$mailnum = $mail->getmaillist();


for($i=1;$i $mailheader = $mail->head($i);
//echo "

";print_r($mailheader);echo "
";
//echo "
";
$msgno = $mailheader['id'];
$size = $mailheader['size'];
$messageid = $mailheader['message_id'];
$udate = $mailheader['udate'];
$subject = addslashes($mailheader['subject']);
$charset = $mailheader['charset'];
$sendmail = $mailheader['from'];
$senduser = addslashes($mailheader['frompersonal']);
if($senduser==''){
$senduser = $sendmail;
}
$fromcharset = $mailheader['fromcharset'];
if($fromcharset=="default"){
$fromcharset = "gb2312";
}
if($charset=="default"){
$charset = "gb2312";
}
$csql = "select * from maillist where msessage_id='".$messageid."' and sendtime='".$udate."'";
$crlt = mysql_query($csql);
if(mysql_num_rows($crlt)==0){
$subject = iconv($charset,"utf-8",$subject);
$senduser = iconv($fromcharset,"utf-8",$senduser);
$sql = "insert into maillist set title='".$subject."',senduser='".$senduser."',sendmail='".$sendmail."',
sendtime='".$udate."',msessage_id='".$messageid."',msgno='".$msgno."',accept_email='".$emailaddress."'";
mysql_query($sql);
}else{
$sql = "update maillist set msgno='".$msgno."' where msessage_id='".$messageid."' and sendtime='".$udate."'";
mysql_query($sql);
}

}

$mail->close();
header("location:list.php?email=".$emailaddress);
?>

read_mail.php读取邮件页面
include_once 'dbconfig.php';
$msessage_id = $_request['msessage_id'];
$udate = $_request['udate'];
$msgno = $_request['msgno'];
$dblink = mysql_connect("localhost",$dbuser,$dbpasswd);
mysql_query("set names 'utf8'");
mysql_select_db("test");
$email = $_request['email'];
?>


list


echo "
";
$sql = "select * from maillist where sendtime='".$udate."' and msessage_id='".$msessage_id."'";
$rlt = mysql_query($sql);
$row = mysql_fetch_array($rlt);
$mailid = $row['id'];
$title = stripslashes($row['title']);
$senduser = stripslashes($row['senduser']);
$sendmail = stripslashes($row['sendmail']);

$senddate = date("y-m-d h:i:s",$udate+3600*8);

echo "主题:".$title."
";
echo "发件人:".$senduser."
";
echo "发件时间:".$senddate."
";
?>

getmailbody.php页面代码
set_time_limit(0);
header("content-type: text/html; charset=utf-8");
include_once 'imap.php';
include_once 'dbconfig.php';
$dblink = mysql_connect("localhost",$dbuser,$dbpasswd);
mysql_query("set names 'utf8'");
mysql_select_db("test");


$msessage_id = $_request['msessage_id'];
$udate = $_request['udate'];
$msgno = $_request['msgno'];
$emailaddress = $_request['email'];

$sql = "select id,is_download from maillist where sendtime='".$udate."' and msessage_id='".$msessage_id."'";
$rlt = mysql_query($sql);
$row = mysql_fetch_array($rlt);
$is_download = $row['is_download'];
$mail_id = $row['id'];
if($is_download==0){
$d = 1;

$sql = "select * from mail_account where emailaddress='".$emailaddress."'";
$rlt = mysql_query($sql);
$row = mysql_fetch_array($rlt);

$username = $row['username'];
$password = $row['password'];
$emailaddress = $row['emailaddress'];
$mailserver = $row['mailserver'];
$servertype = $row['servertype'];
$port = $row['port'];



$mail = new imap($username,$password,$emailaddress,$mailserver,$servertype,$port,false);
$mail->open();

$mail->settype();
$mail->fetch_structure($msgno);

$attacharr = $mail->getallattachment($msgno);
$i = 1;
$inlineattach = array();
if(!empty($attacharr)){
foreach ($attacharr as $rows){
$filename = $rows['filename'];
$details = $rows['details'];
$attachtype = $rows["attachtype"];
$filesize = $rows["filesize"];
$inlineid = $rows['inline_id'];
$downloadname = $mail->deliverattach($filename,$details,$path,$i);
if($attachtype=="inline"){
$inlineattach[$i-1]["filename"] = $filename;
$inlineattach[$i-1]["downloadname"] = $downloadname;
$inlineattach[$i-1]["filesize"] = $filesize;
$inlineattach[$i-1]["attachtype"] = $attachtype;
$inlineattach[$i-1]["inline_id"] = $inlineid;
}

$isql = "insert into mail_attach set mail_id='".$mail_id."',filename='".$filename."',
filename_tmp='".$downloadname."',download_time=now()";
mysql_query($isql);

$i++;
}
}

$content = $mail->getbody($msgno);
$content = $mail->replaceimg($inlineattach,$content);

$sql = "update maillist set content='".addslashes($content)."',is_download='".$d."' where sendtime='".$udate."' and msessage_id='".$msessage_id."'";
mysql_query($sql);

if($d){
while(true){
if($mail->deletemails($msgno)){
if($mail->emptydeletebox()){
$sql = "update maillist set msgno=0 where sendtime='".$udate."' and msessage_id='".$msessage_id."'";
mysql_query($sql);

$sql = "select id from maillist where msgno0 order by msgno asc";
$rlt = mysql_query($sql);
$i = 1;
while ($row = mysql_fetch_array($rlt)){
$id = $row['id'];
$usql = "update maillist set msgno=".$i." where id='".$id."'";
mysql_query($usql);
$i++;
}
break;
}

}
}
}

$mail->close();
}

$sql = "select * from maillist where sendtime='".$udate."' and msessage_id='".$msessage_id."'";
$rlt = mysql_query($sql);
$row = mysql_fetch_array($rlt);
$content = $row['content'];

$asql = "select * from mail_attach where mail_id='".$mail_id."'";
$arlt = mysql_query($asql);
if(mysql_num_rows($arlt)!=0){
echo "附件:";
while ($arow = mysql_fetch_array($arlt)){
$filename = $arow['filename'];
$filename_tmp = $arow['filename_tmp'];
echo strtolower($filename)." ";
echo "
";
}
}

echo $content;
?>

imap.php页面代码

/**
*+----------------------------------------------------------------------------------------------------+
*| imap message class - recevie mailbox by imap |
*| |
*| author: tiger zhang |
*| mailto: [email protected] |
*| |
*| |
*| (c) copyright 2010, tiger zhang, all rights reseverd |
*+----------------------------------------------------------------------------------------------------+
**
*/
class imap{

var $server='';
var $username='';
var $password='';

var $marubox='';

var $email='';

var $linkserver;//email stream

var $error;

var $mailnum = 0;

var $partsary = array();

var $data_types; // (array)(string) various message part types
var $encoding_types; // (array)(string) various encoding types

var $structure; // (array)(object) contains the complete body structure
var $pid; // (array)(array)(str) part id
var $file_type; // (array)(array)(str) mime type
var $disposition; // (array)(array)(str) inline | attachment
var $fsize; // (array)(array)(int) part size in bytes
var $encoding; // (array)(array)(str) message encoding
var $fname; // (array)(array)(str) original file name
var $inline_id; // (array)(array)(str) string containing the id for multipart/related
var $has_attachments; // (array)(array)(bool)
var $file_code; // (array)(array)(str) charset

function imap($username,$password,$emailaddress,$mailserver='localhost',$servertype='pop',$port='110',$ssl = false){
if($servertype=='imap')
{
if($port=='') $port='143';
$strconnect='{'.$mailserver.':'.$port. '}inbox';
}
else
{
$strconnect='{'.$mailserver.':'.$port. '/pop3'.($ssl ? "/ssl" : "").'}inbox';
}
$this->server = $strconnect;
$this->username = $username;
$this->password = $password;
$this->email = $emailaddress;
}
function open(){
//$this->linkserver = @imap_open($this->mailbox,$this->username,$this->password);
$this->linkserver=@imap_open($this->server,$this->username,$this->password) or die("connecting to mail server,");

if(!$this->linkserver)
{
echo "error: connecting to mail server,";
exit;
}
}

function checkopen(){
if(imap_ping($this->linkserver) === false){
$this->error = 'imap_open的连接错误:'.imap_last_error();
return false;
}else{
$this->error = 'ok';
return true;
}
}

function nd(){
$checkresult = imap_check($this->linkserver);
$nummsgs = $checkresult->nmsgs;
$date = $checkresult->date;
$checkreturn[] = $nummsgs;
$checkreturn[] = $date;
return $checkreturn;
}

function getmsgno($messageid){
return imap_uid($this->linkserver,$messageid);
}

/**
* 获取邮件总数
*
*/
function getmaillist(){
$checkresult = imap_check($this->linkserver);
$nummsgs = $checkresult->nmsgs;
return $nummsgs;
}

/**
* 获取邮件头内容
* @param unknown_type $id
* @param unknown_type $all
* @param unknown_type $several
* @return array
(
[id] => 4
[size] => 1697
[message_id] =>
[udate] => 1276687275
[subject] => 测试
[charset] => gbk
[topersonal] => test
[tocharset] => gbk
[to] => [email protected]
[frompersonal] => 五月的饼干屋
[fromcharset] => gbk
[from] => [email protected]
)
*/
function head($id = '',$allhead=false,$all = false,$several = array()){
if($all == true){
$checkreturn = imap::nd();
$nummsgs = $checkreturn[0];
for($i=0;$i $head_array[$i]['id'] = $i+1;
$head = imap_header($this->linkserver,$i+1);
if($head->udate == ''){
$head_array[$i]['udate'] = '';
}else{
$head_array[$i]['udate'] = $head->udate;
}
if($head->subject == ''){
$head_array[$i]['subject'] = '';
}else{
$head_array[$i]['subject'] = $head->subject;
$analyze_charset = imap_mime_header_decode($head_array[$i]['subject']);
//echo "

".print_r($analyze_charset);echo "
";
$head_array[$i]['subject'] = $analyze_charset[0]->text;
}
if($head->to == ''){
$head_array[$i]['to'] == '';
}else{
$to = $head->to;
$to = $to[0]->mailbox.'@'.$to[0]->host;
$head_array[$i]['to'] = $to;
$analyze_charset = imap_mime_header_decode($head_array[$i]['to']);
$head_array[$i]['to'] = $analyze_charset[0]->text;
}
if($head->from == ''){
$head_array[$i]['personal'] == '';
$head_array[$i]['from'] == '';
}else{
$from = $head->from;

$head_array[$i]['personal'] = $from[0]->personal;
$analyze_charset = imap_mime_header_decode($head_array[$i]['personal']);
$head_array[$i]['personal'] = $analyze_charset[0]->text;

$fromaddress = $from[0]->mailbox.'@'.$from[0]->host;
$head_array[$i]['from'] = $fromaddress;
$analyze_charset = imap_mime_header_decode($head_array[$i]['from']);
$head_array[$i]['from'] = $analyze_charset[0]->text;
}
}
return $head_array;
}

if(count($several) > 0){
$num = count($several);
for($i=0;$i $head_array[$i]['id'] = $several[$i];

$head = imap_header($this->linkserver,$several[$i]);
if($head->udate == ''){
$head_array[$i]['udate'] = '';
}else{
$head_array[$i]['udate'] = $head->udate;
}
if($head->subject == ''){
$head_array[$i]['subject'] = '';
}else{
$head_array[$i]['subject'] = $head->subject;
$analyze_charset = imap_mime_header_decode($head_array[$i]['subject']);
$head_array[$i]['subject'] = $analyze_charset[0]->text;
}
if($head->to == ''){
$head_array[$i]['to'] == '';
}else{
$to = $head->to;
$to = $to[0]->mailbox.'@'.$to[0]->host;
$head_array[$i]['to'] = $to;
$analyze_charset = imap_mime_header_decode($head_array[$i]['to']);
$head_array[$i]['to'] = $analyze_charset[0]->text;
}
if($head->from == ''){
$head_array[$i]['personal'] == '';
$head_array[$i]['from'] == '';
}else{
$from = $head->from;

$head_array[$i]['personal'] = $from[0]->personal;
$analyze_charset = imap_mime_header_decode($head_array[$i]['personal']);
$head_array[$i]['personal'] = $analyze_charset[0]->text;

$fromaddress = $from[0]->mailbox.'@'.$from[0]->host;
$head_array[$i]['from'] = $fromaddress;
$analyze_charset = imap_mime_header_decode($head_array[$i]['from']);
$head_array[$i]['from'] = $analyze_charset[0]->text;
}
}
return $head_array;
}
$head = imap_header($this->linkserver,$id);
if($allhead){
return $head;
}
$head_array['id'] = $id;
$head_array['size'] = $head->size;
if($head->message_id == ''){
$head_array['message_id'] = '';
}else{
$head_array['message_id'] = $head->message_id;
}
if($head->udate == ''){
$head_array['udate'] = '';
}else{
$head_array['udate'] = $head->udate;
}
if($head->subject == ''){
$head_array['subject'] = '';
}else{
$head_array['subject'] = $head->subject;
$analyze_charset = imap_mime_header_decode($head_array['subject']);
$head_array['charset'] = $analyze_charset[0]->charset;
$head_array['subject'] = $analyze_charset[0]->text;
}

if($head->to == ''){
$head_array['topersonal'] == '';
$head_array['tocharset'] == '';
$head_array['to'] == '';
}else{
$to = $head->to;
$head_array['topersonal'] = $to[0]->personal;
$analyze_charset = imap_mime_header_decode($head_array['topersonal']);

$head_array['topersonal'] = $analyze_charset[0]->text;
$head_array['tocharset'] = $analyze_charset[0]->charset;
$to = $to[0]->mailbox.'@'.$to[0]->host;

$head_array['to'] = $to;
$analyze_charset = imap_mime_header_decode($head_array['to']);
$head_array['to'] = $analyze_charset[0]->text;
}
if($head->reply_to == ''){

}
if($head->from == ''){
$head_array['frompersonal'] == '';
$head_array['fromcharset'] == '';
$head_array['from'] == '';
}else{
$from = $head->from;

$head_array['frompersonal'] = $from[0]->personal;
$analyze_charset = imap_mime_header_decode($head_array['frompersonal']);
$head_array['frompersonal'] = $analyze_charset[0]->text;
$head_array['fromcharset'] = $analyze_charset[0]->charset;

$fromaddress = $from[0]->mailbox.'@'.$from[0]->host;
$head_array['from'] = $fromaddress;
$analyze_charset = imap_mime_header_decode($head_array['from']);
$head_array['from'] = $analyze_charset[0]->text;
}
return $head_array;
}

function analyze_body($id){
$structure = imap_fetchstructure($this->linkserver,$id);
return $structure;
}

/**
* 设置code和filetype
*
*/
function settype()
{
$this->data_types = array();

$this->data_types[0] = 'text';
$this->data_types[1] = 'multipart';
$this->data_types[2] = 'message';
$this->data_types[3] = 'application';
$this->data_types[4] = 'audio';
$this->data_types[5] = 'image';
$this->data_types[6] = 'video';
$this->data_types[7] = 'other';

$this->encoding_types = array();

$this->encoding_types[0] = '7bit';
$this->encoding_types[1] = '8bit';
$this->encoding_types[2] = 'binary';
$this->encoding_types[3] = 'base64';
$this->encoding_types[4] = 'quoted-printable';
$this->encoding_types[5] = 'other';

return;
}

/**
* * 遍历structure取出所有的相关信息
*
* @param unknown_type $mid
* @param unknown_type $sub_part
* @param unknown_type $sub_pid
* @param unknown_type $n
* @param unknown_type $is_sub_part
* @param unknown_type $skip_part
* @return unknown
*+----------------------------------------------------------------------------------------------------+
*| imap message scanner - scans information provided by imap_fetchstructure() |
*| |
*| author: richard york |
*| mailto:richy at smilingsouls.net |
*| http://www.*sm**ilingsouls.net |
*| modified by:tiger zhang(2010-6-20) |
*| |
*| (c) copyright 2004, richard york, all rights reseverd |
*+----------------------------------------------------------------------------------------------------+
**
*/


function fetch_structure($mid, $sub_part = null, $sub_pid = null, $n = 0, $is_sub_part = false, $skip_part = false){
if (!is_array($sub_part))
{
$this->structure[$mid] = imap_fetchstructure($this->linkserver, $mid);
}

if (isset($this->structure[$mid]->parts) || is_array($sub_part))
{
if ($is_sub_part == false)
{
$parts = $this->structure[$mid]->parts;
}

else
{
$parts = $sub_part;
$n++;
}

for($p = 0, $i = 1; $p {
// skip the following...
// skip multipart/mixed!
// skip subsequent multipart/alternative if this part is message/rfc822
// skip multipart/related

$ftype = (empty($parts[$p]->type))? $this->data_types[0].'/'.strtolower($parts[$p]->subtype) : $this->data_types[$parts[$p]->type].'/'.strtolower($parts[$p]->subtype);
$encoding = (empty($parts[$p]->encoding))? $this->encoding_types[0] : $this->encoding_types[$parts[$p]->encoding];
$skip_next = ($ftype == 'message/rfc822')? true : false;

if ($ftype == 'multipart/mixed' || $skip_part == true && $ftype == 'multipart/alternative' || $ftype == 'multipart/related')
{
$n--;
}

else
{
$this->pid[$mid][$n] = ($is_sub_part == false)? $i : $sub_pid.'.'.$i;
$this->file_type[$mid][$n] = $ftype;
$this->encoding[$mid][$n] = $encoding;
$this->fsize[$mid][$n] = (!isset($parts[$p]->bytes) || empty($parts[$p]->bytes))? 0 : $parts[$p]->bytes;

# force inline disposition if none is present

if ($parts[$p]->ifdisposition == true)
{
$this->disposition[$mid][$n] = strtolower($parts[$p]->disposition);

if (strtolower($parts[$p]->disposition) == 'attachment')
{
if ($parts[$p]->ifdparameters == true)
{
$params = $parts[$p]->dparameters;

foreach ($params as $param)
{
if(strtolower($param->attribute) == 'filename')
{
$this->fname[$mid][$n] = $param->value;
break;
}
}
}
}else if (strtolower($parts[$p]->disposition) == 'inline')
{
if ($parts[$p]->ifdparameters == true)
{
$params = $parts[$p]->dparameters;

foreach ($params as $param)
{
if(strtolower($param->attribute) == 'filename')
{
$this->fname[$mid][$n] = $param->value;
break;
}
}
}
}
}
else if ($parts[$p]->ifparameters == true)
{
$this->disposition[$mid][$n] = 'inline';
$params = $parts[$p]->parameters;
foreach ($params as $param)
{
if(strtolower($param->attribute) == 'name')
{
$this->fname[$mid][$n] = $param->value;
break;
}else if(strtolower($param->attribute) == 'charset'){
$this->file_code[$mid][$n] = strtolower($param->value);
&n

相关文章

精彩推荐