BASH Script to Fix Dovecot Message Dates

Some time ago I was migrating our home email to a new email server. At one point the date was off, specifically the year on several message’s file stamps were “2020.”

This is going to present itself in a very particular way, mostly to iOS/OS X devices. A Dovecot IMAP server offers two ways to show messages to an email client, either using the date stamp from the email header itself or the date stamp in which the message file was written to the server and for this is uses the file system’s file date stamp. This is the option in an email client for Date Sent vs. Date Received. Apple devices prefer the latter.

The below script is a quick one I cobbled together to fix the date stamp on the message file. It will read though messages with my “2020” error, extract the sent date from within the message header and then update the timestamp on the file itself using that date. It will then delete the dovecot.index.cache for that mailbox to force a rebuild with the corrected dates.

It is a very use-specific script that solves a problem, but is a good example for someone that may have a similar issue and it can be modified as needed.

This entry was posted in Tech Trinkets. Bookmark the permalink.

5 Responses to BASH Script to Fix Dovecot Message Dates

  1. Tom Hughes says:

    I love you! Thank you so much for this script!

    1 question, how do I now make dovecot show the messages in the date order correctly?

    They’re currently shown out of order despite now showing the correct date.


  2. Kristian says:

    I just had the same issue and took a quick look on your script, but I ended up just solving it with the following two commands:
    for f in .*/cur/*; do touch -d “$(grep -m 1 “^Date:” “$f” | cut -f2- -d:)” — “$f” || echo “$f”; done
    rm .*/dovecot.index.*

Leave a Reply

Your email address will not be published. Required fields are marked *