sendmail in script.

SCO UnixWare 711
ksh

Hi Friends.

I am using sendmail. I can issue a sendmail request via the prompt but the same request will not work though a script it hangs after the first line of sendmail - can someone plese help.

Below is what hangs:

From Script:

$NUMBER has been declared.

/usr/lib/sendmail suresh_yadav@myaddress.com
from: root
to: queue_admins
subject: queue increasing
The queue is increasing currently at `expr ${NUMBER} -12`
.

I have run the script using set -vx and hangs at the following:

+ /usr/lib/sendmail suresh_yadav@myaddress.com

I would appreciate help on resolving why.

Thanks

Suresh

Hi Suresh,

I had the same problem. Now after some reconfig I re-wrote an excisting script. This is what I re-use for each program that needs to mail something.
Hope you understand it a little, it's a cool one.

Regs David

#!/bin/ksh

# We have to invoke a mailer that has no escape possibilities...
#
# The only one I know of would be sendmail, but this is very ugly
# to invoke directly because we have no control of To: and
# Subject: lines...
#
# This wrapper writes some of the Header-Fields before forwarding the
# mail itself in the body
#
# usage: start-mail mailaddr subject

# you might want to change the following line to set a sepcific from: address
#sender=`/usr/bin/id|/usr/bin/sed 's/.*(\(.*\)) .*/\1/'`
sender='root@'`/usr/bin/hostname`

# it's a bad idea to use temporary files with predictable names that are
# located in a world-writable /tmp dir because other local users may create
# sym-links pointing to another file...
#
# better use a tmp-dir that is only accessible by the logsurfer user
# change the following line!
# example: TMP=/home/logsurfer
TMP=/tmp

recepient="$1"
subject="$2"
message="$3"

if [ $# -eq 0 ]; then
	echo 'usage: start-mail mailaddr subject' 1>&2
	exit 1
fi

/bin/rm -f $TMP/start-mail.$$

echo "From: $sender" > $TMP/start-mail.$$
echo "To: $recepient" | /usr/bin/tr -cd '[\040-\176]' >> $TMP/start-mail.$$
echo "" >> $TMP/start-mail.$$
if [ $# -gt 1 ]; then
	shift
	echo "Subject: $subject" | /usr/bin/tr -cd '[\040-\176]' >> $TMP/start-mail.$$
	echo "" >> $TMP/start-mail.$$
else
	echo "Subject: (no subject given - logsurfer output)" >> $TMP/start-mail.$$
fi
echo "" >> $TMP/start-mail.$$
echo "$message" | /usr/bin/tr -cd '[\040-\176]' >>/$TMP/start-mail.$$
echo "\n." >>/$TMP/start-mail.$$


cat $TMP/start-mail.$$ - | /usr/lib/sendmail -odq -t

/bin/rm -f $TMP/start-mail.$$

added code tags for readability --oombera

just looking for some clarification.

is there a reason you dont use the commandline utility mail or mailx to send outbound mail?

does doing it your way offer something that the commandline utility dosent?

i dunno it just looks like alot of work could have been simplified.

Hi Optimus,

No, I just have this script in my path (mailform). Whenever I want to send an e-mail from inside a script or what ever I just type :

mailform "my@emailadres" "This is my Subject" "And this is my message. Just \
until I am finished \
Like now"

Yes, It's some work to set up, after that it is easier in work. Especialy inside scripts. f.e. when mailing inside an if-statement, the dot (.) still needs to be at the beginning of the line.
Also from and other setting could be added very easily like this.

Regs David

You could have used mailx

mailx -s "This is my Subject" "my@emailadres" << EOF
And this is my message. Just
until I am finished
Like now
EOF

Hi,

Thanks for your replies.

I used to use mail/mailx which I much prefer - however it stopped working when we changed our domain name and relocated our exchange server. If anyone can tell me where to look to configure mailx again - this would be a better solution as it would stop me having to rewrite all of my scripts.

Thanks

Suresh