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.

6 Replies to “BASH Script to Fix Dovecot Message Dates”

  1. 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.


    1. Sorry I’m wrong, they are now in the correct order. I had to modify your script a little, but it’s gonna be one that’ll be handy for my migrations!

      Again, thank you so much.

  2. 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.*

    1. That will only work for Linux and other *NIX but not for OS X.

      OS X’s date or touch command does not have the -d option to process dates from char-strings. You will have to prune the weekday (Mon~Sun) and convert the UTC offset through a script to the machine’s timezone, then pipe it to touch – just as in Steve’s script.

      Why doesn’t OS X have this awesome -d option? I think it’s because OS X supports multi-byte characters in standard input but many date string manipulation commands
      do not.

      So that brings us to the obvious caveat for Steve’s script. You have to be working in US English or the month names and date format will not match.

      This comment is 22 months late to the party… For people looking for OS X answers, just FYI.

Leave a Reply

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