Vertigo / Group

Previous
Next

Apple Mail: Incorrect Received Date

Recently, we upgraded one of our internal Mac servers from 10.5.8 to Snow Leopard. While on the surface, this upgrade seemed to have successfully completed, users started experiencing strange issues with mail items; specifically, with the Apple Mail client and messages showing a received date of 11/26/2020.

Our stellar IT staff investigated this, and determined that it somewhere during the upgrade our mail store became corrupt. This corruption specifically happened when Snow Leopard began upgrading mail to the dovecot system. Our engineers contacted Apple, but unfortunately, they were unable to assist and needed to escalate our trouble ticket to advanced server support with a turnaround time of three to four business days. Completely unacceptable in my book, but luckily, one our engineers was able to find a great post in the Apple Support Discussion Forums.

In the interest of assisting others with this possible problem, I have posted what our engineers deemed the best method for correcting the issue. Please note, this solution differs slightly from the support discussion page as we have tens of thousands of email messages in each subfolder; therefore, there is an extra command that needs to be run to avoid the dreaded “Argument list too long” error message from the terminal.

Follow the steps below in order:

  1. Download and install xcode onto the server in question
  2. From the terminal:
    perl -MCPAN -e shell
    install File::Touch
    exit
  3. Stop mail service
  4. Save the following script to a file titled “maildatefix.pl”
    #!/usr/bin/perluse strict;
    use warnings;
    use MIME::Parser;
    use MIME::Entity;
    use MIME::Body;
    use Date::Parse;
    use File::Touch;
    if( !@ARGV )
    {
    die( “No arguments provided.\n” );
    }

    if( !-d( “/var/tmp/set_date” ) )
    {
    system( “mkdir /var/tmp/set_date” );
    }

    foreach my $arg ( @ARGV )
    {
    if(!-e $arg || !-f $arg)
    {
    print( STDERR “File $arg not found or not a file\n”);
    next;
    }
    process( $arg );
    }

    sub process
    {
    my $file = shift @_;
    print “Processing $file “;
    my $parser = new MIME::Parser;
    $parser->output_under(“/var/tmp”);
    $parser->output_prefix(“msg”);
    $parser->extract_nested_messages(0);
    $parser->decode_bodies(0);
    my $entity = $parser->parse_open( $file );
    my $header = $entity->head;
    my $date = $header->get(‘Date’);
    chomp($date);
    print(“with date $date… “);
    my $time = str2time($date);
    my $touch = File::Touch->new(mtime => $time, no_create => 1);
    if( $touch->touch( $file ) )
    {
    print( “ok\n” );
    }
    else
    {
    print( “failed.\n” );
    }
    }

    exit(0);

  5. Save the aforementioned file on the server to /bin/
  6. chmod 775 /bin/maildatefix.pl
  7. cd /var/spool/imap/dovecot/mail/USERS MAIL DIR/cur/
  8. sudo maildatefix.pl *
  9. cd ..
    sudo rm dovecot.index*
    sudo rm subscri*
  10. cd .Sent\ Items/cur/
    repeat steps 7 and 8 for each mailbox in the directory (.Sent Items, .Drafts, etc…)
  11. Repeat steps 6 through 9 for all mail users
  12. Start Mail services

Please note: iPhone users may need to remove and re-add mail accounts to clear the cache of bad dates.

Now, in the event that you receive the following error in terminal when executing the maildatefix.pl commend “Argument list too long,” please modify your statement to this [find . -name '*' -print0 | sudo xargs -0 maidatefix.pl] without the brackets.