I have Postfix running on my Ubuntu server, and I'd like to configure it to run a command when it receives certain emails. For example, if I email myself from a gmail account with the subject "todo", I'd like Postfix to append the body of the message to /home/brian/todo.

From reading Postfix docs, I've only seen how to configure rules to reject emails with error codes. So maybe Postfix is not the tool for the job, in which case I'd appreciate feedback on a more appropriate tool.

Thanks, Brian

[Update: 20090616] Expanding on the selected answer, here is my .procmailrc:

* ^To.*john@doe.com
* ^Subject.*specialsubject
| ~/bin/extract_email_body.py >> ~/todo

And here is ~/bin/extract_email_body.py:


import sys
import email

def print_plain_text_parts(msg):
    if msg.is_multipart():
        for part in msg.get_payload():
    elif msg.get_content_type() == 'text/plain':

msg = email.message_from_file(sys.stdin)

you can use procmail to filter e-mails according to subject. Then you will have the option to run any command in response to the e-mail, or you can leave it untouched and have it delivered to your mailbox.

You can read procmail quickstart which is an excellent article.

To enable procmail usage, place a ".forward" file under your user's home directory, and make its content like this

| "/usr/bin/procmail"

Then write your procmail "recipes" under ~/.procmailrc

Note that most other filtering options (like sieve) will not allow you to execute arbitrary commands.


try this - put into /etc/aliases:

username: "|/your/script"

and then run

postalias /etc/aliases

when e-mail comes to username@yourdomain.org /your/script will be executed and will receive content of message [ with all headers ] into stdin. i'm affraid you'll have to check the subject by your self.


If you only want some emails to a given address to trigger a command (based on their subject or whatever), and others to be delivered as normal email, you'll have to use procmail as described above. Using /etc/aliases will cause every single message sent to that address to invoke the specified command (and won't be delivered via the local delivery mechanism).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy