[Webinar] Learn how to a build a cloud-first strategyRegister Now

  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 719
  • Last Modified:

PHP script to read e-mail attachment after POP3 login

So many questions asked about how to upload/attach a file to PHP's mailer process, sure no problem there.

Here's something on a different level I am trying to do.

1: PHP script that checks a specific e-mail account (via POP3)

2: After reading all of the data, I need PHP to read/import any file attachments based on an extension filter (ONLY import if they are *.gif, *.jpg, etc)

3: Save attachment to a folder (create if necessary) based on the sender's email address.

Most of this I have the answer for.  A bit unclear on making a POP3 connection with PHP, but am sure I can figure that out... But how would I go about step 2, actually importing the body of the message, and grabbing any attachments?  I need to accomodate both inline HTML images, and actual attachments.
  • 2
1 Solution
To access the POP mailbox, I would user the IMAP extensions in PHP. There are not well named but all the same, the extension does support POP3.
To 'read' the email, I would use the PEAR mime_Decode routine. Or you can the use the routines fromthe IMAP extension. The latter will be faster actually.
Once the email has been 'read' you need to traverse the tree of MIME content and look for any MIME parts of type "image/xxxx" (use a PCRE, or match on a subset of image types that you want to accept).
You can get the sender email address from the MIME headers as well.

By the way, you can easily by-pass the POP3 step if you happen to have a server which also offers sendmail. You can use the virtusertable to set up all mail to a certain email address to be forwarded directly to a PHP script, using the syntax "|/usr/local/bin/captureemail.php" for example. This would be simpler to code, if you have the ability to access the mail in that way.

You might also want to have a look at the 'fakemail' project on sourceforge, http://sf.net/projects/fakemail

Hope that helps

purpleonyxAuthor Commented:
Thanks JP, got everything working how I wanted it to.  I found all of the IMAP_* commands to use, and just parsed the remainder out, for plain-text and image/*.  Got it to auto create directories based on email address and save the attachments to that newly created directory.

As for the forward script, the problem is the mail server (Qmail) is on a separate machine, and it does not want to use dot-forward to send to another machine unless there is some sort of directory sharing, which I don't want.  Feel free to correct me if my understanding of how this works is incorrect.

Much appreciated!
You're welcome. Sounds like you've come up with a good solution

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now