8

I want to send a HTML file as message body and want to attach multiple text files to this email message.

Since html file needs to be sent, sendmail has to be used ( I could not do it using mailx ).

How do you send HTML body email and multiple text attachments using sendmail?

Eric Leschinski
  • 146,994
  • 96
  • 417
  • 335
vaichidrewar
  • 9,251
  • 18
  • 72
  • 86

3 Answers3

9

Assuming you have uunecode available in your system you can send email with multiple attachments like this:

#!/bin/bash

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

{
   echo  "From: $MAILFROM"
   echo  "To: $MAILTO"
   echo  "Subject:" $SUBJECT
   echo  "MIME-Version: 1.0"
   echo  "Content-Type: MULTIPART/MIXED; "
   echo  "    BOUNDARY="\"$BOUNDARY\"
   echo
   echo  "        This message is in MIME format.  But if you can see this,"
   echo  "        you aren't using a MIME aware mail program.  You shouldn't "
   echo  "        have too many problems because this message is entirely in"
   echo  "        ASCII and is designed to be somewhat readable with old "
   echo  "        mail software."
   echo
   echo  "--${BOUNDARY}"
   echo  "Content-Type: TEXT/PLAIN; charset=US-ASCII"
   echo
   echo  "This email comes with multiple attachments."
   echo
   echo
   echo  "--${BOUNDARY}"
   echo  "Content-Type: application/zip; charset=US-ASCII; name="${ZIPFILE}
   echo  "Content-Disposition: attachment;   filename="`basename ${ZIPFILE}`
   echo
   uuencode $ZIPFILE $ZIPFILE
   echo
   echo  "--${BOUNDARY}--"
   echo  "Content-Type: application/pdf; charset=US-ASCII; name="${PDFFILE}
   echo  "Content-Disposition: attachment;   filename="`basename ${PDFFILE}`
   echo  
   uuencode $PDFFILE $PDFFILE
   echo
   echo  "--${BOUNDARY}--"
} | /usr/lib/sendmail -t
anubhava
  • 761,203
  • 64
  • 569
  • 643
  • The mail sent from above suggested method is able to send the file in attachment, but it is received as an encoded file in outlook windows mail client. How to send the file such that it is received as a decoded file as receiver's end? – greperror Jun 14 '17 at 08:46
0

Here is a bash script I use to send reports I generate to people. They are sent as attachments. Place your HTML in the "body" variable of the script. I will leave the parametrization of the variables up to you.

#!/bin/bash

function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}

from="me.last@company.com"
to="some.one@companyBlah.com"
subject="Your Report my Lord"
boundary="=== Boundary ==="
body="The reports are attached to this email"
declare -a attachments
attachments=( "fileOne.out" "fileTwo.out" "fileThree.out" "file-et-cetera.out")

# Build headers
{

printf '%s\n' "From: $from
To: $to
Subject: $subject
Mime-Version: 1.0
Content-Type: multipart/mixed; boundary=\"$boundary\"

--${boundary}
Content-Type: text/plain; charset=\"US-ASCII\"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline

$body
"

for file in "${attachments[@]}"; do

      [ ! -f "$file" ] && echo "Attachment $file not found, omitting file" >&2 && continue

        mimetype=$(get_mimetype "$file")

  printf '%s\n' "--${boundary}
Content-Type: $mimetype
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename=\"$file\"
  "

  base64 "$file"
  echo
done

# print last boundary with closing --
printf '%s\n' "--${boundary}--"

} | sendmail -t -oi
Colin MacKenzie - III
  • 1,373
  • 12
  • 13
0

I don't think sendmail is going to help you with that. Go for a client like mutt, and do e.g. mutt -a file1 -a file2 -- recipient@do.main. Or go for perl.

Volker Stolz
  • 7,274
  • 1
  • 32
  • 50
  • Not all systems will have the ability to install mutt, so your advice isn't very helpful to somebody who needs to use sendmail or mailx, as the question asked :) – stevepastelan Aug 06 '13 at 23:17
  • @stevepastelan Sendmail simply can't do it without additional tools. Which tools you choose is up to you of course. – Volker Stolz Aug 08 '13 at 10:49
  • 1
    But of course, sendmail *can* do it. It's just a question of how to format the contents to pass on to sendmail. – stevepastelan Aug 21 '13 at 13:47