PHP Script to trim IMAP Mailbox (e.g. trash)

I am lazy! Wait, that came out wrong. I like efficiency and automating things, and I am notorious for having scripts for anything and everything. Well, today, I have a new one to share with you.

I know most people these days use things like Gmail, but I am old fashioned and still like to own my own data, which is why I stick with IMAP. The problem with that, is that searching can get a little slow, or even time out on folders that might get a little too big.

For example, occasionally I’ll need to go and search for something in my “Trash” folder, but the search times out because of the thousands of messages I have there. The reality is, I only need messages going back about a month or so, if I only I could something that would automatically purge anything older than, say, 30 days…. I could not find anything, so I wrote my own PHP script, ready to run as cron job.

The script takes configuration parameters for:

  • IMAP server hostname (or IP) and port
  • Mailbox username and password
  • The folder of interest (e.g. Trash, Sent…etc)
  • Threshold for keeping messages newer than X days, which defaults to 30.

This is exactly what the script does:

  1. Connect to IMAP server
  2. Find the exact path of the target folder (e.g. Trash)
  3. Loop through all the messages in that folder
  4. Examine the date on each message, if older than threshold (e.g. 30 days) then delete, otherwise move on
  5. Purge deleted message (aka expunge in IMAP terminology) and disconnect

If you capture the output of the cron (e.g. have it emailed to you), then you’ll get a list of deleted messages and/or any errors encountered (if any).

View the PHP source code of the IMAP mailbox trimning cron script – Enjoy!

Wake-on-LAN is an easy, practical way to save on power

If you have ready my post on the new, power efficient power supplies for computers, you may start to notice a trend here! I have a desktop machine that, once upon a time, was doing quite a bit for me (web server, SVN repo, MySQL, PosgtreSQL, Samba shares, UPnP streaming, CUPS print server…etc), which required it to be on at all times. However, my needs have changed and these days I only need this machine turned on occasionally.

I had previously connected that machine to an LCD energy meter and figured it was probably costing me $5/month. Not too much, but not too little either. I wanted to find an easy, convenient way to turn this machine on and off (i.e. without having to physically push the power button). The turning off part was pretty easy, since I can do that over SSH or VNC, so it was just a matter of figuring out a way to turn it on remotely.

I started my search in the wrong direction, looking at power bars with wireless switches (Belkin makes one), or even Bluetooth connected power bars. All of which would be too expensive and an overkill for my simple purpose.

Eventually, a light bulb went on in my head! An old technology that I knew about for years, but never explored it, was the solution! It is called Wake-on-LAN (WOL) and it allows the network adapter, upon the reception of a “magical” trigger packet, to turn on the whole machine.

Sounds too good to be true? That’s what I thought, too. Then I Googled for examples of how to configure it and found an excellent guide at Life Hacker. It turned out to be even easier than I thought! In my case, all the configuration was perfectly lined up out of the box, so it worked on the first attempt without having to change anything.

As for where or what to use to send that magic packet, there are at least three very easy choices:

  • Your router, especially if you’re running an aftermarket firmware like DD-WRT or Tomato
  • A smartphone app
  • Websites offering it as a free service, although, you’d have to be OK with opening UDP ports 7 or 9 – or both if you don’t feel like searching!

I am surprised that I don’t hear much chatter about WOL in the IT circles. Personally, I think it is a great technology that can save a lot of energy and money, and it has been around for a while so most computers out there should have it built-in. It certainly has worked quite well for me!