Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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)
share|improve this question
add comment

3 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

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.

share|improve this answer
add comment

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).

share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.