Think you're a good unix scripter?

Then peep this: How can you send an email with a file attachment from a KornShell script, or even a perl script?

Actually, I think I’m an awesome scripter. But this is the last free software you get.

I have tested this script under Solaris 2.6.

<BLOCKQUOTE><font size=“1” face=“Verdana, Arial”>code:</font><HR><pre>#! /usr/bin/ksh

quickmime — quick demo of how to send mail with mime attachments

Parameters:

-t recipient (required)

-a recipient’s address (required)

-A attachment (optional file name…only ascii files are supported)

-s subject (optional)

The script will read the body of the mail message from standard in.

example:

quickmime -t “Joe Blow” -a “jb@xyz.com” -A attach.txt -s “here it is”

Joe,

Here is that file.

<control d>

And in the above example attach.txt must be a file in current directory

process parameters

((error=0))
while getopts ‘:t:a:A:s:’ opt ; do
case $opt in
t)
TO=$OPTARG
;;
a)
ADDRESS=$OPTARG
;;
A)
ATTACH=$OPTARG
;;
s)
SUBJECT=OPTARG ;; \?) print -u2 what is -{OPTARG}?
((error=error+1))
;;
:slight_smile:
print -u2 $OPTARG need an argument
((error=error+1))
;;
esac
done

if [[ -z $TO ]] ; then
print -u2 “-t NAME is required”
((error=error+1))
fi

if [[ -z $ADDRESS ]] ; then
print -u2 “-a ADDRESS is required”
((error=error+1))
fi
if [[ ! -z $ATTACH ]] ; then
if [[ ! -f $ATTACH | | ! -r $ATTACH ]] ; then
print -u2 “-b $ATTACH is not a readable file”
((error=error+1))
fi
fi

if ((error)) ; then
print -u2 “error in parameter list…exiting”
exit 1
fi

Get the password file entry

If you’re using NIS or something, you will need to change this

pwentry=(grep "^(whoami):" /etc/passwd)

Break the fields up and store them in an array

((index=0))
while [[ pwentry = *:* ]] ; do pwfield[index]={pwentry%%{pwentry##*([!:])}} pwentry={pwentry##*([!:]):}
((index=index+1))
done
pwfield[index]=${pwentry}

Set the mime boundary

BOUNDARY=’=== This is the boundary between parts of the message. ===’

Build the mail message

MYNAME={pwfield[4]%%,*} MYADDR={pwfield[0]}
{
print - “From: MYNAME &lt;{MYADDR}>”
print - “To: TO &lt;{ADDRESS}>”
if [[ -n $SUBJECT ]] ; then
print - ‘Subject:’ $SUBJECT
fi
print - ‘MIME-Version: 1.0’
print - ‘Content-Type: MULTIPART/MIXED; ’
print - ’ BOUNDARY=’"$BOUNDARY"
print -
print - ’ This message is in MIME format. But if you can see this,’
print - " you aren’t using a MIME aware mail program. You shouldn’t "
print - ’ have too many problems because this message is entirely in’
print - ’ ASCII and is designed to be somewhat readable with old ’
print - ’ mail software.’

print -
print - “–{BOUNDARY}" print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII' print - cat # here is where the body of the mail message is read print - print - print - "--{BOUNDARY}”
if [[ ! -z $ATTACH ]] ; then
echo Adding attachment ATTACH &gt; /dev/tty print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name='{ATTACH}
print -
cat ATTACH print - print - "--{BOUNDARY}–"
fi
} | /usr/lib/sendmail $ADDRESS

exit 0


Now that was funny! This time I have disabled smilies. But gosh that code construct should imply no smilies.
<BLOCKQUOTE><font size=“1” face=“Verdana, Arial”>code:</font><HR><pre>#! /usr/bin/ksh

quickmime — quick demo of how to send mail with mime attachments

Parameters:

-t recipient (required)

-a recipient’s address (required)

-A attachment (optional file name…only ascii files are supported)

-s subject (optional)

The script will read the body of the mail message from standard in.

example:

quickmime -t “Joe Blow” -a “jb@xyz.com” -A attach.txt -s “here it is”

Joe,

Here is that file.

<control d>

And in the above example attach.txt must be a file in current directory

process parameters

((error=0))
while getopts ‘:t:a:A:s:’ opt ; do
case $opt in
t)
TO=$OPTARG
;;
a)
ADDRESS=$OPTARG
;;
A)
ATTACH=$OPTARG
;;
s)
SUBJECT=OPTARG ;; \?) print -u2 what is -{OPTARG}?
((error=error+1))
;;
:slight_smile:
print -u2 $OPTARG need an argument
((error=error+1))
;;
esac
done

if [[ -z $TO ]] ; then
print -u2 “-t NAME is required”
((error=error+1))
fi

if [[ -z $ADDRESS ]] ; then
print -u2 “-a ADDRESS is required”
((error=error+1))
fi
if [[ ! -z $ATTACH ]] ; then
if [[ ! -f $ATTACH | | ! -r $ATTACH ]] ; then
print -u2 “-b $ATTACH is not a readable file”
((error=error+1))
fi
fi

if ((error)) ; then
print -u2 “error in parameter list…exiting”
exit 1
fi

Get the password file entry

If you’re using NIS or something, you will need to change this

pwentry=(grep "^(whoami):" /etc/passwd)

Break the fields up and store them in an array

((index=0))
while [[ pwentry = *:* ]] ; do pwfield[index]={pwentry%%{pwentry##*([!:])}} pwentry={pwentry##*([!:]):}
((index=index+1))
done
pwfield[index]=${pwentry}

Set the mime boundary

BOUNDARY=’=== This is the boundary between parts of the message. ===’

Build the mail message

MYNAME={pwfield[4]%%,*} MYADDR={pwfield[0]}
{
print - “From: MYNAME &lt;{MYADDR}>”
print - “To: TO &lt;{ADDRESS}>”
if [[ -n $SUBJECT ]] ; then
print - ‘Subject:’ $SUBJECT
fi
print - ‘MIME-Version: 1.0’
print - ‘Content-Type: MULTIPART/MIXED; ’
print - ’ BOUNDARY=’"$BOUNDARY"
print -
print - ’ This message is in MIME format. But if you can see this,’
print - " you aren’t using a MIME aware mail program. You shouldn’t "
print - ’ have too many problems because this message is entirely in’
print - ’ ASCII and is designed to be somewhat readable with old ’
print - ’ mail software.’

print -
print - “–{BOUNDARY}" print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII' print - cat # here is where the body of the mail message is read print - print - print - "--{BOUNDARY}”
if [[ ! -z $ATTACH ]] ; then
echo Adding attachment ATTACH &gt; /dev/tty print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name='{ATTACH}
print -
cat ATTACH print - print - "--{BOUNDARY}–"
fi
} | /usr/lib/sendmail $ADDRESS

exit 0