Avatar of Marthaj
Marthaj
Flag for United States of America

asked on 

PHP and extracting a file from a forwarded email

Asked by: dogsareit
Hello Folks. Been awhile. I am spinning my wheels on trying to extract a file that is attached to a email that was forwarded. I need to extract the file which will be written out a file (decoded). The user forwards an email that has a pdf file attached to it and so the 'forwarded email' appears as an attachment. That I can extract, but I can not extract the file attached to it. I have even tried to parse the forwarded email. I can display  the forwarded email and its attached pdf  but I must not be starting/ending in the right area to parse it because  it will not base64_decode it.

I know this issue has had to occur before and that there must be a solution.
My coding works fine extracting attachments as ling as it is not attached to a forwarded email.

Any help/guidance would be appreciated. Thank you in advance.
My code to extract attachments.
And below that - is how I access the array etc. after extracting the attachments.
 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;
}

Open in new window

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


Open in new window




PHP

Avatar of undefined
Last Comment
Marthaj

8/22/2022 - Mon