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 |
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.
Are you new to LinuxQuestions.org? Visit the following links:
Site Howto |
Site FAQ |
Sitemap |
Register Now
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.
|
 |
11-14-2009, 10:58 PM
|
#1
|
Member
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 36
Rep:
|
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.
|
|
|
11-14-2009, 11:35 PM
|
#2
|
Member
Registered: Nov 2001
Location: US
Distribution: Slackware 14.2
Posts: 375
Rep: 
|
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.
|
|
|
11-15-2009, 08:29 AM
|
#3
|
Member
Registered: Nov 2008
Location: Washington State
Distribution: Mint
Posts: 36
Original Poster
Rep:
|
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:35 AM.
|
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.
|
Latest Threads
LQ News
|
|