Deleting Junk messages from POP server

From MozillaZine Knowledge Base
Jump to navigationJump to search
The printable version is no longer supported and may have rendering errors. Please update your browser bookmarks and please use the default browser print function instead.

Since moving from OE, the main thing I have missed is the automatic deletion of messages from the POP3 server when I have deleted them locally -- I either only keep the last week's messages or I have to delete messages by hand, to stay below the mailbox's size-limit.

The following solution has worked well, but expects you to have a server (e.g. Apache) with php available on your computer, as well as the extension php_imap.dll (on Windows; this can be found by searching the web; there is an .so version for Linux systems.) The extension has to be loaded of course, which requires the removal of one ";" (semicolon) in php.ini.

While this may be a rather specific solution, I hope it will inspire other people to write a more general one. Hopefully that option from OE will eventually find its way into Thunderbird.

The script

<html>
<head>
</head>
<body>
<pre>
<?php
$server   = 'mail.your.server'; 
$username = 'username';
$password = 'password';
$junkfile = 'c:/path/to/folder/Junk';
set_time_limit(30);
echo "   Deleting messages marked as 'junk' from account $username on $server:\n\n";
$file = file_get_contents($junkfile);
$mbox = imap_open('{'.$server.':110/pop3}Inbox', $username, $password) OR 
     exit("   Error: unable to open online-Inbox.\n");
$numm = imap_num_msg($mbox);
echo "   There are $numm messages in the online-Inbox.\n\n";
$numj = preg_match_all("/X-UIDL/i", $file, $junk);
if ($numj == 0) {
  echo "   The folder $junkfile is empty.\n";
  imap_close($mbox);
  exit;
}
else {
  preg_match_all("/(?:Message-ID|Date): (.*)/i", $file, $junk);
}
$junk = join(' ', $junk[1]);
$done = 0;
for ($i = $numm; $i >= $numm-14; $i--) {
  $head = imap_fetchheader($mbox, $i);
  preg_match_all("/(?:Message-ID|Date): (.*)/i", $head, $rtrn);
  $rtrn = join(' ', $rtrn[1]);
  if (strpos(' '.$junk, $rtrn)) {
    $done++;
    if (preg_match("/From: (\".*\")/", $head, $from)) {
      imap_delete($mbox, $i);
      echo "   ... message deleted  From: $from[1] \n";
    }
    elseif (preg_match("/From: (.{0,40})/", $head, $from)) {
      imap_delete($mbox, $i);
      echo "   ... message deleted  From: $from[1] \n";
    }
    else {
      imap_delete($mbox, $i);
      echo "   ... message deleted:\n$head\n";
    }
    if ($done == $numj) {
      break;
    }
  }
}
$date = date('D, d M y H:i');
if ($done == 0) {
  echo "\n   Done. No messages were deleted.\n   ($date)\n";
}
else {
  imap_expunge($mbox);
  echo "\n   Done. $done message(s) was/were deleted.\n   ($date)\n";
}
imap_close($mbox);
?>
</pre>
</body>
</html>

How to use

To start,

  1. copy the above script into a file "deljunk.php" in a place where Apache can find it (-- note the one long line that should be read as one;)
  2. set the parameters in the first four lines of the php-section;
  3. set the Junk Mail Controls in Thunderbird to
    • "Move incoming messages determined to be junk mail to ..." and choose a folder, and
    • "When I manually mark messages as Junk: Move them to the "Junk" folder."

Then, whenever there are a number of messages in the Junk folder,

  1. move any messages that are not junk out of the Junk folder;
  2. compact the Junk folder (otherwise messages that have been moved out will also be deleted);
  3. while on-line, in your browser, run the "deljunk.php" script, like "localhost\path\to\deljunk.php": this will delete those of the 15 most recent messages on the server that are in the Junk folder;
  4. delete the mails from the Junk folder.

If the script regularly times out, it may be necessary to increase the number of seconds in the line "set_time_limit(30);"

(Needless to say, the script is provided as is, with no guarantees.)

Kai 17:10, 30 May 2005 (PDT)


One Alternative

Another lower-tech alternative to tricking Thunderbird into deleting Junk messages from a POP server is to use a Local Folders specific message filter. One of the actions available to message filters is "Delete from POP Server". Unfortunately, there's no "match all" conditional argument for filters, so I used "Status" "is" "Flagged", then flagged all messages in the Junk folder. After selecting the Junk local folder and choosing "Tools -> Run Filters on Folder", the next time I connect to the POP server(s), magically all of my junk mail is deleted.

ViperGeek 15:07, 14 Nov 2006 (EST)