receive and parse email in bash (or execute script when message is received)
I am trying to figure out a way that when an email is received on my local machine the mail program will execute a bash script echoing the full email message into the script. I have been searching, and not found much for receiving / parsing email in bash, just how to send it. If anyone has a direction they could point me in, it would be greatly appreciated.
My intent goes something along these lines: message recieved >> execute script (echo message) & >> delete message The script itself will parse the message with case statements so I can create SNMP traps from the message itself. I am not too worried about the script, as I have ideas on how to do that part, but getting the darn data in there has got me stumped. |
Procmail could be used to "intercept" the incoming mail message and then execute a bash script (based on some match found within the message). It also has the ability to strip out the body of the email, if that's all you need, and pass that data to your script.
Procmail Tips site that gives a good overview about how it works. But I would be very cautious with just dumping the contents of an email into a script. |
I know just about anybody who reads this is going to just cringe, but I found an alternate solution to using procmail. I wrote the worlds dumbest MT/DA in bash. The scary thing is, it does what I need.. I know it is not safe and we will not even mention RFC, but this WILL receive emails and put them into /tmp/mail###.txt I thought I would share this in case anyone wants to do something silly or cool.. (come on, there is a bash webserver, so why not this too?)
#!/bin/bash # This is a personal MDA that I am using to parse mail to SNMP traps. # Do NOT ever use this for anything important! It is a toy! (but a kinda cool one) # This script has no safety! Only use in on Local LAN's. # remember anything less than port 1024 needs root to run. export port if [ $1 ] ; then port=$1 fi # do we know the port to listen on? if [ ! $port ]; then echo USAGE: $0 port ; exit; fi #Start netcat here echo "-l -p $port -e $0" | nc 2>/dev/null & [ $1 ] && exit; #generate your file name for the email CHECK=$RANDOM FILE="/tmp/mail$CHECK.txt" sleep 1 echo "220 localhost.localdomain.com ESMTP bash_mta" while read line; do case $line in HELO*) # hello message and server ID. echo "250 localhost.localdomain.com" echo $line >> $FILE ;; DATA*) echo "354" echo $line >> $FILE ;; QUIT*) echo "221 localhost.localdomain.com doing something" ;; quit) # quit the connection echo "250 ok" echo $line >> $FILE ;; .) # hopefully a period by itself for this case echo "250 ok" echo $line >> $FILE # echo "Message queued as -never going to happen, fake MDA-" ;; *) # all message data (I hope) echo $line >> $FILE echo "250 ok" ;; esac done echo "I CAN RUN A SCRIPT AGAINST THE TMP FILE" >> $FILE echo "I could also continue the existing script that I am in and work with " >> $FILE echo "the file that I just created." >> $FILE |
All times are GMT -5. The time now is 08:46 PM. |