I have written a small little script that looks up a work on wikipedia and then prints the result to the command line. I want to be able to email that word to my server and then have my server email me the results back.
So far I have a new user named 'wiki' where the e-mails are being sent to. I am receiving the e-mails fine. In /etc/aliases I have redirected incoming emails开发者_JAVA技巧 to my script I wrote.
# See man 5 aliases for format
wiki: "|/home/wiki/scripts/wiki"
That works fine.
My script works find from the command line, as if i typed
$ ./wiki <whatever>
I get permission denied errors when I sent an email to wiki@mydomain.com
My wiki script permissions are:
-rwxr-xr-x 1 wiki wiki 427 2011-04-18 22:54 wiki
What is wrong! What permissions do I need to set? Any help is appreciated.
EDIT (4/18/11 8:20pm): This is my script.
#!/bin/bash
read MSG
echo $MSG >> "newfile"
FROM=$(echo "$MSG" | cut -d " " -f2)
DATA=$(echo "$MSG" | cut -d " " -f3)
if [ MSG ]
then
RTN=`nslookup -q=txt $DATA.wp.dg.cx | grep "text =" | cut -d"=" -f2`
echo $RTN | sendmail -s "wikipedia: '$DATA'" $FROM
else
echo wilkipedia nslookup. Please supply a command line argument.
fi
Depending on you mailserver (e.g. postfix) you may need to configure it to allow piping mail. Most often, using ~/.procmailrc is easiest.
This is what I did to take care of my problem...
I scrapped my bash script, and wrote the same function in python. I used python because of its email parsing functionality. I also ran the user input through some string escape prevention code so that I could safely hand it off to python's
subprocess.popen()
method.I was trying to write the stdin to a local file so I could see where to parse the text, however the mail application (mail or postfix, not sure) doesn't have permissions to write files - Prob a good thing in the end. To debug, I just returned the raw stdin (email header and all) in a string and emailed it back to myself to see what was going on.
I set /etc/aliases to read
wiki: "|/home/wiki/scripts/wiki.py"
permissions on wiki.py are
4 -rw-r--r-- 1 wiki mail 1902 2011-04-19 21:04 wiki.py
and this way the mail program successfully hands it off to your script. You can also go check "/var/log/mail.log" for output if you have errors in your script and nothing gets emailed back to you. If the mail reached the script, but error-ed out because of syntax or a bug, you should get an email reply from MAILER-DAEMON stating that it was undeliverable.
I didn't use
procmail
or have a.procmailrc
file./etc/aliases
worked just fine. It passes your e-mail to the stdin stream, and in python try this:
extra=""
while 1:
line = sys.stdin.readline()
if not line:
break
extra = extra + line.strip(" ")
精彩评论