LinuxQuestions.org
Go Job Hunting at the LQ Job Marketplace
Go Back   LinuxQuestions.org > Forums > Linux Forums > Linux - Newbie
User Name
Password
Linux - Newbie This Linux forum is for members that are new to Linux.
Just starting out and have a question? If it is not in the man pages or the how-to's this is the place!

Notices

Reply
 
Search this Thread
Old 11-14-2009, 09:58 PM   #1
Guyverix
Member
 
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 31

Rep: Reputation: 2
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.
 
Old 11-14-2009, 10:35 PM   #2
speck
Member
 
Registered: Nov 2001
Location: California
Distribution: Slackware 14.1
Posts: 271

Rep: Reputation: 54
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.
 
Old 11-15-2009, 07:29 AM   #3
Guyverix
Member
 
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 31

Original Poster
Rep: Reputation: 2
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
 
  


Reply

Tags
bash, email, mail, mta, script


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How to parse strings in bash script crimson08 Linux - Newbie 17 05-11-2009 11:29 AM
[shell script] execute command and parse output stoiss Programming 2 01-26-2009 01:49 AM
script to confirm email received from both sides arsha Linux - Networking 1 07-01-2008 02:19 AM
script to parse variables to curl script and execute morphix Programming 17 11-30-2007 01:27 AM
run script when email is received Syncrm Linux - General 10 04-20-2003 04:57 PM


All times are GMT -5. The time now is 01:56 AM.

Main Menu
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration