简体   繁体   中英

mail command from bash script not sending attachment when using uuencode , mail body is html file

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

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM