Visualizzazione dei risultati da 1 a 6 su 6
  1. #1
    Utente di HTML.it
    Registrato dal
    Oct 2001
    Messaggi
    131

    Leggere Email Attachment via Imap

    sto cercando di leggere i file allegati ad una mail e salvarli in un determinato spazio...

    riesco tranquillamente a collegarmi al server di posta
    e scaricare tutti vari messaggi

    l'unico problema è la decodifica del mime

    qualcuno ha qualche idea di come fare?
    o qualche codice?

    non vorrei che utilizzando imap_body per scaricare la mail mi restituisca la mail già decodificata e quindi non posso decodificare il mime dell'attachment

    (utillizzando mimedecode di pear)

    è da oggi che ci sto

    ciao sam

  2. #2
    Utente di HTML.it L'avatar di iox84
    Registrato dal
    May 2004
    Messaggi
    754
    Ci sto provando anch'io a fare una webmail, purtroppo ho qualche problemino, in fatto di attachments sono solo riuscito a scaricare il nome del file, per fare questo ho utilizzato la funzione imap_bodystruct(), che restituisce un oggetto che chiamero' successivamente "oggetto mail", questo oggetto restituisce un attributo parts che e' un Array di oggetti molto simili "mail", l'nica differenza e' che gli elementi di tale array non hanno l'attributo "parts",

    accedendo ai vari elementi di parts

    $body = imap_bodystruct(....,2);

    L'ultimo parametro della funzione e' 2 a me funziona, ma non credo copra il 100% dei casi(sarebbe troppo facile), lo sto ancora testando.
    Successivamente devi accedere al nome del file:

    if ($body->ifdparameters){
    $att = $body->dparameters[0];
    if ($att->attribute == 'FILENAME' || $att->attribute == 'filename'){
    $file = $att->value;
    }
    }

  3. #3
    Io sto indagando la funzione imap_fetchstructure():

    ho scritto queste righe facendo dei test, ma non ho ricavato molto.... solo nome dell'allegato e codifica caratteri:

    codice:
    echo "
    
    <h1>Mailboxes</h1>\n";
    $folders = imap_listmailbox ($mbox, "{server: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";
        }
    }
    
    echo "
    <hr/>
    ";
    
    echo imap_body($mbox,1);
    
    echo "
    <hr/>
    ";
    
    $structure = imap_fetchstructure($mbox,1);
    
    $parametri = $structure->parts;
    
    foreach ($parametri as $etichetta=>$valore) {
        echo " ||| " . $etichetta . " -> " . $valore . " | ";
    
    	foreach ($valore->parameters as $etichetta2=>$valore2){
     		echo $valore2->value;
    	}
    }
    
    
    
    
    imap_close($mbox);
    ?>

  4. #4
    Utente bannato
    Registrato dal
    Apr 2004
    Messaggi
    1,392
    non vi conviene scaricare direttamente una webmail?

  5. #5
    C'è ache per server sotto win (Apache2)???

    Poi vorrei capire il come + che avere il risultato di una webmail (a me poi servono molte meno caratteristiche)

    - leggere la mail
    - identificarne la provenienza
    - scrivere in un db l'allegato
    - abbinare parole chiave etc.

    varie ed eventuali

  6. #6
    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);
    ?>

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.