Email with multiple attachments & HTML body

I have a html file:

# cat sample.html
<html>
<body>
Sample HTML file</p>
</body>
</html>

And I have two excel sheets (sheet1.xls & sheet2.xls)
I want to send an email by having the sample.html as the message body and two spreadsheets as the attachments.
I tried using the below command:

(cat sample.html; uuencode sheet1.xls sheet1.xls; uuencode sheet2.xls sheet2.xls) | mailx -m -s "Test Mail 
Content-type: text/html" "recipient address"

We are getting the message body, but the attachments are missing in the email. If I remove the "Content-type: text/html", then I am getting both attachment and the message body, but the message body is coming with html tags and they are actually not getting parsed.

Please provide me a solution.

WIKIPEDIA has an excellent article on MIME messages, it is a good place to start. See MIME - Wikipedia, the free encyclopedia.

This was answered just a little while ago...search the forums.
Short answer: you can't get attachments and HTML with sendmail.
I believe the solution was to use the mutt email client.

You can use sendmail to send html message body with attachments...

I was looking for the similar kind of example without any attachments. How do I go with this on AIX.

I removed the related ATTACH code and also tried with
uuencode --base64
(and )
uuencode ${1} ${1}

but it was hang, appreciate your help.

You just want to send an HTML email (and you use AIX)?

1 Like

Thanks .. I could able to do it.. So much of good information .. keep sharing..

I'm new to UNIX and need some assistance in creating/sending an email containing HTML body and a .csv attachment. I've found
numerous posts on the forum that discuss this, however none of the solutions work. I've created a KSH script running on UNIX AIX.
I found a post with a solution that works on a linux box which has uuencode --base64, but it did not work for me. The code ran
but did not send the email.
Any suggestions......

My .html file looks something like this:

# cat email.html
<html>
<body>
Sample HTML file</p>
</body>
</html>

I'm able to send the html formatted email with no attachments using the following code:

cat email.html \
| /usr/lib/sendmail -t "someone@email.com"

When I update the code to include the attachement as follows, the email is sent with the attachment but the email body is missing:

echo "To: $p_email\nSubject: $month $year Subject\n" > $mailfile
(cat email.html; \
uuencode ${ofile1} ${ofile1} >> $mailfile) \
| /usr/lib/sendmail -t < ${mailfile}

I developed this on AIX:

cat html_email_with_attachments.body

<html>
<body>
Body of email.
</body>
</html>

cat html_email_with_attachments

#!/usr/bin/ksh

# http://en.wikipedia.org/wiki/MIME

export MAILTO=abc@abc.com
export SUBJECT=$0
export BODY=html_email_with_attachments.body
export ATTACH=cards.txt
EMAIL_BOUNDARY_STRING=Z${RANDOM}${RANDOM}${RANDOM}${RANDOM}

(
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo 'Content-Type: multipart/mixed; boundary="${EMAIL_BOUNDARY_STRING}"'
 echo '--${EMAIL_BOUNDARY_STRING}'

 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo '--${EMAIL_BOUNDARY_STRING}'

 echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo '--${EMAIL_BOUNDARY_STRING}'

 echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo '--${EMAIL_BOUNDARY_STRING}--'

) | /usr/sbin/sendmail $MAILTO

then:

./html_email_with_attachments

Thanks, that works and the email is sent with the attachment and the formatted HTML. The only issue is I'm getting 2 errors as follows:

-m: No such file or directory
-m: No such file or directory

When I man uuencode I don't see the -m option. When I remove this from the code, the email is sent with 2 attachments (both the same). When I remove the last block of code in your example, the email is sent with 2 attachments, the second file is an empty notepad .txt file. Can you explain this?

First, you are running AIX? on 5.3:

Syntax

       uuencode [ -m ] [ SourceFile ] OutputFile

Description

       The uuencode command converts a binary file to ASCII data. This is useful before using BNU (or uucp) mail to send the file to a remote
       system. The uudecode command converts ASCII data created by the uuencode command back into its original binary form.

       The uuencode command takes the named SourceFile (default standard input) and produces an encoded version on the standard output. The encoding
       uses only printable ASCII characters, and includes the mode of the file and the OutputFile filename used for recreation of the binary image
       on the remote system.

       Use the uudecode command to decode the file.

Flags
       -m
            Encode the output using the MIME Base64 algorithm. If -m is not specified, the old uuencode algorithm will be used.

Second, I wrote the code as:

 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo '--${EMAIL_BOUNDARY_STRING}'
 echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo '--${EMAIL_BOUNDARY_STRING}'
 echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo '--${EMAIL_BOUNDARY_STRING}--'

To show how different 'blocks' or things can be included in the email. I believe this format can be repeated many times.

Notice the last:

echo '--${EMAIL_BOUNDARY_STRING}--'

is slightly different. The trailing -- specifies the end of the email.

If you post your code and the output we can look at it.

Thank you. it is cognitively

I'm runing on HP-UX B.11.11

I used your code exactly the way you wrote it only changing the values in the export statment for the various variables (ie email address, html body, attachment & subject). On the fist run I received the email with the html body and the attachment but received the errors regarding the -m flag.

On a subsequent run, I removed the -m flag from both blocks and received the email with the html body but to copies of my $ATTACH file were attached. The code is as follows:

EMAIL_BOUNDARY_STRING=Z${RANDOM}${RANDOM}${RANDOM}${RANDOM}
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="${EMAIL_BOUNDARY_STRING}"'
echo '--${EMAIL_BOUNDARY_STRING}'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '--${EMAIL_BOUNDARY_STRING}'
echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode $ATTACH $(basename $ATTACH)
echo '--${EMAIL_BOUNDARY_STRING}'
echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode $ATTACH $(basename $ATTACH)
echo '--${EMAIL_BOUNDARY_STRING}--'
) | /usr/sbin/sendmail $MAILTO 

On the last run, I removed the 2nd block which references the $ATTACH file and this time I received the email with the html body, my $ATTACH file and a 2nd attached file that was an empty .txt file. The code is as follows:

EMAIL_BOUNDARY_STRING=Z${RANDOM}${RANDOM}${RANDOM}${RANDOM}
(
echo "To: $MAILTO"
echo "Subject: $SUBJECT"
echo "MIME-Version: 1.0"
echo 'Content-Type: multipart/mixed; boundary="${EMAIL_BOUNDARY_STRING}"'
echo '--${EMAIL_BOUNDARY_STRING}'
echo "Content-Type: text/html"
echo "Content-Disposition: inline"
cat $BODY
echo '--${EMAIL_BOUNDARY_STRING}'
echo 'Content-Type: application/octet-stream; name="'$(basename $ATTACH)'"'
echo "Content-Transfer-Encoding: base64"
echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
uuencode $ATTACH $(basename $ATTACH)
echo '--${EMAIL_BOUNDARY_STRING}'
) | /usr/sbin/sendmail $MAILTO