Is it safe to move IMAP message files around when using dovecot

dovecotemail

I'm running postfix + dovecot, and all IMAP messages are in /var/vmail/DOMAIN/USER. I'd like to make a cron script that will look for messages for all users in a folder called 'False Positives,' sa-learn them as ham, and then move them into the inbox.

The question here is, is it safe to just mv one of these message files around? Or must it be done through some "official" means so the IMAP server is aware of the move?

Best Answer

Dovecot detects mailbox changes and rebuilds indexes automatically. Instead of 'mv' however, you could use dovecot's doveadm command and save the unnecessary index rebuilds.

Something like this should work:

doveadm search -A mailbox 'False Positives' 2>/dev/null | while read user guid uid; do   doveadm fetch -u $user text mailbox-guid $guid uid $uid | sa-learn --ham ; doveadm move -u $user INBOX mailbox-guid $guid uid $uid ; done
Learned tokens from 1 message(s) (1 message(s) examined)
Learned tokens from 1 message(s) (1 message(s) examined)
Learned tokens from 1 message(s) (1 message(s) examined)

Explanation:

  • doveadm search -A mailbox 'False Positives' -> returns user/guid/uid of all messages in a "False Positives" Folder
  • doveadm fetch -u $user text mailbox-guid $guid uid $uid | sa-learn --ham -> prints out the full message (header & body) and pipes it to sa-learn
  • doveadm move -u $user INBOX mailbox-guid $guid uid $uid -> moves the message to INBOX