Courier IMAP and auto deleting trash after 7 days

E-mails that had been deleted for over 7 days were automatically removed from the IMAP server. E-mail date was ignored (ie the mail could have been from 2010; the actual time in “Trash” counted). This didn’t happen to other folders (Sent, Archive, Spam). This recently happened and hadn’t happened before.

I had to restore my trash folder from backups every 7 days (yay for rdiff-backup).

It took me a while to figure it out… The problem first appeared in October, right after several big changes:

  • Yosemite update
  • Airmail to Airmail 2 update (I was convinced this was the root cause, looking at my clients instead of the server)
  • Android 5.0.x
  • Android Email app sunset, changes to GMail app
  • IPv6 working decently at home after updating my RPi to Fritzbox devices.
  • Random connection errors in GMail app (this was due to misconfigured DNS in the GMail app and causing IPv6 catch all to redirect to the webserver instead of the mailserver. It didn’t happen consistently because over mobile (4G & lower) there is no IPv6 and at home is randomly falls back to IPv4 as well. IPv4 DNS was well configured.
  • Moving my ~100.000 deleted e-mails from “Deleted Items” (OS X Mail default) to “Trash” (Android & Courier default) to stop having to move them manually from one folder to the other every so often. => this was eventually the cause but I didn’t realize.

Being convinced it was most likely Airmail 2 and very maybe Android I had been looking in that direction.

Debugging was also extremely slow as I had to wait 7 days before being able to check if the changes I made helped anything.

I eventually figured out that it was not Airmail when I rebuild my whole mail database and it defaulted back to putting my deleted mails into the Archive folder instead of Trash. Archive mails were kept over 7 days, but items in Trash still removed.

That’s when I started looking at Courier IMAP config: /etc/courier/imapd (and not imapd-ssl).

There’s an option that says:

##NAME: IMAP_EMPTYTRASH:0
#
# The following setting is optional, and causes messages from the given
# folder to be automatically deleted after the given number of days.
# IMAP_EMPTYTRASH is a comma-separated list of folder:days.  The default
# setting, below, purges 7 day old messages from the Trash folder.
# Another useful setting would be:
#  
# IMAP_EMPTYTRASH=Trash:7,Sent:30
#
# This would also delete messages from the Sent folder (presumably copies
# of sent mail) after 30 days.  This is a global setting that is applied to
# every mail account, and is probably useful in a controlled, corporate
# environment.
#
# Important: the purging is controlled by CTIME, not MTIME (the file time
# as shown by ls).  It is perfectly ordinary to see stuff in Trash that's
# a year old.  That's the file modification time, MTIME, that's displayed.
# This is generally when the message was originally delivered to this
# mailbox.  Purging is controlled by a different timestamp, CTIME, which is
# changed when the file is moved to the Trash folder (and at other times too).
#
# You might want to disable this setting in certain situations - it results
# in a stat() of every file in each folder, at login and logout.
#
IMAP_EMPTYTRASH=Trash:7

Comment out that last line, and restart courier-imap(-ssl)… Simple as that.

This solved my issue.

I’m not sure when that config change happened (Debian update?) and I do not know who at Courier thought it was a good idea …. But sheesh.

Gmail spam “mark-as-read”

Google changed their Mailbox names (from Google Mail, to Gmail) so here is the update from my previous php script I made a few years ago.

Edit: Gmail reverted back to the previous version (imap folders were “Google Mail”, changed to “Gmail” yesterday and today). To version v0.03 won’t work anymore. I’m guessing this was a temporary change to implement their exchange/push-support.

Imap/Gmail mark as read

I’ve “updated” my previous php-app.

Since today I ended up getting this error:

Warning: imap_open(): Couldn't open stream {imap.gmail.com:993/imap/ssl}[Google Mail]/Spam in /home/yeri/.gmail.php on line 30
Certificate failure for imap.gmail.com: unable to get local issuer certificate: /C=US/ST=California/L=Mountain View/O=Google Inc/CN=imap.gmail.com - Connection failed.

Simply searching this line:

$open = imap_open ("{imap.gmail.com:993/imap/ssl/}$path"

And editing it to:

$open = imap_open ("{imap.gmail.com:993/imap/ssl/novalidate-cert}$path"

Solves the problem.

This will ignore the Google certificate. Please do double check that imap.google.com really points to Google, and it’s not some kind of DNS issue.

I’ve updated the 1st version, that ignores the certificate.

Gmail automatic “mark (spam) as read”

I hate having unread mail. I hate having spam. And most of all, I hate spam that asks for attention.

When receiving spam in Gmail, you get the annoying bold Spam (12353434) with ‘12353434’ being the number of unread spam messages.

With the introduction of Gmail Labs I’d hope to see an “automaticly mark spam message as seen” feature, which, well, I haven’t found yet.

Being tired of all these increasing spam numbers, and my daily efforts to “select all unread + mark as read”, I ended up writing a small PHP file which connects to the Gmail (using IMAP, so I asume you’ll need IMAP enabled in your settings..) and marks all spam messages as read. Running this script locally with a curl or cron every X time will solve most of your problems, I hope.

Download the file here (phps) or here (txt).

Or just copy paste it!

< ?php
/*
Gmail "spam mark-as-read"
 
by Tuinslak
www.tuinslak.be
 
v0.01 :: 26/08/2008 . init php codez
*/
 
// Config starts here
 
// Gmail user &amp; pass
$user = "YOU";
$pass = "hiddensecretz";
 
// Debugging - Outputs a list of mailboxes and status on the imap server (1/0)
$listmailbox = 0;
 
// Advanced config :o
 
// "Path" of the (spam) mailbox and/or its name.
// default should be ok with Gmail.
$path = "[Google Mail]/Spam";
 
// End of config
 
$open = imap_open ("{imap.gmail.com:993/imap/ssl}$path", $user, $pass, "", 1) or die(imap_last_error() . "
 
Connection failed.");
 
// debug
if($listmailbox) {
        echo "<strong>Mailboxes:";
        $folders = imap_listmailbox($open,  "{  Gmail  }", "*");
 
        if ($folders == false) {
                echo "Call failed\n";
        } else {
                foreach ($folders as $val) {
                        echo $val . "\n";
                }
        }
 
        echo "<strong>Status</strong>:";
        $status = imap_status($open, "{imap.gmail.com}$path", SA_ALL);
        if ($status) {
                echo "Messages:   " . $status-&gt;messages    . "\n";
                echo "Recent:     " . $status-&gt;recent      . "\n";
                echo "Unseen:     " . $status-&gt;unseen      . "\n";
                echo "UIDnext:    " . $status-&gt;uidnext     . "\n";
                echo "UIDvalidity:" . $status-&gt;uidvalidity . "\n";
        } else {
                echo "imap_status failed: " . imap_last_error() . "\n";
        }
}
 
// Mark as read
$search = imap_search($open, 'UNSEEN');
// print out the array containing $search info
//print_r($search);
 
for ($i = 0; $i &lt; sizeof($search); $i++) {
        $read = imap_setflag_full($open, $search[$i], '\\Seen');
}
 
// and close it down !
imap_close($open);
 
// EOF
?>

If you plan to execute it from shell, don’t forget to add a shebang “#!/usr/bin/php” on line 1 and chmod +x gmail_spamreader.php.

Edit: Please add a comment if you’re using it, just for statistics. 🙂