ATTENTION: WiBit.Net will be temporarily taken offline for routine maintenance on 12/9/2016.
We apologize for any inconvenience.
WiBit.Net Blog (43)

 Clean Your IMAP Folders with PHP

Thu Aug 21, 2014 0 Comments 2279 views 3 Likes
kevin

Clean Your IMAP Folders with PHP

Recently I received a message from Gmail stating that my personal account was about to exceed the 40 GB of space I have!

I had no idea that my emails were consuming so much space! I started checking around and found the problem. I have security cameras at my home and they spit out alert emails with images every time movement is detected. The emails actually go to a different email account that my wife and I share, however, the outgoing emails are configured to go through my personal email account. This means that my Sent folder is filled with years of images from my security cameras!!!

Ahh, so now I need to do some sent box cleaning! I didn’t feel like doing it manually, because who has time for that? I mean, seriously, I have these alert emails going back to 2010, so it would have taken me an unimaginable amount of time cleaning these out.

I decided to write a program to do it, and I chose to use PHP. I have used the IMAP Extension for PHP in other projects before, and I simply found it easy to use, and plus it’s free!

The program is quite simple. All I need to do is search my inbox for the subject line that I need to remove, and then delete the entries.

You may need to install the IMAP Extension on your machine first. I was working on an Ubuntu machine, so it can be installed using apt-get:

sudo apt-get install php5-imap

Next, we need to become familiar with a few PHP functions:


imap_open

Opens an IMAP stream and returns the resource which can be used for subsequent calls throughout the remainder of the session. As you can see (in the code below), I am opening my Gmail ‘Sent Mail’ folder, since this is what I wanted to clean out.

imap_search

Searches an IMAP mailbox. There are numerous ways to use this function (check the documentation for more details). The example I am using is simply searching for text in the subject line that I know is from my security cameras. This results an array of sequence numbers (the order the email appears in the mailbox).

imap_delete

Marks a message as ‘deleted’ based on the sequence number passed in. If you want to, you can send an option (FT_UID) which treats the message sequence number as the message Unique ID. In my example, I am simply deleting based on the message sequence number.

imap_expunge

This function ensures that messages marked as deleted are removed. This can be called at the conclusion of the session.

imap_close

Closes the IMAP stream.


Here is a simple program that will do what I need:

<?php
  // Connect to Gmail IMAP stream
  // and open the 'Sent Mail' folder
  $mbox = imap_open(
    "{imap.gmail.com:993/imap/ssl}[Gmail]/Sent Mail",
    "[email protected]",
    'P@$$w0R6'
  );
  // Search my emails.
  // Result email sequence numbers for messages
  // with a subject containing the string
  // "motion alarm at"
  $emails = imap_search(
    $mbox,
    'SUBJECT "motion alarm at"'
  );
  // Reverse sort the array, so the emails
  // are listed from newest to oldest
  rsort($emails);
  // Iterate through the email sequence numbers
  foreach($emails as $email_number) {
    // Mark the email as deleted
    imap_delete($mbox, $email_number);
  }
  // Make sure the emails marked as deleted
  // are removed
  imap_expunge ($mbox);
  // Close the IMAP stream
  imap_close($mbox);
?>

Remember, you don’t need a web server to run PHP. You can run this from the command line:

>php remove_security_cam_alert_emails.php

WiBit.Net has, like, totally gone Metro!

It was a LONG time in the making, BUT FINALLY, WiBit.Net is proud to announce the release of a series of Windows Apps for your Windows 8.1 devices!

Adding Custom Fonts To Your Website

Not long ago web developers were stuck using a specific subset of universal fonts on their websites. But times have changed!