|
[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]
这只是测试用的脚本,实际的还要复杂一些………… |
|