> I receives like the following -
> Is it possible to log onto my POP3 account grab the email and extract the
> Order Number
> Order Date
> Ref# (000000000000)
> Ship To: Name and Address
Yes. You can do it all in a shell script if you have bash2 or
ksh93, but I'd recommend using a mail client to save the
message to a file.
> ******Email Sample*****
> C U S T O M E R R E C E I P T
> X O M XXR Order Number: 175381
> America's Premier Value Merchant Order Date: 4/4/2004
> 84 Commercial Road Ship Via: FedEx
> Huntington, IN 46750 Payment: MC
> 800-348-5004
> Fax: 219-358-1806
> Bill To: Ship To:
> Steve xxxxx Rochelle Trom
> 5xxx South xxxxxx xxx Re: 000000000000
> 1920 West 1 Street
> xxxx Springs, xx xxxx Superior, WI 54880
> Telephone: (x1x)2x9-xxxx
> ********End Sample************
It's hard to tell from the mangled message above just which parts
are the ship-to address (is the REF# actuall in the middle of
it?), but the Order Number, Order Date, and Ref# can be obtained
with:
## these work in bash2 and ksh93
NL=$'\n'
CR=$'\r'
## use literal newline and carriage return in other shells
msg=`< MSG-2` ## Use correct name of file
tail=${msg#*Order Number: }
on=${tail%%$NL*}
tail=${tail#*Order Date: }
od=${tail%%$NL*}
ref=${tail#*Re: }
ref=${ref%%$NL*}
echo "ORDER NUMBER: $on"
echo "ORDER DATE: $od"
echo "REF #: $ref"
#### EOS ####
To retrieve the e-mail from a POP3 server in bash or ksh:
## set these variables to appropriate values
pop=localhost
user=bgates
pass=money
cmd() {
printf "%s\n" "$*" >&3
read ok num junk <&3
[ "${ok%$CR}" = "+OK" ] || { printf "%s\n" "$*"; exit 5; }
Quote:}
NL=$'\n'
CR=$'\r'
exec 3<>/dev/tcp/$pop/110
read ok num junk <&3
[ "$ok" = "+OK" ] || exit 5
cmd user $user
cmd pass $pass
cmd stat
msg=1
MSG=$num
while [ $msg -lt $MSG ]
do
retr=0
cmd top $msg 0
while IFS= read -r line <&3
do
line=${line%$CR} ## strip CR
case $line in
From:*"$from"*) retr=1 ;;
.) break;;
esac
done
if [ $retr -eq 1 ]
then
cmd retr $msg
while IFS= read -r line <&3
do
line=${line%$CR}
case $line in
.) break ;;
*) printf "%s\n" "$line" ;;
esac
done >> MSG-$msg
break
fi
msg=$(( $msg + 1 ))
done
#### EOS ####
Afer running the above script, any messages from the address in
$from will be stored in MSG-XXX, where XXX is the number of the
message (as it was on the server).
--
Chris F.A. Johnson http://cfaj.freeshell.org/shell
===================================================================
My code (if any) in this post is copyright 2004, Chris F.A. Johnson
and may be copied under the terms of the GNU General Public License