Function RtnExtractAttachments($inbox, $email_number) { //echo '<BR>AT RtnExtractAttachments'; /* get mail structure */ $structure = imap_fetchstructure($inbox, $email_number); $attachments = array(); /* if any attachments found... */ if(isset($structure->parts) && count($structure->parts)) { for($i = 0; $i < count($structure->parts); $i++) { $attachments[$i] = array( 'is_attachment' => false, 'filename' => '', 'name' => '', 'attachment' => ''); if($structure->parts[$i]->ifdparameters) { foreach($structure->parts[$i]->dparameters as $object) { if(strtolower($object->attribute) == 'filename') { $attachments[$i]['is_attachment'] = true; $attachments[$i]['filename'] = $object->value; } // end of if(strtolower } // foreach($structure->parts } //if($structure->parts if($structure->parts[$i]->ifparameters) { foreach($structure->parts[$i]->parameters as $object) { if(strtolower($object->attribute) == 'name') { $attachments[$i]['is_attachment'] = true; $attachments[$i]['name'] = $object->value; } // end of if(strtolower } // end foreach($structure-->parts[$i]->parameters as $object) } // end of if($structure->parts[$i]->ifparameters) if($attachments[$i]['is_attachment']) { $attachments[$i]['attachment'] = imap_fetchbody($inbox, $email_number, $i+1); /* 3 = BASE64 encoding */ if($structure->parts[$i]->encoding == 3) { $attachments[$i]['attachment'] = base64_decode($attachments[$i]['attachment']); } /* 4 = QUOTED-PRINTABLE encoding */ elseif($structure->parts[$i]->encoding == 4) { $attachments[$i]['attachment'] = quoted_printable_decode($attachments[$i]['attachment']); } // end of elsef($structure->parts } // end of } } // end of if(isset($structure->parts) // GET RID OF EMPTY NULL VALUES IN THE ARRAY $attachments = array_map('array_filter', $attachments); $attachments = array_filter( $attachments ); // RETURN ARRAY THAT CONTAINS ALL THE ATTACHMENTS return $attachments; }And the code I use to rewrite the file:
foreach($attachments as $attachment) { if($attachment['is_attachment'] == 1) { $filename = $attachment['name']; if(empty($filename)) $filename = $attachment['filename']; { // empty line } if (!preg_match('/pdf/', $filename)) { echo '<BR><BR>ERROR ** NOT A PDF FILE - SKIPPING - NOT PROCESSING - SUBJECT LINE: ' . $subject . ' FILENAME: ' . $filename; $_SESSION['NbrOfInvalidAttachments'] = ($_SESSION['NbrOfInvalidAttachments'] + 1); $strLogMsg = 'ERROR - NOT PDF FILE' . ' FILENAME: ' . strval($filename) . ' SUBJECT LINE CONTAINS: ' . $subject . PHP_EOL; RtnWriteLogMsg($strLogMsg); continue; }else{ echo '<BR><BR>IS A VALID PDF FILE - CONTINUING PROCESSING: ' . $filename; // CREATE NEW SEQ NBR $strSeqNbr = ($strSeqNbr + 1); $strSeq = 'SEQNBR_' . strval($strSeqNbr); // CREATE NEW FILENAME FOR UPLOADING $wrkFilename = 'PDF_' . $strSeq . trim('.pdf'); $wrkFilename = str_replace ( ' ' , '_' , $wrkFilename); echo '<BR><BR>NEW PDF FILENAME: ' . $wrkFilename; $wrkNewFileName = trim('./workpdf/') . trim($wrkFilename); file_put_contents($wrkNewFileName, $attachment['attachment']) or die('FPUT CONTENTS FAILED'); } } // END OF foreach($attachments as $attachment) }
Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.
”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.
Our community of experts have been thoroughly vetted for their expertise and industry experience.
The Distinguished Expert awards are presented to the top veteran and rookie experts to earn the most points in the top 50 topics.