Jump to content

Retrieve and echo email attachment using imap help


mangy1983

Recommended Posts

I have spent nearly all day scouring Google for help on this and after trying numerous scripts unsuccessfully and reading up on imap functions but still getting nowhere as well as frying my brain l have had to accept defeat unless anyone of the many gifted coders here can help me out with a script they might have written and used.

 

many thanks Callum

Link to comment
Share on other sites

Thank you for your reply Pikachu2000. Out of all the codes l have tried this was the best one as it reads the last email and echoes it out. as well as the unformatted (I think) body. As far as l can tell the attachments extension is .ZRXPSEND.

 

<?php
$mailbox = imap_open("{mail.******.co.uk:143/notls}INBOX", "creedlevel@*********.co.uk", "insertpasshere");  //connects to mailbox on your server

if ($mailbox == false) {
echo "<p>Error: Can't open mailbox!</p>";
echo imap_last_error();
}
else {

//Check number of messages
$num = imap_num_msg($mailbox);

//if there is a message in your inbox
if( $num > 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
$email = imap_fetchheader($mailbox, $num); //get email header

$lines = explode("\n", $email);

// data we are looking for
$from = "";
$subject = "";
$to = "";
$splittingheaders = true;

for ($i=0; $i < count($lines); $i++) {
if ($splittingheaders) {
// this is a header
$headers .= $lines[$i]."\n";

// look out for special headers
if (preg_match("/^Subject: (.*)/", $lines[$i], $matches)) {
$subject = $matches[1];
}
if (preg_match("/^From: (.*)/", $lines[$i], $matches)) {
$from = $matches[1];
}
if (preg_match("/^To: (.*)/", $lines[$i], $matches)) {
$to = $matches[1];
}

}
}

//We can just display the relevant information in our browser, like below or write some method, that will put that information in a database
echo "FROM: ".$from."<br>";
echo "TO: ".$to."<br>";
echo "SUBJECT: ".$subject."<br>";
echo "BODY: ".imap_qprint(imap_body($mailbox, $num));

imap_expunge($mailbox);
}
else {
echo "No more messages";
}

imap_close($mailbox);
}
?>

 

Below is an example of the attachment l would be sent:

 

 

## Exported ZRXP Block for Halkirk.SG.ir.O

##TSNAMEHalkirk.SG.ir.O;*;

#ZRXPVERSION2206.235;*;ZRXPCREATORZEXP3.9.2;*;

#ZRXPMODEextended;*;

#CUNITm;*;

#SNAMEHalkirk;*;SANR234231;*;SWATERThurso;*;CNR3033705;*;

#CNAMESG;*;CMW86400;*;CTYPEn-min-ip;*;

#RTYPEMomentanwerte;*;RTIMELVLHochaufloesend;*;RORPRoriginal;*;CNTYPE1;*;

##DAYSTART0000

#REXCHANGEHalkirk.SG.ir.O;*;

#RSTATEW6;*;

20080224000000 0.48 0

20080224001500 0.477 0

20080224003000 0.477 0

20080224004500 0.475 0

20080224010000 0.477 0

20080224011500 0.475 0

20080224013000 0.476 0

20080224014500 0.473 0

20080224020000 0.471 0

20080224021500 0.469 0

20080224023000 0.469 0

20080224024500 0.466 0

20080224030000 0.466 0

20080224031500 0.464 0

20080224033000 0.463 0

20080224034500 0.461 0

20080224040000 0.458 0

20080224041500 0.458 0

20080224043000 0.456 0

20080224044500 0.456 0

20080224050000 0.457 0

20080224051500 0.454 0

20080224053000 0.455 0

20080224054500 0.452 0

20080224060000 0.452 0

20080224061500 0.452 0

20080224063000 0.451 0

20080224064500 0.452 0

20080224070000 0.451 0

20080224071500 0.451 0

20080224073000 0.449 0

20080224074500 0.451 0

20080224080000 0.448 0

20080224081500 0.447 0

20080224083000 0.449 0

20080224084500 0.448 0

20080224090000 0.448 0

20080224091500 0.445 0

20080224093000 0.444 0

20080224094500 0.446 0

20080224100000 0.448 0

20080224101500 0.448 0

20080224103000 0.449 0

20080224104500 0.45 0

20080224110000 0.448 0

20080224111500 0.448 0

20080224113000 0.449 0

20080224114500 0.449 0

20080224120000 0.451 0

20080224121500 0.449 0

20080224123000 0.45 0

20080224124500 0.449 0

20080224130000 0.45 0

20080224131500 0.448 0

20080224133000 0.452 0

20080224134500 0.452 0

20080224140000 0.454 0

20080224141500 0.456 0

20080224143000 0.455 0

20080224144500 0.459 0

20080224150000 0.457 0

20080224151500 0.456 0

20080224153000 0.454 0

20080224154500 0.457 0

20080224160000 0.457 0

20080224161500 0.458 0

20080224163000 0.461 0

20080224164500 0.458 0

20080224170000 0.458 0

20080224171500 0.46 0

20080224173000 0.46 0

20080224174500 0.458 0

20080224180000 0.462 0

20080224181500 0.462 0

20080224183000 0.459 0

20080224184500 0.461 0

20080224190000 0.459 0

20080224191500 0.456 0

20080224193000 0.453 0

20080224194500 0.448 0

20080224200000 0.446 0

20080224201500 0.443 0

20080224203000 0.44 0

20080224204500 0.439 0

20080224210000 0.431 0

20080224211500 0.428 0

20080224213000 0.423 0

20080224214500 0.42 0

20080224220000 0.416 0

20080224221500 0.414 0

20080224223000 0.41 0

20080224224500 0.407 0

20080224230000 0.406 0

20080224231500 0.403 0

20080224233000 0.401 0

20080224234500 0.399 0

20080225000000 0.397 0

20080225001500 0.394 0

20080225003000 0.393 0

20080225004500 0.392 0

20080225010000 0.391 0

20080225011500 0.39 0

20080225013000 0.389 0

20080225014500 0.389 0

20080225020000 0.388 0

20080225021500 0.388 0

20080225023000 0.39 0

20080225024500 0.39 0

20080225030000 0.39 0

20080225031500 0.389 0

20080225033000 0.391 0

20080225034500 0.391 0

20080225040000 0.39 0

20080225041500 0.39 0

20080225043000 0.39 0

20080225044500 0.39 0

20080225050000 0.39 0

20080225051500 0.388 0

20080225053000 0.388 0

20080225054500 0.388 0

20080225060000 0.388 0

20080225061500 0.389 0

20080225063000 0.388 0

20080225064500 0.387 0

20080225070000 0.387 0

20080225071500 0.387 0

20080225073000 0.387 0

20080225074500 0.386 0

20080225080000 0.387 0

20080225081500 0.386 0

20080225083000 0.386 0

20080225084500 0.386 0

20080225090000 0.386 0

20080225091500 0.384 0

20080225093000 0.386 0

20080225094500 0.386 0

20080225100000 0.386 0

20080225101500 0.386 0

20080225103000 0.388 0

20080225104500 0.387 0

20080225110000 0.387 0

20080225111500 0.387 0

20080225113000 0.387 0

20080225114500 0.387 0

20080225120000 0.387 0

20080225121500 0.386 0

20080225123000 0.386 0

20080225124500 0.387 0

20080225130000 0.383 0

20080225131500 0.383 0

20080225133000 0.383 0

20080225134500 0.383 0

20080225140000 0.382 0

20080225141500 0.382 0

20080225143000 0.382 0

20080225144500 0.383 0

20080225150000 0.382 0

20080225151500 0.382 0

20080225153000 0.379 0

20080225154500 0.38 0

20080225160000 0.379 0

20080225161500 0.38 0

20080225163000 0.379 0

20080225164500 0.378 0

20080225170000 0.383 0

20080225171500 0.379 0

20080225173000 0.383 0

20080225174500 0.382 0

20080225180000 0.382 0

20080225181500 0.382 0

20080225183000 0.382 0

20080225184500 0.382 0

20080225190000 0.382 0

20080225191500 0.38 0

20080225193000 0.383 0

20080225194500 0.382 0

20080225200000 0.383 0

20080225201500 0.383 0

20080225203000 0.384 0

20080225204500 0.386 0

20080225210000 0.391 0

20080225211500 0.395 0

20080225213000 0.398 0

20080225214500 0.406 0

20080225220000 0.412 0

20080225221500 0.418 0

20080225223000 0.426 0

20080225224500 0.43 0

20080225230000 0.432 0

20080225231500 0.432 0

20080225233000 0.436 0

20080225234500 0.439 0

20080226000000 0.44 0

20080226001500 0.44 0

20080226003000 0.443 0

20080226004500 0.441 0

20080226010000 0.443 0

20080226011500 0.443 0

20080226013000 0.444 0

20080226014500 0.443 0

20080226020000 0.442 0

20080226021500 0.443 0

20080226023000 0.443 0

20080226024500 0.44 0

20080226030000 0.442 0

20080226031500 0.44 0

20080226033000 0.44 0

20080226034500 0.436 0

20080226040000 0.437 0

20080226041500 0.435 0

20080226043000 0.436 0

20080226044500 0.436 0

20080226050000 0.432 0

20080226051500 0.434 0

20080226053000 0.436 0

20080226054500 0.435 0

20080226060000 0.434 0

20080226061500 0.435 0

20080226063000 0.435 0

 

They are the dates, times and water levels of a river I would like to display on a fishing website l am developing. I think that l would only want one of them or if possible the average of all of them but since l can't even code the attachments retrieval l think this would be impossible for me without at least some help. My best way of learning is to look at working code, running it and then studying it to learn how it works but since l can't even find a suitable code l am finding it very difficult.

 

many thanks Callum

Link to comment
Share on other sites

After further investigating and experimenting l have now got a code that echoes out the attachment but l am not sure how to proceed with it as l am not sure what l am doing. Out of all the information held l am only looking at keeping the 0.477 type of digits in the attachments so that l can find the average of them and display the output as well as saving it to database. Would anyone be able to help with this?

 

thanks Callum

 

Heres my code so far:

 

<?php
           
    $mbox = imap_open("{mail.*******.co.uk:143/notls}INBOX", "creedlevel@*********.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']); 
                        } 
                    echo "<br>" . $attachments[$i]['attachment']; 
                    echo $attachments[1];

                    } 
                } 
            } 
                     
        } 
    } 
     
  
     
    imap_close($mbox); 
    
     
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.