LinuxSir.cn,穿越时空的Linuxsir!

 找回密码
 注册
搜索
热搜: shell linux mysql
查看: 1195|回复: 3

最近在写的玩意,用PHP脚本收取POP3信件并写入数据库,差不多可以用了…………

[复制链接]
发表于 2004-11-9 11:52:08 | 显示全部楼层 |阅读模式
[PHP]<?php
//Added by nbxmedia@163.com
//A mail checker,uses Net-POP3 lib

//Set the timeout limit, 0 is recommended.
set_time_limit(0);

//connect to database
require('./inc/db.php');

//init Net-POP3 module
require('./inc/POP3.php');

//init mime_Decoder
require('./inc/mimeDecode.php');

//Create new class form Net_POP3
$pop3 = & new Net_POP3;

//Connect the mail server
if(PEAR::isError( $ret= $pop3->connect($host , $port ) )){
    echo "ERROR: " . $ret->getMessage() . "\n";
    exit();
}

//Login the mailbox
if(PEAR::isError( $ret= $pop3->login($user , $pass,'USER' ) )){
    echo "ERROR: " . $ret->getMessage() . "\n";
    exit();
}

//Get the maillist in the box
$list = $pop3->GetListing();

//Starting to check mailbox
foreach ($list as $mail)
{
        //First time,check the batabase if the mail is already in your mail box
        $sql = "SELECT * FROM mailbox WHERE uidl="."'".$mail['uidl']."'";
        $res = mysql_query($sql) or die(mysql_error());
        if($res)$exist = mysql_num_rows($res);

        //if the mail is a newone or not in the box, get it.
        if(!$exist)
        {
                //decode mail
                //decode settings
                $params['include_bodies'] = true;
                $params['decode_bodies']  = true;
                $params['decode_headers'] = true;
                $params['input']          = $pop3->getMsg($mail['msg_id']);
                $params['crlf']           = "\r\n";
               
                //Start decoding
                $output = Mail_mimeDecode::decode($params);

                //Get the headers information
                $Headers = $output->headers;
               
                //Get the information of the mail
                $MailSize = $pop3->getSize($mail['msg_id']);
                $Timestamp = $Headers['date'];
                $Title = $Headers['subject'];
                $Reciver = $Headers['return-path'];
                $SenderBox = $Headers['x-mailscanner-from'];
                $Sender = $Headers['from'];

                //if it's a single mail and without attachment
                if(!isset($output->parts))
                {
                        //Get more information in the mail
                        $Charset = $output->ctype_parameters['charset'];
                        $MailType = $output->ctype_primary.'/'.$output->ctype_secondary;
                        $Message = $output->body;
                }


                //if there is a mutiple format mail
                if(is_array($output->parts))
                {
                        $MailInfo = $output->parts;
                       
                        //Check parts
                        $Attachment = "";
                        foreach($MailInfo as $Mail)
                        {

                                //Store the text mail first
                                if($Mail->ctype_secondary == "plain" && $Mail->disposition != "attachment")
                                {
                                        $Charset = $Mail->ctype_parameters['charset'];
                                        $MailType = $Mail->ctype_primary.'/'.$Mail->ctype_secondary;
                                        $Message = $Mail->body;
                                }

                                //if there is a html format, replace it
                                if($Mail->ctype_secondary == "html" && $Mail->disposition != "attachment")
                                {
                                        $Charset = $Mail->ctype_parameters['charset'];
                                        $MailType = $Mail->ctype_primary.'/'.$Mail->ctype_secondary;
                                        $Message = $Mail->body;
                                }


                                //For outlook,I hate it very much......
                                if(is_array($Mail->parts))
                                {
                                        foreach($Mail->parts as $OEMail)
                                        {
                                                if($OEMail->ctype_secondary == "plain"  && $OEMail->disposition != "attachment")
                                                {
                                                        $Charset = $OEMail->ctype_parameters['charset'];
                                                        $MailType = $OEMail->ctype_primary.'/'.$OEMail->ctype_secondary;
                                                        $Message = $OEMail->body;
                                                }

                                                //if there is a html format, replace it
                                                if($OEMail->ctype_secondary == "html" && $OEMail->disposition != "attachment")
                                                {
                                                        $Charset = $OEMail->ctype_parameters['charset'];
                                                        $MailType = $OEMail->ctype_primary.'/'.$OEMail->ctype_secondary;
                                                        $Message = $OEMail->body;
                                                }
                                                //Last depth.....
                                                if(is_array($OEMail->part))
                                                {
                                                        foreach($OEMail as $TempMail)
                                                        {
                                                                if($TempMail->ctype_secondary == "plain"  && $TempMail->disposition != "attachment")
                                                                {
                                                                        $Charset = $TempMail->ctype_parameters['charset'];
                                                                        $MailType = $TempMail->ctype_primary.'/'.$TempMail->ctype_secondary;
                                                                        $Message = $TempMail->body;
                                                                }
                                                                if($TempMail->ctype_secondary == "html" && $TempMail->disposition != "attachment")
                                                                {
                                                                        $Charset = $TempMail->ctype_parameters['charset'];
                                                                        $MailType = $TempMail->ctype_primary.'/'.$TempMail->ctype_secondary;
                                                                        $Message = $TempMail->body;
                                                                }
                                                        }
                                                }
                                        }
                                }

                                //if there is an attachment, store it
                                if($Mail->disposition == "attachment" || $Mail->disposition == "inline")
                                {
                                        //Get the name and file contents
                                        $Filename = $Mail->ctype_parameters['name'];
                                        $Content = $Mail->body;
                                        $FileType = $Mail->ctype_primary.'/'.$Mail->ctype_secondary;
                                        $Dispos = $Mail->disposition;
                                        if($Mail->headers['content-transfer-encoding'] == "base64")$Content = base64_decode($Content);
                                        //Write the file to disk
                                        if($Buffer = fopen("./attachments/".$Filename, "w+"))
                                        {
                                                //write the file into disk
                                                fwrite($Buffer, $Content);
                                                fclose($Buffer);

                                                //Get the filename
                                                $Attachment = $Attachment.$Filename.";;";
                                        }else echo "Can not write file!";
                                }
                        }
                }

                //test output
               
                echo $FileType."<br>";
                echo $Dispos."<br>";
                print_r($output);

                //Save to database
                if($Title)
                {
                        $sql = sprintf("INSERT INTO mailbox (uidl, MailSize, Timestamp, Title, Reciver, SenderBox, Sender, Charset, MailType, Message, Attachment) VALUES ('%s','%s','%s','%s','%s','%s','%s','%s','%s','%s','%s')",
                                $mail['uidl'],
                                $MailSize,
                                $Timestamp,
                                $Title,
                                $Reciver,
                                $SenderBox,
                                $Sender,
                                $Charset,
                                $MailType,
                                addslashes($Message),
                                $Attachment
                                );
                        $res = mysql_query($sql) or die(mysql_error());

                        //Make a delete mark in your mail box
                        if($res)$pop3->deleteMsg($mail['msg_id']);
                }
        }
}

//finish the work!
$pop3->disconnect();
//header("Location: index.php");
exit();
?>[/PHP]





这只是测试用的脚本,实际的还要复杂一些…………
发表于 2004-11-9 18:21:06 | 显示全部楼层
顶   加油!
发表于 2004-11-11 14:06:38 | 显示全部楼层
附件怎么办??
 楼主| 发表于 2004-11-13 00:28:33 | 显示全部楼层
最初由 shatan 发表
附件怎么办??

倒,您就不能先看下注释吗?
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表