# Sendmail with HTML body and attachment

**URL:** <https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875>\
**Category:** Shell Programming and Scripting\
**Created:** [May 11, 2011, 7:43am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875 "2011-05-11T07:43:38Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![atelford](https://community.unix.com/letter_avatar/atelford/32/6_449c533f586939b6e5f246632a46421f.png) [@atelford](https://community.unix.com/u/atelford)\
**Post date:** [May 11, 2011, 7:43am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/1 "2011-05-11T07:43:38Z")

</div>

I have an HTML file I am currently sending in the body of an email. I now have a need to send a csv attachment along with it. I can ONLY use sendmail as mutt and xmail etc are not on the server.

Here is what I am currently using: It is possible to add code to add an attachment ??!?

```nohighlight
{
       echo "From: $FROM"
       echo "To: $MAILTO"
       echo "Subject: Blah blah blah"
# echo "MIME-Version: 1.0"
# echo "Content-Type: text/html"
# echo "Content-Disposition: inline"
# cat myhtmlfile.html
#} | /usr/sbin/sendmail $MAILTO

```

My csv attachment sits in the same dir... e.g. myattachment.csv

---

<div class="post-metadata">

**Author:** ![palanisvr](https://community.unix.com/letter_avatar/palanisvr/32/6_449c533f586939b6e5f246632a46421f.png) [@palanisvr](https://community.unix.com/u/palanisvr)\
**Post date:** [May 11, 2011, 7:46am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/2 "2011-05-11T07:46:10Z")

</div>

##this will send three attachments

```nohighlight
SUBJ="Send mail from Unix with file attachments"
TO=someone@domain_name
CC=someoneelse_1@domain_name,someoneelse_2@domain_name
(
cat << !
To : ${TO}
Subject : ${SUBJ}
Cc : ${CC}
!

cat << !
HOPE THIS WORKS
This sample E-mail message demonstrates how one can attach
files when sending messages with the Unix sendmail utility.
!

uuencode ${file_1} ${file_1}
uuencode ${file_2} ${file_2}
uuencode ${file_3} ${file_3}
!

) | sendmail -v ${TO} ${CC} 

if still it is not working try this mailx :

(uuencode a.txt a.txt ; echo "this is body text " )| mailx -m -s a@yahoo.com

```

---

<div class="post-metadata">

**Author:** ![atelford](https://community.unix.com/letter_avatar/atelford/32/6_449c533f586939b6e5f246632a46421f.png) [@atelford](https://community.unix.com/u/atelford)\
**Post date:** [May 11, 2011, 7:51am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/3 "2011-05-11T07:51:06Z")

</div>

Thanks..... but the body of the email needs to be HTML and not txt.

---

<div class="post-metadata">

**Author:** ![palanisvr](https://community.unix.com/letter_avatar/palanisvr/32/6_449c533f586939b6e5f246632a46421f.png) [@palanisvr](https://community.unix.com/u/palanisvr)\
**Post date:** [May 11, 2011, 8:00am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/4 "2011-05-11T08:00:59Z")

</div>

```nohighlight
SUBJ="Send mail from Unix with file attachments"
TO=someone@domain_name
CC=someoneelse_1@domain_name,someoneelse_2@domain_name
(
cat << !
To : ${TO}
Subject : ${SUBJ}
Cc : ${CC}
!

cat << !
##this is body message
##you may paste the html body in here or use the below if the html body exist in a file
`cat body.html`

!

uuencode ${file_1} ${file_1}
uuencode ${file_2} ${file_2}
uuencode ${file_3} ${file_3}
!

) | sendmail -v ${TO} ${CC}

```

---

<div class="post-metadata">

**Author:** ![atelford](https://community.unix.com/letter_avatar/atelford/32/6_449c533f586939b6e5f246632a46421f.png) [@atelford](https://community.unix.com/u/atelford)\
**Post date:** [May 11, 2011, 8:04am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/5 "2011-05-11T08:04:30Z")

</div>

Thanks... Will try this.. Correct me if im wrong though but will this not set the body of the text to the source html e.g. \<html\>\<header\>This is html... etc etc ?

---

<div class="post-metadata">

**Author:** ![palanisvr](https://community.unix.com/letter_avatar/palanisvr/32/6_449c533f586939b6e5f246632a46421f.png) [@palanisvr](https://community.unix.com/u/palanisvr)\
**Post date:** [May 11, 2011, 8:07am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/6 "2011-05-11T08:07:46Z")

</div>

It should appear in new line.

---

<div class="post-metadata">

**Author:** ![atelford](https://community.unix.com/letter_avatar/atelford/32/6_449c533f586939b6e5f246632a46421f.png) [@atelford](https://community.unix.com/u/atelford)\
**Post date:** [May 11, 2011, 8:18am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/7 "2011-05-11T08:18:20Z")

</div>

Thanks for this..... but the HTML is displaying as text and not HTML... any idea ?

##this is body message  
##you may paste the html body in here or use the below if the html body exist in a file

MIME-Version: 1.0  
Content-Type: text/html

\<table\>\<td\>\<b\>\<u\>\<font .................

---

<div class="post-metadata">

**Author:** ![kashif\_cool1](https://community.unix.com/letter_avatar/kashif_cool1/32/6_449c533f586939b6e5f246632a46421f.png) [@kashif\_cool1](https://community.unix.com/u/kashif_cool1)\
**Post date:** [September 29, 2011, 10:15am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/8 "2011-09-29T10:15:22Z")

</div>

```nohighlight
BOUNDARY='=== This is the boundary between parts of the message. ==='
ADDRESS=whatever@whatever.com
ATTACHMENT=$FRAUD_DIR/reports/usageall.csv
BODY=$SPACE_MAIL
SUBJECT="whatever Report"
VERSION=1.0

print - "To: $TO <${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/HTML; charset=US-ASCII'
 print -
 cat $BODY
 print -
 print -
 print - "--${BOUNDARY}"
 print - 'Content-Type: TEXT/PLAIN; charset=US-ASCII; name='${ATTACHMENT}
 print - 'Content-Disposition: attachment; filename='whatever.csv
 print -
 cat ${ATTACHMENT}
 print -
 print - "--${BOUNDARY}--"
 } | /usr/lib/sendmail $ADDRESS

```

---

<div class="post-metadata">

**Author:** ![jayan\_jay](https://community.unix.com/user_avatar/community.unix.com/jayan_jay/32/1006_2.png) [@jayan\_jay](https://community.unix.com/u/jayan_jay)\
**Post date:** [September 30, 2011, 6:10am UTC](https://community.unix.com/t/sendmail-with-html-body-and-attachment/287875/9 "2011-09-30T06:10:15Z")

</div>

> **[html format email with attachment in unix](https://www.unix.com/shell-programming-and-scripting/165656-html-format-email-attachment-unix.html)**
>
> Team, I have the below code, which is working fine and it sends the html report using sendmail command. I want to attach one more file ( which goes as attachment ) in that email. How to achieve it. i | The UNIX and Linux Forums
