|
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_delete : 标记欲删除邮件。
imap_deletemailbox : 删除既有信箱。
imap_expunge : 删除已标记的邮件。
imap_fetchbody : 从信件内文取出指定部分。
imap_fetchstructure : 获取某信件的结构信息。
imap_header : 获取某信件的标头信息。
imap_headers : 获取全部信件的标头信息。
imap_listmailbox : 获取邮箱列示。
imap_listsubscribed : 获取订阅邮箱列示。
imap_mail_copy : 复制指定信件到它处邮箱。
imap_mail_move : 移动指定信件到它处邮箱。
imap_num_msg : 取得信件数。
imap_num_recent : 取得新进信件数。
imap_open : 打开 imap 链接。
imap_ping : 检查 imap 是否连接。
imap_renamemailbox : 更改邮箱名字。
imap_reopen : 重开 imap 链接。
imap_subscribe : 订阅邮箱。
imap_undelete : 取消删除邮件标记。
imap_unsubscribe : 取消订阅邮箱。
imap_qprint : 将 qp 编码转成八位。
imap_8bit : 将八位转成 qp 编码。
imap_binary : 将八位转成 base64 编码。
imap_scanmailbox : 寻找信件有无特定字符串。
imap_mailboxmsginfo : 取得目前邮箱的信息。
imap_rfc822_write_address : 电子邮件位址标准化。
imap_rfc822_parse_adrlist : 解析电子邮件位址。
imap_setflag_full : 配置信件标志。
imap_clearflag_full : 清除信件标志。
imap_sort : 将信件标头排序。
imap_fetchheader : 取得原始标头。
imap_uid : 取得信件 uid。
imap_getmailboxes : 取得全部信件详细信息。
imap_getsubscribed : 列出所有订阅邮箱。
imap_msgno : 列出 uid 的连续信件。
imap_search : 搜寻指定标准的信件。
imap_last_error : 最后的错误信息。
imap_errors : 所有的错误信息。
imap_alerts : 所有的警告信息。
imap_status : 目前的状态信息。 |
|
|