receive and parse email in bash (or execute script when message is received)
Linux - NewbieThis 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
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
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
.) # 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
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.