Retrieve and save mail attachtments with PHP

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • JAKE

    Retrieve and save mail attachtments with PHP

    Hi,



    I made a funstion that checks a mail account, retrieves and stores the
    attachments in a folder on my website.

    This works great for .doc files but not for .pdf files.



    I am almost sure the problem is in: $fileContent =
    imap_base64($fi leContent);

    Whet is the encoding for PDF attachments ?



    Can someone help me with this.



    Best Regards,

    JAKE, Belgium



    This is the script (copied the function in a standalone script, the only
    thing this does is open pop account, check messages, store attachments,
    close imap connection):



    <?php
    $login = 'mypoplogin';
    $password = 'mypoppassword' ;
    $mbox = imap_open("{127 .0.0.1:110/pop3/notls}INBOX", "$login", "$password" )
    or die("Could not open Mailbox - try again later!");
    //
    // make connection with mailbox and check # messages
    if ($hdr = imap_check($mbo x)) {
    echo "Num Messages " . $hdr->Nmsgs ."\n\n<br><br>" ;
    $msgCount = $hdr->Nmsgs;
    } else { echo "failed"; }
    $overview=imap_ fetch_overview( $mbox,"1:$msgCo unt",0);
    $size=sizeof($o verview);

    // get message info
    for($i=$size-1;$i>=0;$i--)
    {
    $val=$overview[$i];
    $msg=$val->msgno;
    $from=$val->from;
    $date=$val->date;
    $subj=$val->subject;
    $seen=$val->seen;
    $from = ereg_replace("\ "","",$from );

    // Check for attachements and store them
    $struct = imap_fetchstruc ture($mbox,$msg );
    $contentParts = count($struct->parts);
    if ($contentParts >= 2) {
    for ($ix=2;$ix<=$co ntentParts;$ix+ +) {
    $att[$ix-2] = imap_bodystruct ($mbox,$msg,$ix );
    }
    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 != "") {
    $FileName[$k] = $att[$k]->parameters[1]->value;
    $FileType[$k] = strrev(substr(s trrev($FileName[$k]),0,4));
    }
    } elseif ($att[$k]->parameters[0]->value != "iso-8859-1" &&
    $att[$k]->parameters[0]->value != "ISO-8859-1") {
    $FileName[$k] = $att[$k]->parameters[0]->value;
    $FileType[$k] = strrev(substr(s trrev($FileName[$k]),0,4));
    $fileContent = imap_fetchbody( $mbox,$msg,$fil e+2);
    $fileContent = imap_base64($fi leContent);

    $localfile = fopen("/var/www/html/storedattachmen ts/$FileName[$k]","w");
    fputs($localfil e,$fileContent) ;
    fclose($localfi le);
    }
    }
    }
    }
    imap_close($mbo x);
    ?>


  • Alvaro G Vicario

    #2
    Re: Retrieve and save mail attachtments with PHP

    *** JAKE wrote/escribió (Sun, 07 Aug 2005 14:31:07 GMT):[color=blue]
    > I am almost sure the problem is in: $fileContent =
    > imap_base64($fi leContent);
    >
    > Whet is the encoding for PDF attachments ?[/color]

    Mail encoding does not depend on file contents, which is obviously a good
    thing: imagine you could only attach and receive files your mail client is
    coded for!

    Main point: what does "doesn't work" mean for you?


    --
    -- Álvaro G. Vicario - Burgos, Spain
    -- http://bits.demogracia.com - Mi sitio sobre programación web
    -- Don't e-mail me your questions, post them to the group
    --

    Comment

    Working...