Download your favorite Linux distribution at LQ ISO.
Go Back > Forums > Linux Forums > Linux - Newbie
User Name
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!


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

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
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 375

Rep: Reputation: 115Reputation: 115
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
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 36

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?)

# 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

# do we know the port to listen on?
if [ ! $port ]; then
echo USAGE: $0 port ;

#Start netcat here
echo "-l -p $port -e $0" | nc 2>/dev/null &
[ $1 ] && exit;

#generate your file name for the email

sleep 1
echo "220 ESMTP bash_mta"

while read line; do
case $line in

HELO*) # hello message and server ID.
echo "250"
echo $line >> $FILE

echo "354"
echo $line >> $FILE

echo "221 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"

echo "I could also continue the existing script that I am in and work with " >> $FILE
echo "the file that I just created." >> $FILE


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 > Forums > Linux Forums > Linux - Newbie

All times are GMT -5. The time now is 03:41 AM.

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