0

I have a server (example.com) running on Debian that is not configured to receive emails (or at least I've never done any effort to do so). From what I understand I just need to create an MX DNS entry pointing to example.com to be able to receive emails.

Now once I receive emails, I want the headers + the body to be piped to a script (probably Go) that will parse the content and add it to a database).

I've heard that I can use procmail, but is this all I need? Don't I need to setup something that will receive the email coming from an MX route?

PS: it looks like postfix is running on my server (after doing a ps aux), is it my MDA? Is it what I have to configure to add some procmail magic to it?

1 Answers1

2

If Postfix is correctly configured, it will know how to run Procmail out of the box. Just create a simple .procmailrc file and send yourself a message. Detailed guidance e.g. at http://www.iki.fi/era/mail/procmail-debug.html but do read through to the end before you spend time on this answer; the last paragraph suggests you don't.

You don't absolutely need Procmail for this; I believe you could configure Postfix to write to your Go program directly. But unless your program is extremely failure-resistant, Procmail offers a security scaffolding which is useful as a safety net at least during development.

To have a copy of every mail message for a user account you@example.com be piped as standard input to goprogram, the account owner's /home/you/.procmailrc only needs

# If goprogram is in a nonstandard directory,
# add it to PATH
PATH=$HOME/go/bin:$PATH

:0c
| goprogram

Obviously, make sure you have permissions to execute goprogram and that your PATH includes the directory where it is installed. (You could obviously pipe to /path/to/goprogram and leave the PATH alone, but if things get nontrivial, you want to avoid littering the .procmailrc with hard-coded paths to every binary. Like in the shell, Procmail's default PATH lists a number of standard system-wide locations like /bin:/usr/bin:/usr/local/bin etc.)

The c flag causes Procmail to regard this as a secondary destination, so every message will also be written to the default inbox /var/mail/you (or maybe a Maildir in the user's home directory, these days). Once you are confident your goprogram doesn't lose mail (test for disk full, out of memory, etc) you can take out the c and maybe eventually run it straight out of the Postfix master.cf and get rid of Procmail and the Unix user account you in favor of a virtual user.

Technically, Postfix is your MTA and MSA; it knows how to use Procmail (or Maildrop, or a bunch of other alternatives) as the MDA. In order to have an MX record, you need an MTA on the host which the MX points to. A correctly configured Postfix just needs to be told which domain name(s) it should accept inbound traffic for (basically myorigin and relay_domains, but should othewise be ready to serve as the primary MX

If you are unfamiliar with a lot of this territory, you should probably avoid running your own mail server at all. E.g. Amazon SES allows you to easily run a script of yours on every incoming message without having to worry about the quite significant administrative burden and tall learning curve of stable, secure email service.

tripleee
  • 1,416
  • 3
  • 15
  • 24
  • Disk full seems like a "turtles all the way down" problem. But Postfix has already queued the message locally on disk, so your program needs to save a second copy before Postfix will purge the spool file. Until you clear up space on the disk, Postfix can keep it in the queue, but you need to signal an error to tell it that you couldn't successfully accept the message if it tries to deliver it but your program can't complete the task. – tripleee Oct 28 '17 at 08:59
  • The `mailman` configuration example in the Debian wiki I link to should give you a rough idea of how to cut Procmail out of the loop. – tripleee Oct 28 '17 at 09:07
  • The presentation is kind of backwards in a way, but if you decide to implement this, I actually suggest you set up Procmail and test with local messages before you start to look at exposing your creation to the world. – tripleee Oct 28 '17 at 09:13