views:

1688

answers:

3

I have a pretty usual requirement with procmail but I am unable to get the results somehow. I have procmailrc file with this content:

:0
* ^To.*@myhost
| /usr/bin/python /work/scripts/privilege_emails_forward.py

Wherein my custom python script(privilege_emails_forward.py) will be scanning through the email currently received and do some operations on the mail content. But I am unable to get the script getting executed at the first shot(let alone scanning through the mail content).

  • Is this a correct way of invoking an external program(python) as soon as new mail arrives?
  • And how does my python program(privilege_emails_forward.py) will receive the mail as input? I mean as sys.argv or stdin????
+3  A: 

That is just fine, just put fw after :0 (:0 fw). Your python program will receive the mail on stdin. You have to 'echo' the possibly transformed mail on stdout.

fw means:

  • f Consider the pipe as a filter.
  • w Wait for the filter or program to finish and check its exitcode (normally ignored); if the filter is unsuccessful, then the text will not have been filtered.

My SPAM checker (bogofilter) just works like that. It adds headers and later procmail-rules do something depending on these headers.

Johannes Weiß
I tried that too. But still it doesn't work I dont know why :( Here is my procmailrc script::0 fw* ^To.*@myhost| /usr/bin/python /work/scripts/privilege_emails_forward.py
Maddy
try VERBOSE=yes and have a look in your procmail log file. Perhaps you can spot the error!
Johannes Weiß
Johannes! Since this comment text field is not enough to print the procmail log output, I have given the output as an ANSWER. please have a look at it.
Maddy
Johannes! I have checked the error log, and one of the things which caught my eye is: Executing "/usr/bin/python,/work/scripts/privilege_emails_forward.py"Why is there a comma between /usr/bin/python and my custom script. First of all is my procmailrc correct?
Maddy
A: 

The comments block is too small. Thats why I am commenting using this:
Johannes! This is what got.

procmail: Assigning "INCLUDERC=/work/scripts/privilege_emails_forward.rc"
procmail: Assigning "VERBOSE=yes"
procmail: Match on "^To.*@myhost"
procmail: Executing "/usr/bin/python,/work/scripts/privilege_emails_forward.py"
procmail: Bypassed locking "/var/mail/webmail.lock"
procmail: Assigning "LASTFOLDER=/var/mail/webmail"
procmail: Opening "/var/mail/webmail"
procmail: Acquiring kernel-lock
procmail: Notified comsat: "webmail@12489:/var/mail/webmail"
Folder: /var/mail/webmail 1

And my python script doesn't get executed. Where is the bug?

Maddy
I did at test here and everything worked find.Thats my ~/.procmailrc, hope you can read it:VERBOSE=yesLOGFILE="/home/test/.procmail.log":0 fw* ^To.*@.*| /usr/bin/python /home/test/test.py
Johannes Weiß
perhaps, you want to write a little test program instead your script. my /home/test/test.py looks like that: #!/usr/bin/pythonimport sysf = file("/tmp/procmailtest", "w+")f.write("hallo")x= sys.stdin.read()f.write(x)f.close()print x
Johannes Weiß
after sending a mail to test, I had the mail text and "hallo" in the file, so it seems to work!
Johannes Weiß
Thanks Johannes! Its working now. I dont know what was missing! Cool Thanks Johannes once again :D
Maddy
+1  A: 

The log excerpt clearly states that your script is executed, even if it doesn't show the desired effect. I'd expect procmail to log an error if the execution failed.

Anyway, make sure that the user (uid) that procmail is executed with has the correct permissions to execute your script. Wire the script into procmail only if you succeeded testing with something like this (replace 'procmail' with the correct uid):

# sudo -u procmail /bin/sh -c '/bin/cat /work/scripts/mail.txt | /usr/bin/python /work/scripts/privilege_emails_forward.py'

Depending on your sudo configuration, you'd have to run this as root. Oh, and make sure you use absolute file paths.

paprika
Awesome paprika! I tried to run the script you gave with procmail replaced with webmail(current user), My script got executed. But when I receive a mail(be it from anybody),my procmail runs the procmailrc right? So who will be the correct user to run the script?
Maddy
Paprika! To explain clearly, my custom python script has got enough permissions(777), still its not getting executed automatically when I receive a mail
Maddy
To find out as which user your procmail script is run, replace the script's line in your procmailrc with this: '| /usr/bin/id -un > /tmp/procmailuser' Don' forget the pipe symbol in front! Afterwards, check 'cat /tmp/procmailuser' should reveal the user.
paprika