Pushing / Forwarding Email from one server to another.

When transfering domains from one server to another, sometimes the emails get delievered to the old server after the DNS has been updated. When that happens, you can run the following script to forward the emails to the other server, or to a different email address.

 

#!/bin/sh

for i in /var/qmail/mailnames/<domain>/*
do
user=$(echo $i | cut -d'/' -f6)
for msg in $i/Maildir/{cur,new}/*
do
formail -n3 -s /usr/sbin/sendmail < $msg \ $user@<domain>
done
done

This will iterate through all the usernames in the domain directory of QMail, then look in the cur and new directory for message files and forward the emalis to $user@<domain>