PHP READ POP3 Attachments

  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

My Google searches are being useless. I guess there are just way more people wanting to know how to SEND/Attach a file to an email with PHP rather than READ/Parse.

I have an xml that is emailed to me daily and I need to check my POP account, get the contents of that xml and parse it.

I've already connected to the account and selectively choose the email today and I can display it on the screen but I get the following:
Code: [ Select ]

// message number
// from
// subject
// date
// message

3
sender@email.com
Daily XML
2010-06-29


----boundary_0_6dac5031-aa36-400d-85d4-0ebb1bd74323
Content-Type: text/plain; charset=us-ascii
Content-Transfer-Encoding: quoted-printable

The attached file was automatically generated by Stewie
----boundary_0_6dac5031-aa36-400d-85d4-0ebb1bd74323
Content-Type: application/octet-stream; name=20100629-1052.xml
Content-Transfer-Encoding: base64Content-Disposition: attachment(here is the base64 attachment it looks like : attachmentPD94DWwj0iMs....)==----boundary_0_6dac5031-aa36-400d-85d4-0ebb1bd74323--
  1. // message number
  2. // from
  3. // subject
  4. // date
  5. // message
  6. 3
  7. sender@email.com
  8. Daily XML
  9. 2010-06-29
  10. ----boundary_0_6dac5031-aa36-400d-85d4-0ebb1bd74323
  11. Content-Type: text/plain; charset=us-ascii
  12. Content-Transfer-Encoding: quoted-printable
  13. The attached file was automatically generated by Stewie
  14. ----boundary_0_6dac5031-aa36-400d-85d4-0ebb1bd74323
  15. Content-Type: application/octet-stream; name=20100629-1052.xml
  16. Content-Transfer-Encoding: base64Content-Disposition: attachment(here is the base64 attachment it looks like : attachmentPD94DWwj0iMs....)==----boundary_0_6dac5031-aa36-400d-85d4-0ebb1bd74323--


I left out the base64 code but it follows "attachment".

There's got to be an easy way to grab the contents of the xml file with php. I guess my heads just to wrapped around text manipulation cause I keep wanting to search strings and break apart the Content-Type to get the xml filename or just the base64 data and then obstart it or save it to a temp location on the server. It seems I should be able to just base64decode the data and have my xml text, right? I also wouldn't mind just saving the file to my server that my script is running on, but I won't be monitoring my script. I know if I throw the headers I can prompt to save but I won't be able to click OK. I need it to just automatically save. Actually, once the xml is saved on the server I can then just open and read it with my script and parse it. So, knowing how to just save the file to the server should also get me fixed up even though I'd like to do it all in one script,,, connect,read,open,parse instead of connect,read,save and then open,parse. Thanks in advance.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

I know this doesn't exactly answer your question because you are wanting to retrieve an e-mail via PHP using POP3, and then parse it, but have you ever thought about just setting up some sort of e-mail filter server side and have that piped through a program that will automatically parse the XML file?

I am not sure where you are hosted, or if you use CPanel, but it would be very easy to do with CPanel to setup a filter so that an email that contains a certain subject, comes from a certain person, etc., automatically get piped to whatever script you have written. If you go that route you can completely forget about doing the POP3 stuff.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

Actually, that sounds like perfect and probably more secure. Unfortunately, I'm not familiar with that and I'm not in control of the host and domain. The best I could work out with the network administrator here is an account with pop access. It has a webmail client something called WorldClient for MDaemon. It's actually got a ok looking outlook interface and I noticed I can "Pull out attachments and store them in account's FILES folder" but I wouldn't know how to access it. Maybe it just gives it a URL like a google doc. Also, I can't connect using https or ssl:// in my fsocketopen script so that kinda concerns me. I can only use port 110. Your way would be so perfect cause I also have to loop through the messages and filter the subject line and the sender to get the email I want which wasn't a big deal but it's already done. Lastly, my script is on my local machine and not accessible from outside.
  • Bigwebmaster
  • Site Admin
  • Site Admin
  • User avatar
  • Posts: 9089
  • Loc: Seattle, WA & Phoenix, AZ

Post 3+ Months Ago

What about something like this:

http://pecl.php.net/package/POP3

You can see some sample code using it here:

http://svn.php.net/viewvc/pecl/pop3/tru ... iew=markup
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

That's exactly what I need to get installed. I'll post back my results with the pecl package.
  • devilwood
  • Silver Member
  • Silver Member
  • User avatar
  • Posts: 436

Post 3+ Months Ago

So, I got the pecl ext installed and ran the example code and their test code is all working good.

Then, I started looking for the additional functions for that pecl package and I can't seem to find any documentation. It looks like the example php file they gave incorporates all their functions?? I need to do more than connect, count msgs, get msg IDs, get msg size, get msg headers, get message body, delete, undelete.

I could already do all that with just php by itself. So, I'm at the same place I was before. Basically, I get the same information printed to the screen just with the pecl package I get a ton more information about the email which will actually make it harder to string manipulate the content to find subject,dates,etc. I figured these functions were written into the package, but I need documentation to know how to call them. I really only need the subject line and date of the email. Then, if that matches I then open the attached file into memory with ob_get_contents or something. But like I said my script can just save it to my TMP/ folder and then work with it from there if need be.

Anyway, I searched high and low for more usage of the pecl pop3 package and can't find anything except those examples. It's weird they'd create a whole extension to php but only provide functions that can be done with a simple class. Sorry, I thought it would be more encompassing. I'd like to use it but I need more docs on it.

Post Information

  • Total Posts in this topic: 6 posts
  • Users browsing this forum: No registered users and 143 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.