sloth456 Posted December 15, 2008 Share Posted December 15, 2008 I've searched the forums and google and almost get the answer but then not quite. There is a particular email I'm downloading and it has a zip file attached to it. I've managed to extract the attachment which just comes out as a long string base64 encoded. I've used decode_base64 and get another wierd looking string, presumably this is the actual ZIP. Now it seems I'm unable to read this string using zip_read(), I presumed the string I provided it was the resource but not so, just an error saying it's that it was expecting a resource. Can anyone help? Have I gone wrong further down the line? Quote Link to comment https://forums.phpfreaks.com/topic/137061-solved-decoding-my-email-attachment/ Share on other sites More sharing options...
sloth456 Posted December 18, 2008 Author Share Posted December 18, 2008 ok, managed to solve this one on my own. For those of you who don't know anything about email attachments I'll start from the beginning. The attachment doesn't come in physical form. It comes as a piece of text (a string to be more technical), which must then be decoded to make it into the actual file. The string can be accessed with the follwoing line imap_fetchbody($conn, $emailid, 2); where $conn is the connection you made earlier and $emailid is the email number you're accessing. The number '2' is the "part" that the attachment is in. I've deliberately set it to '2' because I know that part 2 of my email contains the attachment I want. If you have multiple attachments, you'll have to loop through each part number. If you were to echo this, you'd probably get a long string of characters. This is your attachment coded into a piece of text. To decode it you'll need to know how it was encoded. For me I knew my "piece of text" was encoded in base64. So I just used base64_decode($thecodedtextgoeshere); to decode it. If you want this in a downloadable form you'll then need to use fopen() and fwrite() to write this file to your server/hosted area. This is what I did: //grab the attachment (which is just some text at the moment) decode it and stick it into $file $file = base64_decode(imap_fetchbody($conn, $emailid, 2)); //the file may be decoded but its still in text form, so write that text into a physical file on your server and you can open it! $fp = fopen('xml-report.zip', 'w'); fwrite($fp,$file); fclose($fp); Your file should now be located on your server. I knew my file was a zip file so I created a blank zip folder on my server called 'xml-report.zip' and wrote the string to that. For my purposes I also needed to extract the zip on the server so I could read the contents of one of the files inside. $zip = zip_open("xml-report.zip"); if ($zip) { while ($zip_entry = zip_read($zip)) { $fp = fopen("extracted/".zip_entry_name($zip_entry), "w"); if (zip_entry_open($zip, $zip_entry, "r")) { $buf = zip_entry_read($zip_entry, zip_entry_filesize($zip_entry)); fwrite($fp,"$buf"); zip_entry_close($zip_entry); fclose($fp); } } woop! zip extracted and contents put into a folder called "extracted/" Now you can just use fopen and fread again if you want to actually parse out some data from one of those documents. Hope this helps, I notice a lot of people having problems getting attachments from emails. Quote Link to comment https://forums.phpfreaks.com/topic/137061-solved-decoding-my-email-attachment/#findComment-718882 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.