天天看点

php接收邮件类

receivemail是一个专门用来接收邮件的php类,支持pop3和imap等邮件协议。可以接收邮件及邮件附件。

中文乱码解决

php接收邮件类

/** 

 * 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接收邮件类

<?php  

include("receivemail.class.php");  

// 创建一个 recivemail 对象  

$obj = new receivemail('[email protected]','*','[email protected]','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 :     目前的状态信息。