Thursday, January 20, 2011

Executing command after receiving an email

How would I set Postfix to execute a command when it receives an email to a given address/username, or perhaps an email containing some text?

  • There isn't any way to match upon text, but you can forward all messages to an address to a program.

    You need to add an alias to your system aliases file, usually /etc/postfix/aliases or to the user's .forward file. The first option has the better flexibility, cause you can have an aliases for an address which doesn't actually map to an account.

    The alias should be something like

    |/usr/local/bin/command

    You should give a full path, because you don't know the context that it will be executed in.

    If your program exits with 67, then this will be bounce the message as unknown user, 0 will drop the message. Anything else will be retried until the message times out and bounces.

    Be careful of security - you're basically allowing anyone on the Internet to run a program on your system, so don't trust user input, and sanitize it before you use it.

    From gorilla
  • It's been a while since I played with Postfix a lot, but IIRC it usually came bundled with a fairly basic MDA, but this could still understand .forward files in the users home dir, you'd need to read the docs and your postfix config to find what MDA is configured.

    The daddy of all MDAs (IMHO) is procmail. You can substitute procmail for the current MDA in your main.cf - see http://www.postfix.org/faq.html#procmail

    Procmail reads a file in the users home directory to determine how to process messages. This goes way beyond just being a config file - its more like a programming language. Its certainly capacble of what you ask.

    C.

    Patrick R : +1 - nice link - that part about "Sending mail to a FAX machine" might be a perfect example for what starsky is looking for.
    From symcbean

0 comments:

Post a Comment