1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
# fonction CHECK_PART #
function Check_Part($part,$id){
global $attachments, $debug, $debug_depth, $dispos; $ans=sizeof($attachments);
doDebug("+Checking part $id");
$debug_depth++;
doDebug("Parttype=".$part->type ."/". $part->subtype);
if($part->ifdisposition){ $dispos++; doDebug("<b>Disposition</b>"); }
if($part->ifdescription){ doDebug("<b>Description=".$part->description."</b>"); }
switch($part->type){
case 1: if((strtolower($part->subtype) == "mixed")
or (strtolower($part->subtype) == "alternative")
or (strtolower($part->subtype) == "related")) break;
default: $an = sizeof($attachments);
if($part->ifdparameters){
$dpara = $part->dparameters;
for ($v=0;$v<sizeof($dpara);$v++){
if (eregi("filename", $dpara[$v]->attribute))
$fname = $dpara[$v]->value;}
}
if($part->ifparameters){
if(empty($fname)){
$para = $part->parameters;
for ($v=0;$v<sizeof($para);$v++){
if (eregi("name", $para[$v]->attribute)) $fname = $para[$v]->value;
}
}
}
if(empty($fname))$fname = "(no name)";
$attachments[$an]->id = ($an+1);
$attachments[$an]->part = $id;
$attachments[$an]->filename = $fname;
$attachments[$an]->type = $part->type;
$attachments[$an]->subtype = $part->subtype;
$attachments[$an]->dispos = $dispos;
$attachments[$an]->size = $part->bytes;
$attachments[$an]->encod = $part->encoding;
doDebug("Filename=".$attachments[$an]->filename); break;
}
for($x = 0; $x < count($part->parts); $x++){
$attachments = Check_Part($part->parts[$x], $id.".".($x+1));
}
if($part->ifdisposition) $dispos--;
$debug_depth--;
doDebug("-End of part $id");
return $attachments;
}
# fonction DECODE_MAIL #
function decode_mail($mbox, $msg){
global $attachments, $debug, $debug_depth;
doDebug("+Decoding message $msg"); $debug_depth++;
$obj = imap_fetchstructure($mbox, $msg, FT_UID); $moet_werken=true; $id=1;
while($moet_werken){
$attachments = Check_Part($obj, $id);
$moet_werken=false;
}
$debug_depth--; doDebug("-End of message $msg");
return $attachments;
}
# ouverture de la boite mail #
$mbox = open_mailbox($server, $login, $pass);
# nombre de mails #
$mailcheck = imap_check($mbox);
$mailnumber = $mailcheck->Nmsgs;
# on parcours tous les mails #
$overview = imap_fetch_overview($mbox,"1:$mailnumber",0);
$nbm = sizeof($overview);
for($i= $nbm-1; $i >= 0; $i--)
{
$val = $overview[$i];
$nb = $val->msgno;
$rawsubject = imap_mime_header_decode($val->subject);
$subject = $rawsubject[0]->text;
if($subject == '') {$subject = "(no subject)";};
# Decodage du mail #
$attachments = decode_mail($mbox,$nb);
# Récupération du corps et des pieces jointes #
for($x = 0; $x < sizeof($attachments); $x++)
{
if($x == 0)
{ # corp du mail$ #
$db_body = imap_fetchbody($mbox,$nb, $attachments[$x]->id);
$db_body_type = $attachments[$x]->subtype;
if($attachments[$x]->encod == 1) $db_body = imap_8bit($db_body);
if($attachments[$x]->encod == 2) $db_body = imap_binary($db_body);
if($attachments[$x]->encod == 3) $db_body = imap_base64($db_body);
if($attachments[$x]->encod == 4) $db_body = imap_qprint($db_body);
$type_mess = $attachments[$x]->type;
}
else
{ # piece jointe #
$return_content = imap_fetchbody($mbox,$nb, $attachments[$x]->id);
if($attachments[$x]->encod == 1) $return_content = imap_8bit($return_content);
if($attachments[$x]->encod == 2) $return_content = imap_binary($return_content);
if($attachments[$x]->encod == 3) $return_content = imap_base64($return_content);
if($attachments[$x]->encod == 4) $return_content = imap_qprint($return_content);
//echo $nom_fichier;
$pj_type = $attachments[$x]->type;
if($attachments[$x]->filename != '' && $attachments[$x]->filename != '(no name)')
{
$nom_fichier = $attachments[$x]->filename;
# création de la piece jointe #
$link = REPERTOIRE."/".$nom_fichier;
//echo $db_link_pj;
$newfichier = fopen($link,"w+");
# Ecriture dans le fichier #
fwrite($newfichier,$return_content);
# Fermeture du fichier #
fclose($newfichier);
}
}
}; // for x
}; /// for nbm |
Partager