Setting up receive-only mail server and handling incoming mail and attachments

I would like to create a following scenario on my Ubuntu 14.04 server:

  • set up mail server (or is it called ‘Mail Transfer Agent’?) which should take care of receiving mails only. No need to set up sending mails from that server.
  • perform basic filtering of incoming mails (check if mail has a proper attachment – exactly a file with a specific extension)
  • save attachment only to some specific folder
  • delete processed message(s)

I know almost nothing about mail servers and I have tried to play a little bit with Postfix but if I understood correctly there are some better solutions for mail filtering and processing? I know Python and PHP so I could handle filtering/processing with one of those languages, but still not sure where exactly to start.
I’d prefer to handle incoming mail immediately after receiving if possible, rather than running cron job on some Python script, and delete processed message immediately after saving the attachment.
I don’t need to setup any security or spam/virus protection at this particular moment.

Since I’m a bit confused about how to set up this workflow, I’d like to ask for at least a starting point for each step and also links for further reading are more than welcome! I don’t ask for complete solution, but some tips about packages which could be helpful in this scenario are really appreciated.

Asked By: errata


You indicate you want to run this on your server. If that machine is not on a permanent IP address, so it can handle mail sent to specific mail domain, you need to setup picking up email from the provider that does handle the mail.

Since you indicate you want to delete messages after handling, you should not care about having all mail gathered by your provider in one user account, so you should set that up.

Then you can pick up the mail using fetchmail or a similar program, which talks to your locally running postfix only listening to local connections. (if your server has a fixed IP address and can handle external stmp connections you can skip this step and only do the rest).

postfix should be configured to use procmail as your mailbox command (in

mailbox_command = procmail -a "$EXTENSION"

fetchmail should be run as a regular (I check every minute with my provider) cron job for some user that does the procesing. That user should have a ~/.procmailrc file which can do some filtering and hand the email of to a script using an entry like:

$ ^Subject:.*some_text_that_has_to_be_in_the_subject_to_be_valid_for_processing
| /usr/local/bin/your_program 

Now your_program needs to be able to handle the file format (header followed by body). I use python and its standard email module for handling the MIME attachment intricacies.

Tip: Have your_program store the incoming data (on sys.stdin) completely in some file xyz during development, so you can look at that and develop that indepedently ( by just running your_command < xyz ) so you don’t have to go through the hassle of sending out emails with attachments to test that stage of the process.

Answered By: Anthon
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.