sendmail with attachments

Hi,

I got the following script from Ygor on this site:

#!/usr/bin/ksh

export MAILTO="email_address"
export CONTENT="/export/home/aisdba/email_body.html"
export SUBJECT="subject of email"
(
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $CONTENT
) | /usr/sbin/sendmail $MAILTO

It works perfectly and means you don't need to use MIME to format the body of the email message you just use the HTML file defined in the CONTENT section above (its brilliant).

HOWEVER

I need to send the email with an attachment, now this is not the same file used for the body of the email rather a completely different file e.g. a txt file say. The following doesn't work:

#!/usr/bin/ksh

export MAILTO="email_address"
export CONTENT="/export/home/aisdba/email_body.html"
export SUBJECT="subject of email"
(
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $CONTENT
) | /usr/sbin/sendmail $MAILTO > /file_location/filename.txt

It just messes with the body of the email and causes HTML code to be displayed rather than interpreted by Outlook.

Any ideas on how to keep the HTML interpreted in the body of the email and send the email with an attachment?

Thanks
Alec

Take care - the script as posted would overwrite /file_location/filename.txt .
On my system the various Mime lines are surplus so I have removed them.

The construct below should cause the text file to be a proper attachment.

The full stop at the end of the message is assumed in some version of sendmail but there is no harm in including it.

On some unixes the text file conversion program is called unix2dos rather than ux2dos .

#!/usr/bin/ksh

export MAILTO="email_address"
export CONTENT="/export/home/aisdba/email_body.html"
export SUBJECT="subject of email"
(
echo "Subject: $SUBJECT"
# This appears in the mail body
cat $CONTENT
# The next line creates the attachment with a suitable extension to read
# with Windows notepad
ux2dos "/file_location/filename.txt" | uuencode myattach.txt
echo "."
) | /usr/sbin/sendmail $MAILTO

This is a script I have to send html mail with a pdf attachment...

#!/usr/bin/ksh

export MAILTO="spam@ebay.com"
export SUBJECT="Mail Subject"
export BODY="/tmp/email_body.html"
export ATTACH="/tmp/attachment.pdf"
(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo
 echo '---q1w2e3r4t5'
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode --base64 $ATTACH $(basename $ATTACH)
 echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO

I'm using GNU uuencode, hence the --base64, so you may need to change it.

1 Like

Thanks for both your replies. Ygor's reply is the one that seems to be working better for me, but the systems I use are very clunky so don't discount methyl's answer.

With Ygor's code I am now receiving an attachment, with the correct file name, however the file is empty, however I can see that the correct is on box, populated and ready to be attached.

I receive the following error at the end of my script run:

uuencode: illegal option -- base64

So I gather its the way I am encoding this attachment that is the problem. I don't know what exact uuencode I am using or the encoding options open to me, the actual uuencode file date is 2005. What other encoding options are there? My system is likely to use the most awkward one so lots of suggestions are welcome! Thanks

See if you have mimencode or similar: ls /usr/bin/*encode

Thanks Ygor, nothing brought up in the search apart from uuencode sadly. My usual command to attach files to an email is:

uuencode $CSV_FILE $(basename $CSV_FILE)
) | if [ $DATABASE_LIVE = 1 ]
then mailx -b "$COPY_TO" -s "$SUBJECT" -r "$MAIL_FROM" "$MAIL_TO_LIVE"
else mailx -s "$SUBJECT" -r "$MAIL_FROM" "$MAIL_TO_TEST"
fi

However I wanted have the body of the email in MIME or even better (as per your coding) a HTML file that I can construct within my shell script. This makes the body of the email look good (HTML formatting) but also relevant to UNIX Box and Database(s) (with Shell Script variables).

Is there a way to combine the script? Many thanks.

I don't think so. Either create a plain text message with uuencoded attachments (via mailx) or a MIME multi-part message with mimencoded attachments (via sendmail).

I have a play around with the script and the following works: (a fluke obviously!)

  1. HTML email body (in this case defined within the Shell Script)
  2. Multiple attachments (HTML Excel etc)
  3. All working correctly with no encoding problems or error messages!
export MAILTO="someone@somewhere.com"
export SUBJECT="$EMAIL_SUBJECT"
export BODY="$EMAIL_FILE"
export ATTACH="$HTML_FILE"
export ATTACH1="$EXCEL_FILE1"
export ATTACH2="$EXCEL_FILE2"
export ATTACH3="$EXCEL_FILE3"
(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
 echo
 echo '---q1w2e3r4t5'
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY 
 echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: uuencode" 
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 echo '---q1w2e3r4t5--'
 uuencode $ATTACH $(basename $ATTACH)
 echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH1)'"'
 echo "Content-Transfer-Encoding: uuencode" 
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH1)'"'
 echo '---q1w2e3r4t5--'
 uuencode $ATTACH1 $(basename $ATTACH1)
  echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH2)'"'
 echo "Content-Transfer-Encoding: uuencode" 
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH2)'"'
 echo '---q1w2e3r4t5--'
 uuencode $ATTACH2 $(basename $ATTACH2)
  echo '---q1w2e3r4t5'
 echo 'Content-Type: application; name="'$(basename $ATTACH3)'"'
 echo "Content-Transfer-Encoding: uuencode" 
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH3)'"'
 echo '---q1w2e3r4t5--'
 uuencode $ATTACH3 $(basename $ATTACH3) 
) | /usr/sbin/sendmail $MAILTO 

Thanks for your help Ygor, would never have got this without your code.

I'm trying to also embed an image to the email with no luck and wondering if you can help me, here is KSH i'm using:

export MAILTO=myemail@me.com
export FROM="me@me.com"
export SUBJECT="Test"
export BODY="index_image.htm"
ATTACH=$1
export ATTACH1="WirelessEmail1.jpg"
(
echo "To: $MAILTO"
echo "FROM: $FROM"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '---q1w2e3r4t5'
echo 'Content-Type: image/jpeg; filename="WirelessEmail1.jpg"'
echo "Content-Disposition: inline"
echo "Content-Transfer-Encoding: base64"
echo "Content-ID: <myimage.jpg>"
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: uuencode"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
echo '---q1w2e3r4t5--'
uuencode $ATTACH $(basename $ATTACH)
) | /usr/sbin/sendmail $MAILTO

I do receive an email with attached PDF but the image does not show in HTML body, the following shows up between the HTML body and the attached PDF file:
Content-Type: image/jpeg; filename="WirelessEmail1.jpg" Content-Disposition: inline Content-Transfer-Encoding: base64

I tried Google and other posts on this site but couldn't figure out how to embed this image, any help would be appreciated, thank you

Not sure what your $1 variable is. I would try:

export MAILTO=myemail@me.com
export FROM="me@me.com"
export SUBJECT="Test"
export BODY="index_image.htm"
export ATTACH="WirelessEmail1.jpg"
(
echo "To: $MAILTO"
echo "FROM: $FROM"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="-q1w2e3r4t5"'
echo
echo '---q1w2e3r4t5'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '---q1w2e3r4t5'
echo 'Content-Type: image/jpeg; filename="WirelessEmail1.jpg"'
echo "Content-Disposition: inline"
echo "Content-Transfer-Encoding: base64"
echo "Content-ID: <myimage.jpg>"
echo '---q1w2e3r4t5'
echo 'Content-Type: application; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: uuencode"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
echo '---q1w2e3r4t5--'
uuencode $ATTACH $(basename $ATTACH)
) | /usr/sbin/sendmail $MAILTO