Skip to content


How to simply create a catchall e-mail and forward all e-mails to another address in Postfix

I never managed to find a proper tutorial to do just that simple task: bind any e-mail program to all my domains I host on my server, do a big catchall over them all, and forward all this to my “real” webmail (used to be Gmail, now Yahoo! Mail). I’ll leave the painful path for the end and present straight away the working solution that I finally managed to figure out: indeed it seems that RTFMing was the best option in this case (but not anymore since I’m making this great tutorial ;)).

Installing Postfix

I’m not claiming any kind of proficiency with Postfix. Until today, I had only managed to set it up for sending e-mails. This part is really trivial: I just installed it (apt-get install postfix), during the installation configured it as “Internet Site” and left system mail name as default (=name of the server), and poof! just out of the box PHP can then use it to send e-mails already. Good enough for this part. (NB: at the end of this post I’ll put my full – but censored – config files, in case anyone finds them useful)

Actually configuring Postfix to receive and forward e-mails

This is actually quite quick (particularly compared to some HUGE tutorials such as this one, or even worse this one – doesn’t it just make you want to shoot yourself in the head? Seriously!), and can surprisingly be done by just reading of few pages of the manual (basic configuration to being with, but you actually don’t need to read most of it).

First step, you need to create a file containing your e-mail aliases:
nano /etc/postfix/virtual
Then in this file, you can add e-mail addresses in the form:

emailAddress emailWhereToForward1, emailWhereToForward2, emailWhereToForward3, etc

Where emailAddress can be an e-mail address such as example@example.com, but also a catchall such as @example.com.
My own file looks like:

patheticcockroach@myfirstdomain.com patheticcockroach@mywebmail.com
somenickname@myfirstdomain.com patheticcockroach@mywebmail.com
@myfirstdomain.com patheticcockroach@mywebmail.com
someothernickname@myseconddomain.com patheticcockroach@mywebmail.com
#@myseconddomain.com patheticcockroach@mywebmail.com
#@mythirddomain.com patheticcockroach@mywebmail.com

For even more examples, you can check out this part of the manual.

Now you need to specify 2 things in Postfix config file: the domains for which you configure aliases, and the path to the alias file. That’s actually just 2 lines. First open the config file:
nano /etc/postfix/main.cf
Then just add those lines at the end (replace the domain names with yours of course):
virtual_alias_domains = myfirstdomain.com myseconddomain.com mythirddomain.com
virtual_alias_maps = hash:/etc/postfix/virtual

And now surprising as it may seem given the huge size of the usual, unreadable tutorials, you’re quite almost done. There are just 2 things remaining: 1) activate those settings and 2) don’t forget to update your DNS…….

For the first point (activating the settings), here are the commands:
postmap /etc/postfix/virtual
postfix reload

This seems to be able to silently fail sometimes, or to succeed randomly or with a delay. Anyway, as a precaution in the end I would run those commands like twice in a row, just to be sure. And then wait a bit before testing (but instead of just waiting doing nothing, you can move on to configuring your DNS meanwhile).

So, the second point, the DNS, is a bit out of our scope here. When you create a BIND zone in Webmin, e-mails are actually configured to be pointed towards your server so that all is left to do is what we just already did (configure Postfix – or any other MTA – to deal with incoming e-mails). Just in case you’ve got a messed up DNS record, here are the parts needed for e-mail (because I’m nice, I added 2 example fallback MX servers – those are optional and need to be activated first, of course):
@ IN MX 10 mail
@ IN MX 20 mx.zohomail.com.
@ IN MX 30 mx2.zohomail.com.
mail IN CNAME @

Some basic testing

If you want to avoid playing ping pong with your potentially bouncing server, you can use a service such as Pingability to see if your e-mail server seems okay. It’s okay if you get a warning about abuse@yourdomain being rejected. However, if you get an error message such as a timeout on your e-mail server, it means that something’s broken. From my experience if you’re sure you did everything properly, it might be worth waiting a few minutes and then try again.
Once Pingability tell thinks your server works, well, it most likely does, just try sending yourself a test e-mail now 😉

Acknowledgments

Special thanks to Duckduckgo, thanks to which I finally managed to find the proper resources which I didn’t manage to find with Google earlier. This search engine is really one of a kind, but I’ll post more about it later.

Update (2012-02-16)

I forgot a little something: dealing with big attachments. The default, currently, in Postfix, is to limit total e-mail size to ~10MiB. Although I personally think it’s plenty already, I’m aware many people just like to send e-mails just as huge as their web-mail will allow. So you might want to configure Postfix so that it will relay also e-mails with large files, although below the size limit of your final destination (usually about 25 MiB, I guess; I’m not sure about what would happen if your Postfix accepts a big e-mail that latter gets refused by the final destination… I wouldn’t try that…).
I’ve chosen to configure mine to 20MiB. The setting is always in the same file, so:
nano /etc/postfix/main.cf
message_size_limit = 20971520
postfix reload

(NB: message_size_limit may or may not be present in your original configuration file, so either update it or create a new line) Source

Appendixes

Extra sources

Weirdly enough, after figuring out the solution I searched a bit more and then found what seems to be working tutorials…:

And finally, what I told you I’d keep for the end: nothing of this junk works… but in their defense, they did point me towards the right direction:

My config files

My DNS record file for patheticcockroach.com (BIND configuration)

$ttl 5m
@	IN	SOA	s15xxxxxx.onlinehome-server.info. zenxxxxxx.gmail.com. (
			2009011017 ; serial
			3h ; secondary refresh
			3m ; secondary retry
			2w ; secondary expire
			3h ; default record cache time
                        )
@	IN	NS	s15xxxxxx.onlinehome-server.info.
@	IN	NS	puck.nether.net.
@	IN	MX	10 mx.zohomail.com.
@	IN	MX	20 mail
@	IN	MX	30 mx2.zohomail.com.
;@	IN	MX	30 ASPMX.L.GOOGLE.COM.
;@	IN	MX	30 ALT1.ASPMX.L.GOOGLE.COM.
;@	IN	MX	30 ALT2.ASPMX.L.GOOGLE.COM.
;@	IN	MX	10 ASPMX.L.GOOGLE.COM.
;@	IN	MX	20 ALT1.ASPMX.L.GOOGLE.COM.
;@	IN	MX	20 ALT2.ASPMX.L.GOOGLE.COM.
;@	IN	MX	30 ASPMX2.GOOGLEMAIL.COM.
;@	IN	MX	30 ASPMX3.GOOGLEMAIL.COM.
;@	IN	MX	30 ASPMX4.GOOGLEMAIL.COM.
;@	IN	MX	30 ASPMX5.GOOGLEMAIL.COM.
@	IN	TXT	"v=spf1 a mx ~all" ; see openspf.org

@	IN	A	82.165.148.117
mail	IN	A	82.165.148.117
www	IN	A	82.165.148.117
gal	IN	A	82.165.148.117
img	IN	A	82.165.148.117
lime	IN	A	82.165.148.117
notepad	IN	A	82.165.148.117
piwik	IN	A	82.165.148.117
test	IN	A	82.165.148.117
test2	IN	CNAME	@

/etc/postfix/main.cf

# See /usr/share/postfix/main.cf.dist for a commented, more complete version

# Debian specific:  Specifying a file name will cause the first
# line of that file to be used as the name.  The Debian default
# is /etc/mailname.
#myorigin = /etc/mailname

smtpd_banner = $myhostname ESMTP $mail_name (Ubuntu)
biff = no

# appending .domain is the MUA's job.
append_dot_mydomain = no

# Uncomment the next line to generate "delayed mail" warnings
#delay_warning_time = 4h

readme_directory = no

# TLS parameters
smtpd_tls_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
smtpd_tls_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
smtpd_use_tls=yes
smtpd_tls_session_cache_database = btree:${data_directory}/smtpd_scache
smtp_tls_session_cache_database = btree:${data_directory}/smtp_scache

# See /usr/share/doc/postfix/TLS_README.gz in the postfix-doc package for
# information on enabling SSL in the smtp client.

myhostname = s15xxxxxx.onlinehome-server.info
alias_maps = hash:/etc/aliases
alias_database = hash:/etc/aliases
myorigin = /etc/mailname
mydestination = s15xxxxxx.onlinehome-server.info, localhost.onlinehome-server.info, , localhost
relayhost =
mynetworks = 127.0.0.0/8 [::ffff:127.0.0.0]/104 [::1]/128
mailbox_size_limit = 0
recipient_delimiter = +
inet_interfaces = all

# for brute forwarding
virtual_alias_domains = patheticcockroach.com example.com
virtual_alias_maps = hash:/etc/postfix/virtual

Posted in Linux, postfix, servers.


0 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.



Some HTML is OK

or, reply to this post via trackback.

Sorry about the CAPTCHA that requires JS. If you really don't want to enable JS and still want to comment, you can send me your comment via e-mail and I'll post it for you.

Please solve the CAPTCHA below in order to fight spamWordPress CAPTCHA