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!~~~~~~~~~~~~~~~~~~
";
echo imap_num_msg($mbox);
$structure = imap_fetchstructure($mbox,$jk); echo "imap_fetchstructure()
";
$parts = $structure->parts; echo "structure->parts
";
$fpos=2;
for($i = 1; $i < count($parts); $i++) { echo "loop through parts of email
";
$message["pid"][$i] = ($i);
$part = $parts[$i];
if($part->disposition == "ATTACHMENT") { echo "if ATTACHMENT exists then grab data from it
";
$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
";
$mege="";
$data="";
$mege = imap_fetchbody($mbox,$jk,$fpos);
$filename="$newFilename";
$fp=fopen($savedirpath.$filename,w); echo "Create file at specified location
";
$data=$this->getdecodevalue($mege,$part->type);
fputs($fp,$data); echo "Write data to the file
";
echo ">>>>>>>>>>>>> File ".$savedirpath.$newFilename." ~ now exists!
";
fclose($fp);
$fpos+=1;
imap_mail_move($mbox,'1:1','Processed');
echo "****************************************************
";
echo "* Matched - Download and move to Processed folder. *
";
echo "****************************************************
";
echo "
";
}
}
}
}else{
imap_mail_move($mbox,'1:1','Other');
echo "***************************************************
";
echo "******** No Match - Move to Other folder **********
";
echo "***************************************************
";
}
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);