Redirecting Email with Postfix and MySQL
Oct 2, 2015 #sysadminTL;DR: Skip to the sections titled The Actual Commands and Command Breakdown. The guide assumes you have completed the NSA-Proof your email guide.
I followed this tutorial on setting up an email server. Although some parts of the setup (especially the parts about spamd
) are outdated, it’s still a pretty good guide. In particular, it’s missing instructions on redirecting mail between accounts. This post is how you can do that.
Let’s say you have a myraid of addresses on example.com
. Stuff like this:
help@example.com
customerservice@example.com
admin@example.com
security@example.com
advertising@example.com
account@example.com
Now, checking all these addresses individually can be quite tedious, so let’s have all these emails to redirect to your personal account, you@example.com
.
Most information out there on Postfix wants you to use an alias hash in /etc/postfix/virtual
to redirect everything. We can’t do this though, because we’re using MySQL for our addresses and authentication. So we have to make redirect rules ourselves.
The Actual Commands
cam@box:~$ sudo -i
[sudo] password for cam:
root@box:~# mysql -p mailserver
Enter password:
# ... Several login lines omitted
mysql> select * from `virtual_domains`;
mysql> INSERT INTO `mailserver`.`virtual_aliases`
(`id`, `domain_id`, `source`, `destination`)
VALUES
('1', '1', 'help@example.com', 'you@example.com');
mysql> exit;
Command Breakdown
The MySQL query select * from `virtual_domains`
is simply used to figure out the domain id we’re redirecting from. You will use the id
value from this for your domain_id
in the final command.
The command parameters of the INSERT
are as follows:
- The
id
parameter is a unique for each entry yourvirtual_aliases
table. It should start at 1, then go to 2, then 3, and so on. - The
domain_id
is the id of the domain you are redirecting main from. For example, if you were redirecting mail fromroot@example.com
toyou@yourwebsite.tld
, then you would want to use theid
ofexample.com
. This can be found from the table printed by the first command. - The
source
value is pretty simple. It’s simply the address you want to redirect from, such ashelp@example.com
. - The
destination
value is pretty simple too. It’s where you want to redirect your mail. This doesn’t even have to be on a domain hosted by you!
Here’s another example of creating a redirect rule:
mysql> INSERT INTO `mailserver`.`virtual_aliases`
(`id`, `domain_id`, `source`, `destination`)
VALUES
('2', '1', 'admin@example.com', 'you@yourwebsite.test');
And that’s about it. Email me if you have issues. My info is on the about page.