日期:2014-05-17 浏览次数:20502 次
receivemail是一个专门用来接收邮件的PHP类,支持POP3和IMAP等邮件协议。可以接收邮件及邮件附件。
中文乱码解决
/** * decode the subject of chinese * * @param string $subject * @return sting */ public function subjectDecode($subject) { $beginStr = substr($subject, 0, 5); if($beginStr == '=?ISO') { $separator = '=?ISO-2022-JP'; $toEncoding = 'ISO-2022-JP'; } else { $separator = '=?GB2312'; $toEncoding = 'GB2312'; } $encode = strstr($subject, $separator); if ($encode) { $explodeArr = explode($separator, $subject); $length = count($explodeArr); $subjectArr = array(); for($i = 0; $i < $length / 2; $i++) { $subjectArr[$i][] = $explodeArr[$i * 2]; if (@$explodeArr[$i * 2 + 1]) { $subjectArr[$i][] = $explodeArr[$i * 2 + 1]; } } foreach ($subjectArr as $arr) { $subSubject = implode($separator, $arr); if (count($arr) == 1) { $subSubject = $separator . $subSubject; } $begin = strpos($subSubject, "=?"); $end = strpos($subSubject, "?="); $beginStr = ''; $endStr = ''; if ($end > 0) { if ($begin > 0) { $beginStr = substr($subSubject, 0, $begin); } if ((strlen($subSubject) - $end) > 2) { $endStr = substr($subSubject, $end + 2, strlen($subSubject) - $end - 2); } $str = substr($subSubject, 0, $end - strlen($subSubject)); $pos = strrpos($str, "?"); $str = substr($str, $pos + 1, strlen($str) - $pos); $subSubject = $beginStr . imap_base64($str) . $endStr; $subSubjectArr[] = iconv ( $toEncoding, 'utf-8', $subSubject ); // mb_convert_encoding($subSubject, 'utf-8' ,'gb2312,ISO-2022-JP'); } } $subject = implode('', $subSubjectArr); } return $subject; }
if(strtolower($sender->mailbox)!='mailer-daemon' && strtolower($sender->mailbox)!='postmaster')
{ $subject = $this->subjectDecode($mail_header->subject);
使用方法如下:
<?php include("receivemail.class.php"); // 创建一个 reciveMail 对象 $obj = new receiveMail('469671292@163.com','*','469671292@163.com','pop.163.com','pop3','110',false); // 连接到邮件服务器 $obj->connect(); //If connection fails give error message and exit // 读取全部信件 $tot = $obj->getTotalMails(); //Total Mails in Inbox Return integer value echo "收到$tot封邮件::<br>"; for($i = $tot; $i > 0; $i--) { $head = $obj->getHeaders($i); // 读取获取邮件头信息,返回数组 **数组键值为 (subject,to,toOth,toNameOth,from,fromName) echo "主题 :: ".$head['subject']."<br>"; echo "收件人 :: ".$head['to']."<br>"; echo "抄送 :: ".$head['toOth']."<br>"; echo "发件人 :: ".$head['from']."<br>"; echo "发件人名称 :: ".$head['fromName']."<br>"; echo "<br><br>"; echo "<br>*******************************************************************************************<br>"; echo $obj->getBody($i); // 邮件正文 $str = $obj->GetAttach($i,"./"); // 获取邮件附件,返回的文件名以逗号隔开。 例如. (mailid, Path to store file) $ar = explode(",",$str); foreach($ar as $key=>$value) echo ($value == "") ? "" : "Atteched File :: " . $value . "<br>"; echo "<br>------------------------------------------------------<br>"; //$obj->deleteMails($i); // Delete Mail from Mail box } $obj->close_mailbox(); //Close Mail Box ?>
?receivemail.class.php需要imap模块支持。
<?php $imap = imap_open("{localhost}mbox-bak","graeme","inferno"); // get recent messages $number = imap_num_recent($imap); echo "Number of recent messages: $number<BR>\n"; // display subjects of messages for ($i=1; $i<=$number; $i++) { $header = imap_header($imap, $i); echo "Subject: ", $header->Subject, "<BR>\n"; } imap_close($imap); ?>
imap_append : ?? ?附加字符串到指定的邮箱中。
imap_base64 : ?? ?解 base64 编码。
imap_body : ?? ?读信的内文。
imap_check : ?? ?返回邮箱信息。
imap_close : ?? ?关闭 imap 链接。
imap_createmailbox : ?? ?建立新的信箱。
imap_d