0

I have to email an HTML file in the body of an email from a UNIX machine. Let us call the HTML file as hello.html

I am using the following code in bash script,

(
echo "To: xyz@gmail.com" 
echo "Subject: Report" 
echo "Content-Type: text/html" 
echo
cat hello.html
echo 
) | /usr/sbin/sendmail -t

The above code works perfectly fine for me. Now I also want to attach a zip file to this email. Is there any way to do that?

I am limited to using only mail, mailx and /usr/sbin/sendmail.

3

I suggest you send yourself an actual email with zip file attachment and look at the raw contents.

What you'll find is that the email will be formatted per the MIME standard which specifies how to include various file formats in a textual email.

In your case the top-level Content-type will be something like multipart/alternative or multipart/mixed. Within that will be two or more parts or sections with their own Content-type. One will have type application/zip. The application/zip section will have a text encoding (e.g. Base 64) of the zip file.

Here's a sample:

From: John Doe <example@example.com>
MIME-Version: 1.0
Content-Type: multipart/mixed;
        boundary="XXXXboundary text"

This is a multipart message in MIME format.

--XXXXboundary text 
Content-Type: text/plain

this is the body text


--XXXXboundary text 
Content-Type: text/html

<html>
  ...Body of email in HTML format...
</html>

--XXXXboundary text 
Content-Type: application/zip;
Content-Transfer-Encoding: base64
Content-Disposition: attachment;
        filename="test.zip"

BASE64 ENCODING TEXT HERE

--XXXXboundary text--

Some more information: Email File Attachments Using MIME

Back to the root of your question, how to do this? I'm not familiar with any standalone tools that help construct an email like this but I'd be surprised if you couldn't find something useful. Perhaps one of the email clients supports command-line/bulk operations. But if worse comes to worst, it's all just ASCII text so you can easily construct it manually along the lines of what you're doing with your HTML email. But I suggest you build the message in a temporary file before piping it to your mail agent. (Look into "here documents" for easy saving of large amounts of text.)

And as indicated, you will need to convert your zip file to Base 64. Just about every Linux installation should have the base64 utility or equivalent. Just pass your zip to it and append to your email file at the appropriate time...

base64 test.zip >> my-email-file.txt
0

#!/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

( or ) as an inline mail you can send.

cat <<'EOF' - CH_ARG.html | /usr/sbin/sendmail $TARGET_DL
Subject: your subject line
Content-Type: text/html
MIME-Version: 1.0
Content-Disposition: inline
EOF

( or ) try below for attachment

cat <<'EOF' - CH_ARG.html | /usr/sbin/sendmail $TARGET_DL
Subject: Your subject line
Content-Type: text/text
MIME-Version: 1.0
Content-Disposition: attachment
EOF

  • 1
    How does this add an attachement? – nohillside Jan 7 '18 at 21:31
  • @patrix i have appended my answer – ARGStackOvaFlo Jan 12 '18 at 10:15
  • I still don‘t see how the zip attachment gets added. The question is about adding a zip, not about sending a html Mail as such. – nohillside Jan 12 '18 at 12:55

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.