Jump to content

save email attachment failing


sw9

Recommended Posts

hi everyone,

 

i found a script online that will look at an email account and then save the attachments onto the server. i don't think it's working right. i've been trying it with a few different gmail address and other addresses, and it somehow is working consistently with my main gmail address and attachments are showing up. Yet when i started a new gmail address and sent the SAME attachments there, it worked once and then never worked again (and yes, i have enabled pop and imap for ALL mail). since then i've tried it on tons of different email accounts, and i have no idea why it will only work with my major email account. I can't use that account as I have tens of thousands of messages in it, and I am trying to set up a script that will save some google analytics report (CSV format) each week to the server so i can then parse those into a database.

 

Also when I run this script I get the following warning for every file it parses, so this may be why nothing comes in:

PHP Warning:  imap_fetchstructure(): Bad message number in /var/www/me/streaming/gr/test.php on line 7

 

here is the script attachmentread.class.php I'm using:

 

<?php 

            ######################################
            
            #Coded By Jijo Last Update Date[Jan/19/06]
            
            #####################################

    

            
            ##########################################################            


            ###################### Class readattachment ###############
class readattachment
{
    
        function getdecodevalue($message,$coding)
        {
        if ($coding == 0)
        {
           $message = imap_8bit($message);
        }
        elseif ($coding == 1)
        {
          $message = imap_8bit($message);
        }
        elseif ($coding == 2)
        {
           $message = imap_binary($message);
        }
        elseif ($coding == 3)
        {
       $message=imap_base64($message);
       }
        elseif ($coding == 4)
        {
           $message = imap_qprint($message);
        }
        elseif ($coding == 5)
        {
         $message = imap_base64($message);
        }
        return $message;
        }

            function getdata($host,$login,$password,$savedirpath)
            {
            $mbox = imap_open ($host,  $login, $password) or die("can't connect: " . imap_last_error());
            $message = array();
            $message["attachment"]["type"][0] = "text";
            $message["attachment"]["type"][1] = "multipart";
            $message["attachment"]["type"][2] = "message";
            $message["attachment"]["type"][3] = "application";
            $message["attachment"]["type"][4] = "audio";
            $message["attachment"]["type"][5] = "image";
            $message["attachment"]["type"][6] = "video";
            $message["attachment"]["type"][7] = "other";
            
            for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++)
            {
            $structure = imap_fetchstructure($mbox, $jk , FT_UID);    
            $parts = $structure->parts;
            $fpos=2;
                    for($i = 1; $i < count($parts); $i++)
                       {
                        $message["pid"][$i] = ($i);
                        $part = $parts[$i];

                        if($part->disposition == "ATTACHMENT")
                            {
                            
                            $message["type"][$i] = $message["attachment"]["type"][$part->type] . "/" . strtolower($part->subtype);
                            $message["subtype"][$i] = strtolower($part->subtype);
                            $ext=$part->subtype;
                            $params = $part->dparameters;
                            $filename=$part->dparameters[0]->value;
                                                        
                                    $mege="";
                                    $data="";
                                      $mege = imap_fetchbody($mbox,$jk,$fpos);  
                                    $filename="$filename";
                                    $fp=fopen($filename,w);
                                    $data=$this->getdecodevalue($mege,$part->type);    
                                    fputs($fp,$data);
                                    fclose($fp);
                                    $fpos+=1;
                        
                                
                
                            }
            
                    }
//imap_delete tags a message for deletion
            //imap_delete($mbox,$jk);
        
            }
// imap_expunge deletes all tagged messages
            //imap_expunge($mbox);
            imap_close($mbox);
            }
}


?> 

 

and then the file that calls it:

<?php 
require_once("attachmentread.class.php");
$host="{imap.gmail.com:993/imap/ssl}INBOX";
$login="[email protected]"; //pop3 login
$password="mypass"; //pop3 password
$savedirpath="/var/www/me/streaming/gr" ; // attachement will save in same directory where scripts run othrwise give abs path
$jk=new readattachment(); // Creating instance of class####
$jk->getdata($host,$login,$password,$savedirpath); // calling member function

?> 

 

Does anyone have suggestions as to what might be the problem?

 

thanks much.

 

 

Link to comment
https://forums.phpfreaks.com/topic/112618-save-email-attachment-failing/
Share on other sites

well, after coming to the end of my rope and posting here (4 hours spent prior), i tried one more thing and it appeared to work. i changed my gmail to use pop instead:

<?php $host="{pop.gmail.com:995/pop3/ssl/novalidate-cert}INBOX";
?>

 

and I supressed errors on the function that was causing problems:

@imap_fetchstructure

 

Then, I tried it a few more times and it stopped importing. So can anyone tell me what's going on?

 

Also, the files that DID import, came in at 0kb.......am starting to loose my mind a little!

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.