Maildir – Resending emails via formail and procmail

I’m sharing this because someone might find it as being useful info…

Earlier today I had to resend some emails from a users Maildir folder to an external email account. The idea is to forward the existing email in a certain Maildir folder.

To do this I used formail and procmail.

I added some basic settings to the .procmailrc so that all filtered email will be forwarded to the email I want. I looks like this:

MAILDIR=$HOME/Maildir

:0
! user@example.com

From inside the folder (Maildir folder) that contains the email that I wanted to send, I executed the command:

grep -v ^Delivered-To FILE | formail -ds /usr/bin/procmail

The grep -v ^Delivered-To instead of a cat is done to remove the Delivered-To line in the begining of any line.

And what if there are thousands of emails in the folder?

for file in *; do grep -v ^Delivered-To $file | formail -ds /usr/bin/procmail; 
done

There are many tweaks that can be applied to this solution, I just didn’t need them.