Retrieve and save mail attachtments with PHP

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

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 =  

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):

$login = 'mypoplogin';
$password = 'mypoppassword';
$mbox = imap_open("INBOX", "$login", "$password")  
or die("Could not open Mailbox - try again later!");
// make connection with mailbox and check # messages
if ($hdr = imap_check($mbox)) {
 echo "Num Messages " . $hdr->Nmsgs ."\n\n<br><br>";
    $msgCount = $hdr->Nmsgs;
   } else { echo "failed"; }

 // get message info
    $from = ereg_replace("\"","",$from);

 // Check for attachements and store them
   $struct = imap_fetchstructure($mbox,$msg);
   $contentParts = count($struct->parts);
   if ($contentParts >= 2) {
    for ($ix=2;$ix<=$contentParts;$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(strrev($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(strrev($FileName[$k]),0,4));
      $fileContent = imap_fetchbody($mbox,$msg,$file+2);
   $fileContent = imap_base64($fileContent);

   $localfile = fopen("/var/www/html/storedattachments/$FileName[$k]","w");

Re: Retrieve and save mail attachtments with PHP

*** JAKE wrote/escribió (Sun, 07 Aug 2005 14:31:07 GMT):
Quoted text here. Click to load it

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
-- - Mi sitio sobre programación web
-- Don't e-mail me your questions, post them to the group

Site Timeline