TheDotProduct

Web-tech nerd stuff

Quick how-to: (Sub)Domain forwarding Email in Qmail (with virtual domain support)

As part of an ongoing Server move, today I needed to find a way to divert/forward all incoming Email from one Server (Server A) to another Server (Server B). Server A is running Qmail (on Debian etch) with virtual domains as part of a Plesk 8 install (alongside Courier). Server B is running my preferred setup of Postfix and Dovecot.

After a bit of searching, I found a good article describing “forwarding Email to another host” using Qmail. I tried, as the article recommends setting up a Qmail config file (which didn’t exist on my server):

/var/qmail/control/smtproutes

Containing a Qmail Email domain forwarding rule e.g.:

sourcedomain.com:destinationdomain.com

Unfortunately after restarting Qmail there was no change.

So I then thought a little further and realised that as part of Plesk 8, Qmail was being run with virtual domain support (i.e. Email accounts are stored in a Database rather than as system accounts/files) and spotted a Qmail config file:

/var/qmail/control/virtualdomains

This config file contained the name of the domain I was trying to forward followed by a colon and a number, I assumed it was likely that the number was some sort of ID. So, I tried removing the line containing the domain I was trying to forward, restarted Qmail and bingo! Email forwarding was working in Qmail…Nice!

So, as a brief recap, the process is:

  1. As always: Back up all your current, working config files and if at all possible, do this using a test domain or sub-domain name so if you make mistakes you won’t be killing peoples email!
  2. Set up your destination Email Server to receive Email for the relevant (sub)domain and Email account(s).
  3. Ensure that the domain name you are trying to forward Email for is in the Qmail config file /var/qmail/control/rcpthosts (this ensures that Qmail will handle Email for the domain).
  4. If necessary create the Qmail config file /var/qmail/control/smtproutes (this file did not exist on my server).
  5. Add a forwarding rule to /var/qmail/control/smtproutes e.g.
    sourcedomain.com:destinationdomain.com

    Note that you can use sub-domains if you want to e.g.

    mail.sourcedomain.com:newmail.destinationdomain.com

    or you can forward to an IP address e.g.

    sourcedomain.com:1.2.3.4

    If you want to forward all Email for all domains handled by your Qmail Server, you can use a wildcard config which means you omit the source domain name e.g.:

    :destinationdomain.com
    

    or if you want to forward all Email being handled by your Qmail Server:

    :1.2.3.4
  6. If your Qmail installation is running virtual domains (as was the case with my Plesk 8 install), remove the line containing the domain you are trying to forward to which would look something like this:
    yourdomain.com:52
    
  7. Now you should be finished with config so you can restart Qmail:
    /etc/init.d/qmail restart
  8. Check your destination Email Server which should now be receiving Email sent to Server A for the source domain.

This worked fine for me using (source server) Debian Etch/Plesk 8/Qmail and destination server Debian Lenny/Postfix with virtual domains. Please be sensible though, no one likes losing Email so proceed with a little caution.

I assume that config file locations may vary on different installs so the path to config files may be different on your Qmail server.

Recommended references:
TechRepublic’s article on Qmail by Vincent Danen
Life with Qmail (including a very handy guide to Qmail config files)

I hope this article is useful and works for you too!

Created: Sat, 22 May 2010 17:00:00 GMT
Last modified: Sat, 22 May 2010 17:00:00 GMT