PHP script to read e-mail attachment after POP3 login

Posted on 2005-05-10
Last Modified: 2012-05-05
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.
Question by:purpleonyx
    LVL 7

    Accepted 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,

    Hope that helps


    Author Comment

    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!
    LVL 7

    Expert Comment

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

    Featured Post

    Maximize Your Threat Intelligence Reporting

    Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

    Join & Write a Comment

    This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
    Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
    The viewer will learn how to dynamically set the form action using jQuery.
    The viewer will learn how to count occurrences of each item in an array.

    746 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now