Execute command for select emails with postfix

emailpostfix

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:

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

And here is ~/bin/extract_email_body.py:

#!/usr/bin/python

import sys
import email

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

msg = email.message_from_file(sys.stdin)
print_plain_text_parts(msg)

Best Answer

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.