195 lines
6.6 KiB
PHP
Raw Normal View History

<?php
//script will fetch an email identified by $msgid, and parse the its parts into an
//array $partsarray
//structure of array:
//$partsarray[<name of part>][<attachment/text>]
//if attachment- subarray is [filename][binary data]
//if text- subarray is [type of text(HTML/PLAIN)][text string]
//i.e.
//$partsarray[3.1][attachment][filename]=filename of attachment in part 3.1
//$partsarray[3.1][attachment][binary]=binary data of attachment in part 3.1
//$partsarray[2][text][type]=type of text in part 2
//$partsarray[2][text][string]=decoded text string in part 2
//$partsarray[not multipart][text][string]=decoded text string in message that isn't multipart
function parsepart($p,$i){
global $link,$msgid,$partsarray;
//where to write file attachments to:
$filestore = '[full/path/to/attachment/store/(chmod777)]';
//fetch part
$part=imap_fetchbody($link,$msgid,$i);
//if type is not text
if ($p->type!=0){
//DECODE PART
//decode if base64
if ($p->encoding==3)$part=base64_decode($part);
//decode if quoted printable
if ($p->encoding==4)$part=quoted_printable_decode($part);
//no need to decode binary or 8bit!
//get filename of attachment if present
$filename='';
// if there are any dparameters present in this part
if (count($p->dparameters)>0){
foreach ($p->dparameters as $dparam){
if ((strtoupper($dparam->attribute)=='NAME') ||(strtoupper($dparam->attribute)=='FILENAME')) $filename=$dparam->value;
}
}
//if no filename found
if ($filename==''){
// if there are any parameters present in this part
if (count($p->parameters)>0){
foreach ($p->parameters as $param){
if ((strtoupper($param->attribute)=='NAME') ||(strtoupper($param->attribute)=='FILENAME')) $filename=$param->value;
}
}
}
//write to disk and set partsarray variable
if ($filename!=''){
$partsarray[$i][attachment] = array('filename'=>$filename,'binary'=>$part);
$fp=fopen($filestore.$filename,"w+");
fwrite($fp,$part);
fclose($fp);
}
//end if type!=0
}
//if part is text
else if($p->type==0){
//decode text
//if QUOTED-PRINTABLE
if ($p->encoding==4) $part=quoted_printable_decode($part);
//if base 64
if ($p->encoding==3) $part=base64_decode($part);
//OPTIONAL PROCESSING e.g. nl2br for plain text
//if plain text
if (strtoupper($p->subtype)=='PLAIN')1;
//if HTML
else if (strtoupper($p->subtype)=='HTML')1;
$partsarray[$i][text] = array('type'=>$p->subtype,'string'=>$part);
}
//if subparts... recurse into function and parse them too!
if (count($p->parts)>0){
foreach ($p->parts as $pno=>$parr){
parsepart($parr,($i.'.'.($pno+1)));
}
}
return;
}
//require('./constantes.php');
$imap_server='{imap.online.net}';
$imap_account='buzuk@lenaour.org';//'scores-decisions.com';
$imap_password='bzh4231*';//catsysyo92';
if (!$mbox = imap_open($imap_server, $imap_account, $imap_password)) die("Connexion IMAP impossible : ". imap_last_error());
$list = imap_list($mbox, $imap_server, "*");
if(is_array($list)) {
reset($list);
foreach ($list as $val) {
echo imap_utf7_decode($val) . "<br />\n";
}
} else {
echo "imap_list a <20>chou<6F> : " . imap_last_error() . "\n";
}
echo "<p><h1>Entetes de mail dans INBOX</h1>\n";
$headers = imap_headers ($mbox);
if ($headers == false) {
echo "Erreur !\n";
} else {
while (list ($key,$val) = each ($headers)) {
echo $val."<br>\n";
}
}
$check = imap_mailboxmsginfo($mbox);
if($check) {
echo "Date: " . $check->Date ."<br />\n" ;
echo "Pilote: " . $check->Driver ."<br />\n" ;
echo "Mailbox: " . $check->Mailbox ."<br />\n" ;
echo "Messages: ". $check->Nmsgs ."<br />\n" ;
echo "R<EFBFBD>cent: " . $check->Recent ."<br />\n" ;
echo "Non lus: " . $check->Unread ."<br />\n" ;
echo "Effac<EFBFBD>s: " . $check->Deleted ."<br />\n" ;
echo "Taille: " . $check->Size ."<br />\n" ;
} else {
echo "imap_check() a <20>chou<6F>: ".imap_last_error(). "<br />\n";
}
$imap_obj = imap_check($mbox);
$imap_sorted = imap_sort($mbox, SORTARRIVAL, 0);
$MN=$imap_obj->Nmsgs;
echo '<br>Nombre de messages '.$MN.'<br/>';
$overview = imap_fetch_overview($mbox,"1:$MN",0);
$k=0;
$partsarray=array();
while( list($key,$val) = each($overview))
{
if (strpos($val->from, 'rapports@neoveille.fr')!==false) {
$unsorted[$k]=$val;//->uid;
/*$unsorted[$k]["subject"]=imap_utf8($val->subject); // Le sujet du message
$unsorted[$k]['from']=$val->from;
$unsorted[$k]['to']=$val->to;
$unsorted[$k]['date']=$val->date;// Date d'exp<78>dition
$unsorted[$k]['message_id - Identification du message
references - est une r<EFBFBD>f<EFBFBD>rence sur l'id de ce message
in_reply_to - est une r<EFBFBD>ponse <EFBFBD> cet identifiant de message
size - taille en octets
uid - UID du message dans la bo<EFBFBD>te aux lettres
msgno - num<EFBFBD>ro de s<EFBFBD>quence du message dans la bo<EFBFBD>te
recent - Ce message est r<EFBFBD>cent
flagged - Ce message est marqu<EFBFBD>
answered - Ce message a donn<EFBFBD> lieu <EFBFBD> une r<EFBFBD>ponse
deleted - Ce message est marqu<EFBFBD> pour l'effacement
seen - Ce message est d<EFBFBD>j<EFBFBD> lu
draft - Ce message est un brouillon
*/
$s=imap_fetchstructure($mbox,$val->message_id);
/*if (count($s->parts)>0){
foreach ($s->parts as $partno=>$partarr){
//parse parts of email
parsepart($partarr,$partno+1);
}
}
else { //for not multipart messages
//get body of message
//decode if quoted-printable
if ($s->encoding==4) $text=quoted_printable_decode($text);
//OPTIONAL PROCESSING
if (strtoupper($s->subtype)=='PLAIN') $text=$text;
if (strtoupper($s->subtype)=='HTML') $text=$text;
$partsarray['not multipart'][text]=array('type'=>$s->subtype,'string'=>$text);
}*/
$full_bodacc=imap_body($mbox,$val->msgno).'[FIN]';
$k++;
}
}
//usort ($unsorted, create_function('$a,$b','setlocale(LC_ALL,$locale);return strcoll($a["subject"],$b["subject"]);'));
$Bodacc=explode("-------------------------------------------------- \r\n--------------------------------------------------", $full_bodacc);
echo '<pre>';
/*var_dump($imap_obj);
print_r($imap_obj);
print_r($imap_sorted);
print_r($unsorted);
print_r($partsarray);*/
print_r($Bodacc);
echo '</pre>';
imap_close($mbox);
?>