E-Mail to Python via Procmail: Futile?

software development

#1

I’m trying to use mail2entry, a Python 2.2 script, along with a .procmail pipe command, to post e-mailed images to a weblog (creating a ‘moblog’).

I know I need to use Python 2.2 (/usr/bin/python2.2) instead of the default Python 2.1.3 because the mail2entry script requires “email.Header” and “True” functions (I don’t know what these mean, but searching for them after an error message found other discussions concluding that the newer version of Python was needed).

I created a new user with shell access and an e-mail account (seconduser@mydomain.com). Under that user, I used the .forward.postfix kludge to pipe mail to procmail, then used a procmail filters.rc recipe to send all received messages to the python 2.2 script (in my other user directory):

:0
| /usr/bin/python2.2 /home/mainuser/mail2entry/mail2entry.py

My problem? I don’t even know if e-mail sent to seconduser@mydomain.com is being sent correctly to the script in my main user directory. And when I try to test the mail2entry script via Python2.2, I get errors like

UnboundLocalError: local variable ‘result’ referenced before assignment
SyntaxWarning: import * only allowed at module level
ImportError: No module named Header

That last one despite using Python2.2 (which I read is where ‘Header’ was added).

Any ideas? Or am I in way over my head?


#2

If you want to pipe all mail to a particular username to your script, you can just use a .forward.postfix file to pipe mail directly to the program.

Do note that you may run into some weirdness doing stuff like this because the mail machines don’t always have identical installs to the web machines. Currently, the setup should be about the same, and python2.2 should be installed, but if you want to be really careful, you may want to do a local install of python. Your home directory is mounted by both the mail and web machines, so if everything is self contained in your home dir, you’re less likely to run into problems.

Assuming you’re using procmail, the first thing you want to do (if you’re not already doing it) is to enable logging, and watch the procmail log. If one isn’t being created, the problem is almost definitely a permissions problem.

Lastly, you want to get the script working perfectly (taking an email message from stdin) before you try to start forwarding email to it. Another thing you can do to test stuff out is to use a .forward file instead of the .forward.postfix file - this will process mail sent to the local user rather than that sent to the mail machines (i.e., mail coming in to an address at your domain). So then you can just send your test messages to username@machine.dreamhost.com (where machine is your user machine).

Hope this helps a little.


#3

I think procmail is working fine (though thanks for the ‘postfix’ comment - I thought it was odd to forward all mail to another forwarder to forward!)… as a log is being generated. That’s the good news. The bad news is, it doesn’t work, and the log file only says:

procmail: Error while writing to “/usr/bin/python2.2 /home/mainuser/mail2entry/mail2entry.py”

Where could I see what that error is? I checked the error logs under my mainuser, but see nothing traceable to this pipe/script.

And… should my procmail pipe command be in quotes or not? That is:

| “/usr/bin/python2.2 /home/mainuser/mail2entry/mail2entry.py”

versus:

| /usr/bin/python2.2 /home/mainuser/mail2entry/mail2entry.py

Or should I be leaving out the ‘python’ invocation completely? (Both machines seem to have python2.2 in the same place.)

Finally, where can I get a test message (stdn)? I know I can ‘cat’ pipe a test message straight to the mail2entry.py script if I know how to find a sample source. I tried throwing one at it straight from ‘Maildir/new’ (1071622491.13952_0.machinename). I get:

“IOError: [Errno 13] Permission denied: /home/mainuser/mail2entry/mailtoentry.py”

Thanks for the quick and patient reply!


#4

Update. I tried throwing a message at it from seconduser, and got the aforementioned permission error. So I tried throwing a message at it from mainuser (which is technically the owner of the mail2entry.py script), and got (in the mail2entry’s log):

Traceback (most recent call last):
File “mail2entry.py”, line 25, in main
import parsemsg
File “/home/mainuser/mail2entry/parsemsg.py”, line 9, in ?
from email.Header import *
ImportError: No module named Header

So. Do I somehow need to change ownership of the script from mainuser to seconduser (since seconduser, with the procmail script, will be the user throwing files at it)? And does the ‘No Module named Header’ above mean I’m out of luck until a newer Python install?

Thanks again!


#5

Is the script executable by both users currently? Does it work if you do it as “mainuser”?

Well you might consider doing your own install of a newer Python.

Does the script mention requiring a particular version?


#6

I think (though I could be wrong) that you want:
"|/usr/bin/python2.2 /home/mainuser/mail2entry/mail2entry.py"


#7

Trying to run it as seconduser gives me the permission error, so I’m guessing I need to change the script’s ownership from mainuser to seconduser.

How do I do this? Running ‘chown seconduser:group mail2entry.py’ as mainuser gives me “Operation not permitted.”

My question is a guess because it’s looking for a ‘Header’ module. I presumed that’s because the python2.2 that DH has doesn’t have that piece installed (and it was introduced in 2.2.x, according to another forum post).

The script didn’t come with much documentation, but it does mention Python => 2.2.2. I guess the version DH has under /usr/bin/python2.2 is Python 2.2.1. D’oh!

How hard is a Python install?

Mahalo!


#8

You didn’t answer the part about the permissions on the script. You might just need to give group execute permissions to the program if it doesn’t already have them. Or you might need to make something else group readable or executable.

You also didn’t mention whether it works as mainuser.

You can’t chown files, so all else failing, you’d have to just copy the program.

A “permission denied” error can mean a lot of things, though. You might try using strace to see what file permission is being denied to.

I’ve heard it’s pretty easy.


#9

It is very easy. The Dreamhost Knowledgebase tells you exactly what to do. You can find the location of the newest Python release at http://python.org/2.3.2/.

The key step that differs from the install instructions at the Python.org site is the “./configure” step. The Python.org instructions work fine only if you can install it as root.