Hi everyone,
I'm writing a script that connect to telnet and write some email taking the RCPT from a file. My problem is the fact I'm not able to put all the addresses I have in the file as subject for the "RCPT TO:"...
Can anyone please help me?
Thanks
ps: I wrote this right now
(
ADDRESSES=$(cat "/home/addresses")
sleep 1
echo "helo"
sleep 1
echo "mail from:<abc@foo.com>"
sleep 1
echo "rcpt to:$ADDRESSES"
sleep 10
echo "data"
sleep 1
echo "subject:Test message"
sleep 1
echo "from: <abc@foo.com>"
sleep 1
echo "to: $ADDRESSES"
sleep 1
echo " "
sleep 1
echo "Hello."
sleep 1
echo "This is a test message."
sleep 1
echo "Bye."
sleep 1
echo "."
sleep 1
echo "QUIT"
) | telnet xyz.xyz.xyz.zyx
The second step in the procedure is the RCPT command.
RCPT <SP> TO:<forward-path> <CRLF>
This command gives a forward-path identifying one recipient.
If accepted, the receiver-SMTP returns a 250 OK reply, and
stores the forward-path. If the recipient is unknown the
receiver-SMTP returns a 550 Failure reply. This second step of
the procedure can be repeated any number of times.
---------- Post updated at 01:22 PM ---------- Previous update was at 01:19 PM ----------
For a in $ADDRESSES
do
echo "rcpt to:$a"
sleep 10
done
This goes faster if:
you use 'expect'/'autoexpect', but it is a bit of a project to use it dynamically if you are not a tk/tcl guru yet, or
do your own 'expect': send the telnet output to a flat file and grep for responses in it every second or even less ( I wrote a 'nap' command in trivial C to sleep in milliseconds using poll(0,0,#) ). Of course, for n recipients, you need n+m 'grep -c' responses.
Here is one I wrote when the local mailx did not deliver:
$ cat mysrc/mailx4
#!/usr/bin/ksh
waitpat_e(){
export zt=0
if [ "$2" = "" ]
then
export zc=2
else
export zc=$2
fi
while (( $(tail -$zc </tmp/dpmail.$$|egrep -c "$1") < 1 ))
do
if (( $(fgrep -c 'Connection closed by foreign host.' </tmp/dpmail.$$) > 0 ))
then
exit
fi
zt=$(( $zt + 50 ))
if (( $zt > 10000 ))
then
return 1
fi
napx 50
if [ ! -f /tmp/dpmail.$$ ]
then
return 2
fi
done
return 0
}
waitpat(){
export zt=0
if [ "$2" = "" ]
then
export zc=1
else
export zc=$2
fi
while (( $(egrep -c "$1" </tmp/dpmail.$$) < $zc ))
do
if (( $(fgrep -c 'Connection closed by foreign host.' </tmp/dpmail.$$) > 0 ))
then
exit
fi
zt=$(( $zt + 50 ))
if (( $zt > 10000 ))
then
return 1
fi
napx 50
if [ ! -f /tmp/dpmail.$$ ]
then
return 2
fi
done
return 0
}
cd
. ./.profile
while [ $# != 0 ]
do
if [ "$1" = "-s" ]
then
export zsub="$2"
shift
shift
continue
fi
if [ "$1" = "-r" ]
then
export zsndr="$2"
shift
shift
continue
fi
zrec="$zrec $1"
shift
done
if [ "$zsub" = "" ]
then
export zsub="From idxx(real_idxx)@hostxxx, no subject"
fi
if [ "$zrec" = "" ]
then
zrec="David.Pickett@xxx.com"
fi
if [ "$zsndr" = "" ]
then
export zsndr="David.Pickett@xxx.com"
fi
>/tmp/dpmail.$$
(
waitpat '^220 '
echo "HELO"
waitpat '^250 '
echo "Mail From: $zsndr"
waitpat '^250 ' 2
export zct=3
for r in $zrec
do
echo "Rcpt To: $r"
waitpat '^250 ' $zct
zct=$(( $zct + 1 ))
zrecs="$zrecs ; $r"
done
"cho "Data
waitpat '^354 '
echo "From: $zsndr
To:${zrecs#*;}
Subject: $zsub
"
*\)$/. \1/'\(
echo "
."
waitpat_e '^250 '
echo "quit"
)|tcpipe -8E mail_host_ip_or_name 25 >/tmp/dpmail.$$ 2>&1
mv -f /tmp/dpmail.$$ /tmp/mailx4_last
$
Unfortunately I tried using the "for" as you suggested me but maybe im not able to let this work, because I have seen that in the $ADDRESSES variable there's nothing!
My main problem then, is to do a good assignement every time I am inside the "for loop" giving to $a variable the right value every time.
I guess you could modify the pattern checking to recognize when there is a "bad address", and if there is any good address, then proceed after reporting the "bad address" else abort.