Jump to content

read email attachment from last received email and echo out last decimal value


mangy1983

Recommended Posts

I don't know if this is the correct section of the forum to implement this code but l thought since a lot of the guys here helped me with this code l should share my completed code faulty or not (it works for me!) in the hope it helps someone else. It will read an attachment from the last email received to the specified account and remove all data bar data with a numerical decimal value and echo it out. I have spent 3 days trawling the internet and getting help with this code as well as learning from it so l would just like to thank everybody who helped me create it in one way or another.

 

cheers Callum

 

<?php
           
    $mbox = imap_open("{mail.*****.co.uk:143/notls}INBOX", "****@****.co.uk", "*****");  //connects to mailbox on your server
     
     
    $headers = imap_headers($mbox); 
   
    if ($headers == false) { 
        print_r(imap_errors()); 
    } else {





$j = imap_num_msg($mbox);

//if there is a message in your inbox
if( $j > 0 ) {         //this just reads the most recent email. In order to go through all the emails, you'll have to loop through the number of messages








            $info = imap_headerinfo($mbox,$j); 

             
            




            $structure = imap_fetchstructure($mbox, $info->Msgno); 
            
            $attachments = array(); 
             
            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; 
                            } 
                        } 
                    } 
                     
                    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; 
                            } 
                        } 
                    } 
                     
                    if($attachments[$i]['is_attachment']) { 
                        $attachments[$i]['attachment'] = imap_fetchbody( 
                            $mbox, $info->Msgno, $i+1); 
                        if($structure->parts[$i]->encoding == 3) { 
                            // 3 = BASE64 
                            $attachments[$i]['attachment'] = 
                                base64_decode($attachments[$i]['attachment']); 
                        } elseif($structure->parts[$i]->encoding == 4) { 
                            // 4 = QUOTED-PRINTABLE 
                            $attachments[$i]['attachment'] =  
                                quoted_printable_decode($attachments[$i]['attachment']); 
                        } 
                    $attachment =  $attachments[1]['attachment']; 

                    
                    preg_match_all("#\d+\.\d+#", $attachment, $matches);

                    }
                } 
            $riverlevel =  end($matches[0]);
            }                     
        } 
    } 
  


imap_close($mbox); 

echo $riverlevel;  
    
     
?>

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.