Having worked with Linux for over 10 years, I’ll be the first to tell you that setting up, running and maintaining a working mail server can be a royal pain! There’s a lot to keep up with (standards, protocols, ports, spam etc) and backing up your configuration is an absolute must; no exceptions!
I say this not to deter you, though unless you’ve got plenty of free time, using a service, such as the one mentioned (MailGun) or a similar alternative, such as Mandrill, SendGrid etc, is a much better and far more reliable solution. This is even more so true if you’re looking for a lightweight solution.
While using such services will require that you’re able to code, all three that I mentioned have SDK’s for PHP, Python and various other programming languages, so the initial bar to entry is lowered.
That being said, many of the SDK’s rely on third-party libraries (specifically the PHP SDK’s), so if you’re the type who craves complete control over your code (like me!), the best solution is to create your own SDK and wrap it around the API.
In the case of MailGun, which I happen to be working with right now, mail is sent and received through their service and you would setup your MX records to point to theirs. You would then create aliases on their servers through their control panel and define the route that the service pushes your e-mail to.
For example, if you setup
firstname.lastname@example.org, you can choose to store the incoming messages for up to two days (free accounts, I believe paid accounts allow for longer storage periods). During this time, you’d simply call the API to fetch the e-mail and in turn, parse the JSON and store it to your DB (or do whatever you’d like with it). You can also have the service “POST” the request to a URI, thus you could setup automatic parsing for each request as it is received, thus negating the need for a CRON Job.
You’d simply enter in the URI and when a request is received, MailGun will make a POST request and from there, your script will handle the request.
Mail, of course, can be sent from your script through MailGun as well.