ho preparato questi due script:
1 - da informazioni sulle mailboxes, sugli eaders dei messaggi, stampa l'header di un messagggio specifico, stampa la struttura oggetti-array
Codice PHP:
<?
$mbox = imap_open ("{xxx:143}", "xxx", "xxx");
echo "
<h1>Mailboxes</h1>\n";
$folders = imap_listmailbox ($mbox, "{xxx:143}", "*");
if ($folders == false) {
echo "Call failed
\n";
} else {
while (list ($key, $val) = each ($folders)) {
echo $val."
\n";
}
}
echo "
<h1>Headers in INBOX</h1>\n";
$headers = imap_headers ($mbox);
if ($headers == false) {
echo "Call failed
\n";
} else {
while (list ($key,$val) = each ($headers)) {
echo $val."
\n";
}
}
//prendo il messaggio n. 1 leggo le intestazioni
$header = imap_fetchheader($mbox,1);
echo "
-----------------------------
\n" . $header;
// leggo la struttura
$str = imap_fetchstructure($mbox,1);
echo "
-----------------------------
\n";
echo "<pre>";
print_r($str);
echo "</pre>";
imap_close($mbox);
?>
- il secondo mi visualizza (dovrebbe farmi il download...) nome allegato, estensione, contenuto
Codice PHP:
<?php
$mbox = imap_open ("{xxxx:143}", "xxxx", "xxxx");
//struttura per il messaggio n. 1
$msgno = 1;
$struct = imap_fetchstructure($mbox,$msgno);
$contentParts = count($struct->parts);
if ($contentParts >= 2) {
for ($i=2;$i<=$contentParts;$i++) {
$att[$i-2] = imap_bodystruct($mbox,$msgno,$i);
}
for ($k=0;$k<sizeof($att);$k++) {
if ($att[$k]->parameters[0]->value == "us-ascii" || $att[$k]->parameters[0]->value == "US-ASCII") {
if ($att[$k]->parameters[1]->value != "") {
$selectBoxDisplay[$k] = $att[$k]->parameters[1]->value;
}
} elseif ($att[$k]->parameters[0]->value != "iso-8859-1" && $att[$k]->parameters[0]->value != "ISO-8859-1") {
$selectBoxDisplay[$k] = $att[$k]->parameters[0]->value;
}
}
}
$file = 0; //si assume che ci sia un solo allegato;
$strFileName = $att[$file]->parameters[0]->value;
$strFileType = strrev(substr(strrev($strFileName),0,4));
$fileContent = imap_fetchbody($mbox,$msgno,$file+2);
//downloadFile($strFileType,$strFileName,$fileContent);
showFile ($strFileType,$strFileName,$fileContent);
function showFile($strFileType,$strFileName,$fileContent) {
echo $strFileName;
echo "
------------------------------------------------
\n";
echo $strFileType;
echo "
------------------------------------------------
\n";
echo imap_base64($fileContent);
}
function downloadFile($strFileType,$strFileName,$fileContent) {
$ContentType = "application/octet-stream";
if ($strFileType == ".asf")
$ContentType = "video/x-ms-asf";
if ($strFileType == ".avi")
$ContentType = "video/avi";
if ($strFileType == ".doc")
$ContentType = "application/msword";
if ($strFileType == ".zip")
$ContentType = "application/zip";
if ($strFileType == ".xls")
$ContentType = "application/vnd.ms-excel";
if ($strFileType == ".gif")
$ContentType = "image/gif";
if ($strFileType == ".jpg" || $strFileType == "jpeg")
$ContentType = "image/jpeg";
if ($strFileType == ".wav")
$ContentType = "audio/wav";
if ($strFileType == ".mp3")
$ContentType = "audio/mpeg3";
if ($strFileType == ".mpg" || $strFileType == "mpeg")
$ContentType = "video/mpeg";
if ($strFileType == ".rtf")
$ContentType = "application/rtf";
if ($strFileType == ".htm" || $strFileType == "html")
$ContentType = "text/html";
if ($strFileType == ".xml")
$ContentType = "text/xml";
if ($strFileType == ".xsl")
$ContentType = "text/xsl";
if ($strFileType == ".css")
$ContentType = "text/css";
if ($strFileType == ".php")
$ContentType = "text/php";
if ($strFileType == ".asp")
$ContentType = "text/asp";
if ($strFileType == ".pdf")
$ContentType = "application/pdf";
header ("Content-Type: $ContentType");
header ("Content-Disposition: attachment; filename=$strFileName");
echo imap_base64($fileContent);
}
imap_close($mbox);
?>