Download POP3/IMAP Email Attachments with PHP

I found this niftly little class on phpclasses.org. Basically, it logs in to an email server and downloads all email attachments and saves them as files. That’s basically it, no more, no less.

This was exactly what I needed though. I wanted to run some daily reports in Google AdWords and Google AdSense, to a specific email address, then process those reports on a daily basis to customize my own reports… in a nutshell.

I did make a few updates to this class.

  1. The $savedirpath variable was not utilized. It simply saved the files in the same location as the script. I had it implement this parameter.
  2. I added another parameter to delete emails after downloading the attachments. Luckily for me, when I went to add the code, I found it was already there, just commented out.
  3. Tidied up the code a bit to make it more readable for me.

You can download the original files here and get my updates here.

Related posts:

  1. One Click Django Project Publishing I was reading The Joel Test and I got to...
  2. Inconsistent Bugs and Hard Coded Variables Part of the joys of being a one man development...

Related posts brought to you by Yet Another Related Posts Plugin.

December 18, 2008   Posted in: Programming & Internet

17 Responses

  1. PHPer - December 21, 2008

    Dustin,

    The link to the modified class you created returns an empty file… :(

    Please fix.

  2. chamosan - January 2, 2009

    does this class works with ATT mobile phones – MMS attachments?

  3. szotto - March 12, 2009

    it sounds interesting, just please rename the file extension to txt or compress it. the download link gives empty page because the fileextension is (.php) The webserver is parsing the php file and put out the results. there is no way to download it until make the change.

  4. Dustin - March 12, 2009

    Sorry ’bout that. I’ve zipped & fixed the link.

  5. Self Automated - March 17, 2009

    Hi,

    Great script. I followed the trackback from PHP classes to this page. I have tried this with every attachment, and it seems that .wav and .pdf do not work. The files save, but when you open the files, they are corrupt.

    Any suggestions?

  6. Juan Antonio - April 28, 2009

    Hi I was testing the class, but I couldnt connect:

    With right parameter, I receive the following message:
    can’t connect: Certificate failure for domain.com: self signed certificate: /C=US/ST=NY/L=New York/O=Courier Mail Server/OU=Automatically-generated POP3 SSL key/CN=localhost/emailAddress=postmaster@example.com

    what is the problem? I suppose that the problem is with the host parameter:

    $host=”{domain.com:110/pop3}”;

    I would like to receive a replay, please. tsp44@hotmail.com

  7. Juan Antonio - April 28, 2009

    Ok, team,

    finally, I got the solution. My problem was the connectionString :D

    in my case:
    $server = “mail.domain.com”;
    $host= “{” . $server . “:143/notls}INBOX”;

    I used the following URLs:
    http://uk2.php.net/imap_open
    http://www.linuxscope.net/articles/mailAttachmentsPHP.html

    cheers

  8. Kareem - June 15, 2009

    I’ve been trying to get this to work, but all it does is delete the file from the gmail inbox.

    Here’s my code:

    function getEmailFile($directory) {
    $attachment = new ReadAttachment;
    $attachment->getdata(‘{imap.gmail.com:993/imap/ssl/novalidate-cert}INBOX’, ‘address@gmail.com’, ‘password’, ‘xml/’.$directory, ‘false’);
    }

    getEmailFile(‘visitors’);

    Any help would be greatly appreciated!

  9. larry - July 6, 2009

    Any body! I use Kodak EasyShare software, burning CD/DVD without any problem for a long time. Now, when I try to do the same operatin I get message:’IMAP12 is not installed on this machine” and cant do anything father. What is IMAP12 has to do with this????

  10. Anthony - July 8, 2009

    This could be very useful for anyone who gets sent a ton of attachments and needs to have them handy down the road.

  11. Henrique - September 28, 2009

    Kareem, did you ever figure out what your problem was? It’s happening the same thing here.
    First I thought the permissions to the directory weren’t right, until I chmod a+rwx on it and it still won’t work.
    Any ideas?

  12. Henrique - September 29, 2009

    Finally… After all this time, a little tweaks saved the day.

    For some reason the $parts weren’t being read correctly, saying all emails had 0 parts. I then removed FT_UID from the imap_fetchstructure line, leaving it like this:

    $structure = imap_fetchstructure($mbox, $jk);

    but then $part->disposition wasn’t reading “attachment” for attachments. I print_r the array and found out the attachments disposition were reading as “inline”. I have no idea of all of this means, but I changed the following line from:

    if($part->disposition == “ATTACHMENT”) {

    to

    if($part->disposition == “inline”) {

    and it’s working perfectly now.

    Anyway, if anyone is having the same problem I was, try changing those lines and you should be all set.

  13. P2M - October 24, 2009

    @Henrique

    yess I have the same and make your change and it work fine now !!

  14. heppu - November 5, 2009

    @Henrique’s great tip doesn’t work for me. No image saved (folder ftp –> 777). Any other solution?

    For my email this code mark’s email as read, and delete work’s fine, if delete_emails=”true”..

  15. Seth Sym - November 14, 2009

    This is exactly what I needed; my office sends files back and forth (inefficient i know) and this will save some time for sure.

  16. Jeff - November 27, 2009

    I ready across the code also and am trying to run it but get these errors over and over as it reads email from my inbox.

    Notice: Undefined property: stdClass::$parts in /usr/local/websites/test/mobile-reader/attachmentread.class.php on line 50

    Notice: Undefined property: stdClass::$disposition in /usr/local/websites/test/mobile-reader/attachmentread.class.php on line 57

  17. chris from smsdam - December 14, 2009

    Great script and fix too. I already have incoming mail attachments neatly stored in a directory . Writing code to use them as the need arises.

Comments are closed for this entry.