Im sure almost everyone is familiar with Posterous’ killer mail-in feature that allows you to email your blog post to update your blog page, so I won’t ramble on about how awesome I find that feature (yet, I’m still using wordpress largely because my blog migration ended up requiring me to reformat all my posts on posterous again and it does not let me preview my blog.)
So my goal was to setup a mail server that would accept incoming posts (emails which could contain anything from your blog post to images ) to update user accounts or databases.
The way I planned this was to use postfix as my mail server that would use a catch-all email address which would collect and save all the emails in a Mail directory and would allow me to use a ruby program to periodically check the Maildir for new mails to process.
The second part of the process is beautifully described in Jason Seifer’s blog on receiving mails with Rails where he also describes how to configure your server but I needed to google around a lot to actually get that done. Though Jason Seifer’s blog describes a technique which allows you to forward your mails directly to a ruby program I decided to stick with Rick Olsen’s suggestion on having a separate daemon process work on a Maildir
I used an Ec2, Ubuntu instance to setup my basic Rails application and I sent in the necessary requests to ensure that the reverse DNS lookup points to my domain.Here’s a brief overview on the things I had to do setup the mailserver .
Open up the required ports and remember to open the SMTP ports (25) to all. (I realized this the after being ridiculed at the IRC channel.) I realized that EC2 opens the port for you but you need to ensure that its open to all or it won’t be reachable from the outside and you’ll end receiving annoying sending Delayed messages all the time.
This is an automatically generated Delivery Status Notification THIS IS A WARNING MESSAGE ONLY. YOU DO NOT NEED TO RESEND YOUR MESSAGE. Delivery to the following recipient has been delayed: Message will be retried for 2 more day(s) Technical details of temporary failure: The recipient server did not accept our requests to connect. Learn more at http://mail.google.com/support/bin/answer.py?answer=7720 [mail.example.in. (10): Connection timed out]
I did not realize this as I was able to telnet to port 25 but the guys at the IRC channel couldn’t. Just in case your curious on how to open a port to all on EC2 have a look at my earlier posts.
Once your done with that its time to add the MX records
A mail.example.in 220.127.116.11 (delete) 300 2010-10-12 03:37:24 A example.in 18.104.22.168 (delete) 300 2010-10-11 04:34:53 MX example.in mail.example.in (delete) 300 10 2010-10-12 02:03:03 A www.example.in 22.214.171.124 (delete) 300 2010-10-11 04:34:15 MX www.example.in mail.example.in (delete) 300 10 2010-10-12 02:12:51
It could look something like this. I was using name.com but this view may differ based on your provider.
The MX records are crazy important at the time of configuration and here are some useful docs on how you go about adding MX records
Once your done with that install postfix,
sudo apt-get install postfix
and your good to go. The configuration steps expect you to respond to certain questions. The answers would be as follows
1. For an internet site (Since your building an web based site)
2. mail.example.com (where example is your domain name)
Time to create the catch all email address that will receive all the email. I created an Ubuntu user called ‘sid’ to whom these mails would be pushed to but there are ways where this need not be an actual user.
Postfix uses a mbox format to store your mails but I read that the Maildir is better and faster and less susceptible to corruption. In case you are curious about the difference here is a nice place to start.
So to manually convert your mbox format to a maildir format
sudo postconf -e 'home_mailbox = Maildir/'
So this would create a /home/sid/Maildir to store all your mails.
DoveCot is package that allows you to configure SMTP-AUTH. You can configure this in other ways but this is the easiest I could find.
“SMTP-AUTH allows a client to identify itself through an authentication mechanism (SASL). Transport Layer Security (TLS) should be used to encrypt the authentication process. Once authenticated the SMTP server will allow the client to relay mail. “
sudo apt-get install dovecot-postfix
Thats pretty much our mail server. Now to allow are mail server to receive mail from the outside we need to check if we can access port 25 (Remember to open this for all) and verify our postfix configuration file (/etc/postfix/main.cf) to check if the inet_interfaces is set to all
inet_interfaces = all
Try telnetting to port 25 and if you see something like
Trying 126.96.36.199.. Connected to 188.8.131.52. Escape character is '^]'. 220 example.com ESMTP Postfix
Next is to setup our catch-all address
Open up /etc/postfix/virtual (this would be an empty file in most cases) and add
The @yourdomainname is a accept-all entry that will push all mails that reach your domain to the maildir of the user listed next to it.
You may also have a specific id like post@mydomainname being sent to user sid
save the file and verify if your configuration (/etc/postfix/main.cf) file has the following line
virtual_alias_maps = hash:/etc/postfix/virtual
if not add it and reload postfix
sudo postmap /etc/postfix/virtual sudo service postfix reload
Thats pretty much all there is to it.
Here are a set of references that been extremely useful to me
ServerFault: Postfix can’t receive external mail
Postfix setup catch-all email accounts using /etc/postfix/virtual
Amazon Reverse DNS Request Form
Hope this is helpful.