Domain Email Forwarding

Hi

I’m a novice regarding the setup of Virtualmin but learning fast.

One issue we are trying to resolve is that we have two domains and want to forward all email received to one on to the other. If the destination domain is a catchall mailbox then I am able to do it but cannot work out how to forward a catchall on to the destination domain with individual mailboxes.

Obviously I could set up a mailbox or alias for each of the addresses in the first domain but that requires a lot of admin in some of our domains with a lot of users. We ideally want to be able to forward to a wildcard that then sorts out into the correct destination user mailbox.

If this is possible then any advice would be appreciated.

Many thanks

You can’t use a “catchall” (i.e. *) to deliver mail to individual users. You would need an alias for each one (unless the system weren’t configured for virtual mail hosting, but that’s not what I would recommend).

So, this is easy to do if the domain is an alias of the “main” domain (I seem to recall…though it’s been a very long time since I’ve looked at this area). But, if they are independent domains, it is a little trickier. I think you could probably use a post-creation script to automatically add the alias for the other domain…lemme experiment a bit.

Here’s the docs for that feature (and it’s found in the Webmin->System->Users and Groups->Module Config page under “Before an After Commands”. It’s just the name of a shell script, and you’d probably want a script to add aliases a “Command to Run After” (since you need the user to exist before adding an alias to it). And, you’d need a script to remove the alias before removing a user…so you’d need a second script for that, I think.

http://doxfer.webmin.com/Webmin/UsersAndGroups#Before_and_after_commands

And, you’d use the “virtualmin create-alias” script to make the alias.

Here’s an example of how to do that, which I’ve only tested briefly, but I think it’ll work, if you’re using Virtualmin defaults for user names (i.e. “user.domain” as the format, so in my example joe@docs.virtualmin.com would have a username of joe.docs):

#!/bin/bash

if [ “$USERADMIN_ACTION” = “CREATE_USER” ]; then
# Find the first part of the username (without .domain)
user="${USERADMIN_USER%%.*}"

# Is this in the domain we want to create the alias for?
if [ `echo $USERADMIN_USER | grep "docs"` ]; then
    # Create the alias
    /usr/sbin/virtualmin create-alias --domain doxfer.webmin.com --from $user --to $user@docs.virtualmin.com --desc "Autogenerated Alias"
    echo "Created alias from $user to $user@docs.virtualmin.com" >> /var/webmin/alias-maker.log
fi

fi

You’d need to replace the mentions of “docs” and “docs.virtualmin.com” with your “main” domain (the one that receives mail) and the mention of “doxfer.webmin.com” with the domain that needs aliases setup within it. If you needed this to work for more than one domain, you’d need to make it smarter. It would also be plausible to make a plugin for Virtualmin to do this, though it’d be a lot more code…but, could be a lot more flexible, and could add UI elements, if you needed to be able to easily setup more domains with this behavior.

I’ll leave the deletion script as an exercise for the reader. (I’m exhausted, and it’s after midnight…time to crash out for the night. If you need some help working out what the deletion script would look like, let me know…I can tackle it tomorrow, if this is all too much for you.)

There are other ways to go about it, I guess. Maybe a custom procmail rule (I believe Virtualmin can handle per-domain procmail rules in addition to per user procmail rules, but I might be imagining that). You could also make a procmail rule that is a part of the skel directory that gets copied to every new user when creating user in this particular domain that would forward mail to the same username in another domain. That’d be probably more complicated than this, and more prone to breakage (since there’d be a lot of files).

Also, it’d be pretty easy to make a script to generate aliases for existing users, using the list-users command virtualmin command, and then using the create-alias command as in the above script.

Cheers,
Joe