7

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.

3
  • You also need to decide on how to get the mail. Do you want to pick that up from some account or do you want that server to handle all mail for a specific (sub-) domain? For all users?
    – Anthon
    Feb 18, 2015 at 10:51
  • It would be good to pick up all mails which are sent to that specific domain, although if that complicates things I can set it up for a single account.
    – errata
    Feb 18, 2015 at 10:54
  • The best way is to use new transport (see details here serverfault.com/questions/451240/…)
    – vadim_hr
    Jan 28, 2020 at 6:14

1 Answer 1

5

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 main.cf:

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:

:0
$ ^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.

5
  • Thanks for a great answer, this was very helpful although (in my opinion) under the hood it is a complete mess to configure all this stuff :)
    – errata
    Feb 18, 2015 at 14:57
  • 1
    @errata Just start with one thing at a time. If you have specific problem with any of the steps, just come back here and post a detailed question.
    – Anthon
    Feb 18, 2015 at 15:24
  • Definitely will, although I am considering a completely different approach at the moment because I am far away from system admin and even if I manage to set up this thing I won't be sure if I did it "properly" :) Thank you for your effort, will definitely ask if I will need something!
    – errata
    Feb 18, 2015 at 15:27
  • I would want to set up a program to receive all email for a domain as I have a fixed IP address. I don't want to be polling another server. I would prefer to process the email with PHP (or Python) and depending on the outcome forward the email to another email address. From the above answer I'm not quite sure how to do that...? Mar 20, 2018 at 15:53
  • @TheStoryCoder First of all if you have question, you should post it as such and not abuse comments on some other answer (or question). If you have a fixed IP address, set up your MX address and you don't need fetchmail to fetch your email. Postfix will handle the email in both cases.
    – Anthon
    Mar 21, 2018 at 20:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .