Question

Im at a bit of a loss, i have 2 scripts 1 which pulls email attachments from a mailbox and a second one which then parses the attachments and adds them to the DB.

This works ok most of the time, but is throwing up a few issues every now an again. Sometimes the email attachment is created, but not populated (blank file except for the name) and sometimes its just not created (downloaded) at all.

The first script opens a new file and writes to it, the second script then accesses the content of that file. Could these issues be because the file is still open when the second script is attempting to access it?

They run alternatively every 15 seconds.

1st script (its pretty big so i have attempted to just show the parts in question)

            for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) {
                echo "~~~~~~~~~~~~~~BEGIN!~~~~~~~~~~~~~~~~~~\n";
                echo imap_num_msg($mbox);
                $structure = imap_fetchstructure($mbox,$jk);  echo "imap_fetchstructure()\n";  
                $parts = $structure->parts; echo "structure->parts\n";
                $fpos=2;


                for($i = 1; $i < count($parts); $i++) { echo "loop through parts of email\n";
                    $message["pid"][$i] = ($i);
                    $part = $parts[$i];

                    if($part->disposition == "ATTACHMENT") { echo "if ATTACHMENT exists then grab data from it\n";
                        $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;

                        $num = $this->append();
                        $newFilename = $this->addToDB($filename,$num);
                        echo $newFilename."- Added tp DB\n";

                        $mege="";
                        $data="";
                        $mege = imap_fetchbody($mbox,$jk,$fpos);  
                        $filename="$newFilename";
                        $fp=fopen($savedirpath.$filename,w); echo "Create file at specified location\n";
                        $data=$this->getdecodevalue($mege,$part->type);
                        fputs($fp,$data); echo "Write data to the file\n";
                        echo ">>>>>>>>>>>>> File ".$savedirpath.$newFilename." ~ now exists!\n";
                        fclose($fp);
                        $fpos+=1;

                        imap_mail_move($mbox,'1:1','Processed');
                        echo "****************************************************\n";
                        echo "* Matched - Download and move to Processed folder. *\n";
                        echo "****************************************************\n";
                        echo "\n\n\n";

                    }

                }

            }

        }else{
            imap_mail_move($mbox,'1:1','Other');
            echo "***************************************************\n";
            echo "******** No Match - Move to Other folder **********\n";
            echo "***************************************************\n";
        }

        imap_close($mbox);
    }

The 2nd script does a bunch of parsing by taking file names added to the db in the 1st script, then sticking them into the following.

        $addXML = "<xml>".file_get_contents($filename)."</xml>";

        $tickets = simplexml_load_string($addXML);
Was it helpful?

Solution

For anyone who might encounter something similar, i figured out why certain files where appearing blank.

The blank files that where being created where coming from emails that had multiple email attachments. It worked fine with single attachments and the first attachment in multiple attachment emails.

for($i = 1; $i < count($parts); $i++) { echo "loop through parts of email\n";

//some code

if($part->disposition == "ATTACHMENT") { echo "if ATTACHMENT exists then grab data from it\n";

//bunch of code that gets the attachment using the section number

imap_mail_move($mbox,'1:1','Processed');
echo "****************************************************\n";
echo "* Matched - Download and move to Processed folder. *\n";
echo "****************************************************\n";
echo "\n\n\n";
}

}

Basically to get multiple attachments this part loops, but i had the imap_mail_move() function in the loop so the email was moved to a different folder before any other iteration could do its stuff for the other email attachments, hence the blank files

D'oh!

As for it skipping certain emails, i was having a play about with

for ($jk = 1; $jk <= imap_num_msg($mbox); $jk++) { }

It turned out that this was crapping out after about 4 iterations, causing some of the emails to be skipped. At this point im not sure why, however for my purposes i don't actually need this for loop so i have removed it.

I know this was a daft mistake on my part regarding the imap_mail_move(), but i decided to post this in case it might help anyone else in future.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top