0

I have written a script which makes a html table in mail body , I want to send a file/zip as attachment using mail command and uuencode but somehow its not working inside bash script but works from console .

mailbody="<html><table><th><tr><td>Name</td><td>Age</td></tr></th><tr><td>\
           ABC</td><td>25</td></tr></table></html>"
echo $mailbody>>mailer.txt
#assume i have test.txt or test.zip which i need as attachment in mail
#i tried below with html part as body
uuencode test.txt test.txt|mail -s "Test mail\nContent-Type: text/html" "xyz.com" <mailer.txt

this sends mail with mail body showing html Table but attachment is missing .

uuencode test.txt test.txt|mail -s "Test mail" "xyz.com"  properly send mail with test.txt as attachment but a plain mail , no html

thanks in advance

innova
  • 31
  • 5
  • 1
    Take a look at http://stackoverflow.com/a/902628/41747 for a good alternative if you cannot use `mutt` or MetaMail. – D.Shawley Mar 10 '15 at 01:36
  • Possible duplicate of [How do I send a file as an email attachment using Linux command line?](https://stackoverflow.com/q/17359/608639) – jww Dec 23 '19 at 20:59

2 Answers2

1

This is a fairly common question but it is hard to answer generally because there is no one standard mail command. Some support what you are trying; most don't.

Seeing as you are having trouble, probably yours belongs to the group which doesn't support MIME, but do check its manual page for promising options.

Traditionally, e-mail is text only, 7-bit US-ASCII, and there is no such thing as "attachments". Back in the 1980s and sometime into the 1990s, if you wanted to send somebody a file, you embedded a slab of uuencode gobbledygook into the text of the email message, and hoped the recipient would know what to do with it.

Enter MIME. The modern email is a structured being, where each message has one or multiple MIME parts, each with a content type and an encoding. An "attachment" isn't properly well-defined, but commonly, a MIME part which cannot be displayed inline is shown as a clickable link by your email client.

In order to send HTML messages with attachments in the modern world, you need a MIME-aware mail client for sending messages. Looks like your mail doesn't qualify. A common workaround (nearly so common as to be a de facto standard) is to install and use mutt instead, but there are probably lighter alternatives if you are on a reasonably modern platform.

tripleee
  • 175,061
  • 34
  • 275
  • 318
  • I changed line to ( cat mailer.txt;uuencode test.gz test.gz) | mail -s "$subject""$(echo -e "\nContent-Type: text/html\n Content-Encoding: gzip")" ${myToAddr} so it send html table in body and attachment but attachment is corrupted i have mutt which i can use and i tried mutt -s "subject Content-Type: text/html" -a test.gz xyz.com but seems mime type needs to be fixed to cater to html and gz – innova Mar 10 '15 at 06:54
  • Using a fundamentally MIME-unaware mail client to try to send MIME messages (and guessing wrong about some parts) is simply not going to work. It would be interesting to see what you managed to pull off when you did get it to work, but the sustainable solution is to switch to properly MIME-aware tools. `uuencode` is not MIME and claiming it is (by putting in MIME headers to claim it is valid MIME gzip data) will simply not work. – tripleee Mar 10 '15 at 07:03
  • (Well, in fact, `Content-Encoding:` is not actually a valid MIME header name, either.) – tripleee Mar 10 '15 at 07:05
  • If you really want to do it completely by hand, http://stackoverflow.com/a/902628/874188 basically shows you how. You would change the first inline part's content type to `text/html` instead of `text/plain` and put your HTML in the body part, where the linked example has "This email has attached the file". – tripleee Mar 10 '15 at 07:08
1
USE this :

#!/usr/bin/ksh

export MAILTO="spam@ebay.com"
export SUBJECT="Mail Subject"
export BODY="/tmp/email_body.html"
export ATTACH="/tmp/attachment.txt"
(
 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 -m $ATTACH $(basename $ATTACH)
 echo '---q1w2e3r4t5--'
) | /usr/sbin/sendmail $MAILTO