sending email from KSH unix script.


 
Thread Tools Search this Thread
Top Forums Shell Programming and Scripting sending email from KSH unix script.
# 1  
Old 07-22-2005
sending email from KSH unix script.

Hi

Need guidance on including code to mail a couple of files atached with some subject and mail body !!..

Thanks in advance
# 2  
Old 07-22-2005
See this link - hint: use mail attachment subject
# 3  
Old 07-26-2005
set +x
#
# jail -> shell script for sending mime email enclosures from command line
#
# Usage : jail [options] email-address file-to-be-sent
# $1 $2
#
# John Roebuck - 11/08/97
#
# John Roebuck - 29/01/98 - Add options to allow optional subject and urgent.
# John Roebuck - 18/03/98 - Get Hostname of machine and user.
# John Roebuck - 26/05/98 - Allow multiple files to be sent.
# John Roebuck - 11/12/98 - Remove all MTMS options and requirements,
# also added -v for vebose (debug) sendmail option.
# John Roebuck - 17/12/98 - Added -b option of uuencoding binary files.
# John Roebuck - 22/12/98 - Added -r option of delivery receipt required.
# John Roebuck - 16/06/00 - Modified to work on SCO OSR5 korn shell.

version="jail for AIX & SCO OSR5 1.5 by John Roebuck 16/06/2000"

#
# Get command line options
#
bflag=0
rflag=0
sflag=0
uflag=0
vflag=0

while getopts brs:uv name
do
case $name in
b) bflag=1;;
r) rflag=1;;
s) sflag=1
sval="$OPTARG";;
u) uflag=1;;
v) vflag=1;;
#
# Display syntax if ? passed as an option in the command line
#
?) echo " "
echo "Usage : jail [options] email-address files-to-be-set"
echo " "
echo " options are -b for binary files"
echo " -r delivery receipt required"
echo " -s (subject of mail message)"
echo " -u Urgent"
echo " -v Verbose sendmail (debug)"
echo " ("$version")"
echo " "
exit 2;;
esac
done

#
# Set urgent flag to 1 if urgent option is found
#
if [ $uflag = 1 ]; then urgent="urgent"
else urgent="normal"
fi

#
# Include subject from command line if subject option is found
#
if [ $sflag = 1 ]; then subject=$sval
else subject=" "
fi

shift $(($OPTIND -1))

#
# Display syntax if less than 2 command line parameters are found
# after the command line options. Require email address and 1 file
# as a minimum.
#
if [ $# -lt 2 ]
then echo " "
echo "Usage : jail [options] email-address files-to-be-set"
echo " "
echo " options are -b for binary files"
echo " -r delivery receipt required"
echo " -s (subject of mail message)"
echo " -u Urgent"
echo " -v Verbose sendmail (debug)"
echo " ("$version")"
echo " "
exit
fi

#
# set -f stops * being expanded within the shell script. (ksh option)
#
set -f

#
# Now get the send to email address
#
sendto=$1
shift

#
# Find out how many files to send as enclosures in this email message
#
attno=$#
if [ $attno = 1 ]; then attmess="enclosure"
else attmess="enclosures"
fi

#
# Format the variable $now as "Friday 18 December 1998 16:22"
#
xday=`date +%a`
xdayno=`date +%d`
xmonth=`date +%b`
xyear=`date +%Y`
xtime=`date +%H:%M:%S`
now=$xday", "$xdayno" "$xmonth" "$xyear" "$xtime

#
# Create message boundary number based on process id
#
boundary=JPR$$

#
# Create message id number based on date and process id
#
messid=`date +%d%m%y`$$

#
# Create temporary file based on date time and process id
#
tempfile=/tmp/jail-`date +%d%m%y%H%M%S`$$
touch $tempfile

#
# Get sender details eg user name and description, machine hostname
#
# sender login is the login name of the current user account
# Use who am i as whoami does not work under SCO OSR5.
#
senderlogin=`who am i | cut -f1 -d" "`
#
# Domain is taken from the domain field in /etc/resolv.conf file
#
senderdomain=`grep domain /etc/resolv.conf | cut -f2 -d" "`
#
# hostname is taken from the output of the hostname command. hostname
# may also contain domain so cut first field using . as the deliminator
# to get rid of the domain.
#
senderhost=`hostname | cut -d"." -f1`
#
# Sender address is login name @ machine hostname . domain
#
senderaddress=$senderlogin"@"$senderhost"."$senderdomain
#
# Sender name is taken from the login name description field in /etc/passwd
#
sendername=`grep $senderlogin /etc/passwd |cut -d":" -f5`

#
# Create header part of email file
#

echo "From: ""$sendername"" <"$senderaddress">" >> $tempfile
echo "To: "$sendto >> $tempfile
echo "Date: "$now >> $tempfile
echo "Mime-Version: 1.0 "$version >> $tempfile
echo "Content-Type: Multipart/Mixed; boundary=Message-Boundary-"$boundary >> $tempfile
echo "Subject: "$subject>> $tempfile

#
# Is a delivery receipt required ?
#
if [ $rflag = 1 ]; then
echo "Return-Receipt-To: ""$sendername"" <"$senderaddress">" >> $tempfile
fi

echo "Priority: "$urgent >> $tempfile
echo "Message-Id: <"$messid"."$senderdomain >> $tempfile
echo "Status: RO" >> $tempfile
echo "" >> $tempfile
echo "" >> $tempfile
echo "--Message-Boundary-"$boundary >> $tempfile
echo "Content-type: text/plain; charset=US-ASCII" >> $tempfile
echo "Content-transfer-encoding: 7BIT" >> $tempfile
echo "Content-description: Read Me First" >> $tempfile
echo "" >> $tempfile
echo "" >> $tempfile

#
# Create mail message body part of email file
#

echo "Hello" >> $tempfile
echo "" >> $tempfile
echo "Please find "$attno" '"$attmess"' to this email message :- " >> $tempfile
echo "" >> $tempfile

#
# Generate file information for each enclosure. The information is in the
# format : File name 1 info.doc
# Produced on 16 Dec at 18:59
# File size 33469 bytes.
#
messno=0
while [ $attno -gt $messno ]
do

messno=`expr $messno + 1`
filename[$messno]=$1
shift

report1="File name "$messno" "${filename[$messno]}
report2=`ls -l "${filename[$messno]}" |awk '{print "" "Produced on "$6 " " $7 " at " $8 "" }'`
report3=`ls -l "${filename[$messno]}" |awk '{print "" "File size "$5" bytes." "" }'`

echo "$report1" >> $tempfile
echo "$report2" >> $tempfile
echo "$report3" >> $tempfile

echo "" >> $tempfile

done

echo "" >> $tempfile
echo "" >> $tempfile
echo "Regards "$sendername" ("$senderaddress")" >> $tempfile
echo "" >> $tempfile

#
# For each file, create enclosure for the file.
#
messno=0
while [ $attno -gt $messno ]
do

messno=`expr $messno + 1`

#
# Check from command line if files are ascii or binary
#
if [ $bflag = 0 ]; then

#
# Add extra chr$ to ascii files for unix to dos conversion. This
# means reading in each line of the text file and writing it out to
# the temp file with a charage return character added to the end of
# each line.
# Unforntunately ksh read will ignore leading spaces at the beginning
# of each line.
#
echo "--Message-Boundary-"$boundary >> $tempfile
echo "Content-type: Application/Octet-Stream; name=${filename[$messno]}; type=Text" >> $tempfile
echo "Content-description: attachment; filename=${filename[$messno]}" >> $tempfile
echo "" >> $tempfile

{
while read line_data
do
echo "$line_data""
" >> $tempfile
done } < ${filename[$messno]}

else

#
# uuencode binary mail
#
echo "--Message-Boundary-"$boundary >> $tempfile
echo "Content-type: Application/Octet-stream; name=${filename[$messno]}; type=Binary" >> $tempfile
echo "Content-disposition: attachment; filename=${filename[$messno]}" >> $tempfile
echo "Content-transfer-encoding: X-UUencode" >> $tempfile
echo "" >> $tempfile

uuencode ${filename[$messno]} ${filename[$messno]} >> $tempfile

fi
done

#
# Send email message straight to sendmail. Use -v option if debug
# option has been set at the command line.
#
if [ $vflag = 1 ]; then /usr/lib/sendmail -v $sendto < $tempfile
else /usr/lib/sendmail $sendto < $tempfile
fi

#
# Remove temp file
#
rm $tempfile
# 4  
Old 07-26-2005
Computer

or you can just use this

uuencode input_file output_file | sendmail recipient(s)
uuencode input_file output_file | mail -s subject recipient(s)
uuencode input_file output_file | mailx -s subject recipient(s)
Login or Register to Ask a Question

Previous Thread | Next Thread

10 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

Sending email in UNIX

can you tell me what is the syntax if I need to keep someone in cc with the below email. mailx -s "shell script completed successfully" arun@gmail.com < /dev/null (4 Replies)
Discussion started by: ramkumar15
4 Replies

2. UNIX for Advanced & Expert Users

Sending email from UNIX server

Hi, I am trying to send an output of a script in an email. I want to know if there is a way to modify the senders email not as my username on the host server but as one of the sender preset on outlook. I was using cat email.log | /bin/mailx -c cclist -r myemailid -s "subject" tolistids ... (4 Replies)
Discussion started by: gopajitmalakar
4 Replies

3. UNIX for Dummies Questions & Answers

Sending formatted email from unix.

Hi All, I need to send email from unix in the below format. But the email body is coming all in one single line. How do i format the below text, so that the body of email is properly formatted. Please help. TO: <email id> CC: <none> BCC: <none> Subject: Support: file GENERATION is... (3 Replies)
Discussion started by: abhi_123
3 Replies

4. Shell Programming and Scripting

Sending email from a unix program

Hi, I have create a unix prog file to validate data and send out an email. Below is the command used to send out email in the program. But it is not triggering the email. Please advice on this. (echo ${MESSAGE};uuencode "$FCP_OUT" "ERROR_REPORT.csv"; uuencode "$VALIDATION_RPT"... (1 Reply)
Discussion started by: contactsmrajesh
1 Replies

5. Shell Programming and Scripting

Sending email from Unix

I am using the following syntax to send an e-mail from Unix (Sun OS). mail -s "hello" abc@yahoo.com But, when I click enter after typing this command, its not exiting and after a while when I give ctrl+c it creates a dead.letter file in my $HOME dir. Can anyone please tell me, if I have to... (7 Replies)
Discussion started by: rajesh8s
7 Replies

6. HP-UX

Sending Unix files as attachments in an email

Hi, I am executing the following command in order to send a file as an attachment: mailx -s "Subject" emailID@xyz.com < Testfile.txt Instead of attaching the file Testfile.txt, it is writing the contents of the file in the email message body. Please advise on how I can send the file as an... (7 Replies)
Discussion started by: sangharsh
7 Replies

7. Shell Programming and Scripting

sending email from a ksh script

hi all, i have a ksh script which is meant to send an email with an attachment. i use the following command to send email /usr/bin/uuencode $logFn $logFn | /usr/bin/mail -s "restoration results" $EMAILTO; where '$logFn' is the name of the file including the full path e.g... (0 Replies)
Discussion started by: cesarNZ
0 Replies

8. BSD

help request for sending email from unix to internet

I have a requirement to send email to a group once I have done some tests. I tried the following from command line:- mailx -s "Test REsults for today " myname@companyname.com <testresultfile.txt but the email was not received by anyone basically I followed the format: mailx -s... (1 Reply)
Discussion started by: sbabuts
1 Replies

9. Shell Programming and Scripting

sending email in unix. need help!

how can I send an email in UNIX, attach a file and cc a receipient? i use the command below to attach a file (this works): uuencode path/filename filename | mailx -s "subject" addr@host.com here is how i cc a receipient (this also works): i have a file named cc.lis and contains: Hello... (2 Replies)
Discussion started by: tads98
2 Replies

10. Shell Programming and Scripting

unix - c program sending error from DB to email

hi, i have a database table that automatically logs the errors in the database every 5 mins. what i want to happen is to come up with a program using unix-c that gets all the information from the log table, i have a flag initially set to zero which means that the error is not yet sent, the... (2 Replies)
Discussion started by: chino_52284
2 Replies
Login or Register to Ask a Question