# Mailx with attachment and html content

**URL:** <https://community.unix.com/t/mailx-with-attachment-and-html-content/368322>\
**Category:** Shell Programming and Scripting\
**Created:** [September 15, 2017, 5:18am UTC](https://community.unix.com/t/mailx-with-attachment-and-html-content/368322 "2017-09-15T05:18:03Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![bhoshan](https://community.unix.com/letter_avatar/bhoshan/32/6_449c533f586939b6e5f246632a46421f.png) [@bhoshan](https://community.unix.com/u/bhoshan)\
**Post date:** [September 15, 2017, 5:18am UTC](https://community.unix.com/t/mailx-with-attachment-and-html-content/368322/1 "2017-09-15T05:18:03Z")

</div>

Hi, Please see my code below i'm trying get an email send with attachment and html content in the body.

Using the code below will put the encoding for attachment in the body as well

```nohighlight
SUBJECT="$(echo "XPI Monitoring "${tcnt}" transactions waiting \nContent-Type: text/html")"
            cat $RT_DIR/${RPT_DNAME[2]}/${XPILOG} > $TEMP_FILE
            uuencode $RT_DIR/${RPT_DNAME[2]}/${XPILOG} "xpimon.csv" >> $TEMP_FILE
            mailx -s "$SUBJECT" "$mailist" < $TEMP_FILE

```

Please shower some light if possible of what i'm doing wrong  
Thanks

---

<div class="post-metadata">

**Author:** ![jgt](https://community.unix.com/user_avatar/community.unix.com/jgt/32/10703_2.png) [@jgt](https://community.unix.com/u/jgt)\
**Post date:** [September 15, 2017, 8:45am UTC](https://community.unix.com/t/mailx-with-attachment-and-html-content/368322/2 "2017-09-15T08:45:53Z")

</div>

Save and examine the contents of temp\_file. There are required blank lines close to the "content\_type" line.  
If need be, post the contents of temp\_file, obscuring anything identifiable.

---

<div class="post-metadata">

**Author:** ![bhoshan](https://community.unix.com/letter_avatar/bhoshan/32/6_449c533f586939b6e5f246632a46421f.png) [@bhoshan](https://community.unix.com/u/bhoshan)\
**Post date:** [September 15, 2017, 9:47am UTC](https://community.unix.com/t/mailx-with-attachment-and-html-content/368322/3 "2017-09-15T09:47:33Z")

</div>

$TEMP\_FILE is as follows.

```nohighlight
m158xpimdir,m158xpisdte,m158xpistme, m158xpiprost,m158xpiprbm, m158xpiprst, m1ll, m1mcu, m1ldnm,m1kcoo, m1doco, m1dcto, m1lnid,m158xpiproc,m1user
O,117258,135109,S,TRIGGXPI,S,TS_NL,124030,0,,0,,0,PPLCALL_1,TSTXPI00
I,117258,83411,S,IS02,S,JTS_US,804030,0,,775900,,0,OMACONF_1,XPI
O,117258,135346,S,TRIGGXPI,S,TS_NL,124030,0,,0,,0,PPLCALL_1,TSTXPI00

```

This $TEMP\_FILE will be converted with html tags later

What am really trying to do is send a mail with an attachment while the mail body is html as well.

any sample would be appreciate.

---

<div class="post-metadata">

**Author:** ![jgt](https://community.unix.com/user_avatar/community.unix.com/jgt/32/10703_2.png) [@jgt](https://community.unix.com/u/jgt)\
**Post date:** [September 15, 2017, 12:36pm UTC](https://community.unix.com/t/mailx-with-attachment-and-html-content/368322/4 "2017-09-15T12:36:12Z")

</div>

request

```nohighlight
m@from.com~a@to.com~SUBJECT~42833379.memo~uel14647.pdf uel14827.pdf      

```

Send process

```nohighlight
#!/bin/ksh                                                                
 IFS="~"                                                           
 cat requests/$request | read user email subject memo attachments 
 IFS=" "                                                           
 delvmime $attachments <../requests/$request \                     
 |/usr/lib/sendmail -f$user $email       

```

Build content

```nohighlight
#!/bin/ksh                                                                      
                                                                                
#Convert pdf file to mime encoded                                               
pdf(){                                                                          
echo Start pdf >>/tmp/delv.log                                                  
echo --PART-BOUNDARY.abcdefghijklmnopqrstuvwxyz                                 
echo X-Zm-Content-Name: `/bin/basename $filename.pdf`                           
echo "Content-Type: application/pdf; name=\"`/bin/basename $filename.pdf`\""    
echo "Content-Transfer-Encoding: base64"                                        
echo "Content-Dispostion: attachment;"                                          
echo " filename=`/bin/basename $filename.pdf` \n"                         
b64encod <$filename.pdf                                                         
}                                                                               
#Convert eml file to mime encoded                                               
eml(){                                                                          
echo Start eml >>/tmp/delv.log                                                  
echo --PART-BOUNDARY.abcdefghijklmnopqrstuvwxyz                                 
echo X-Zm-Content-Name: `/bin/basename $filename.eml`                           
echo "Content-Type: application/eml; name=\"`/bin/basename $filename.eml`\""    
echo "Content-Transfer-Encoding: base64"                                        
echo "Content-Dispostion: attachment;"                                          
echo " filename=`/bin/basename $filename.eml` \n"                         
b64encod <$HWD/$filename.eml                                                    
}                                                                               
#convert French language letters                                       
frn(){                                                                 
cd /u2/email/eps                                                       
echo '1d\nw\nq' |ed $HWD/$tiff                                         
cat /u2/email/escape /u2/email/letterhead $HWD/$tiff |\                
enscript -B -e -f Courier@10/11.6 -h -X ibmpc -p $HWD/$filename.ps     
ps                                                                     
}                                                                      
#convert French language letters                                       
tsa(){                                                                 
cd /u2/email/eps                                                       
echo '1d\nw\nq' |ed $HWD/$tiff >/dev/null 2>&1                         
cat /u2/email/escape $HWD/$tiff |\                                    
enscript -B -e -f Courier@10/11.6 -h -X ibmpc -p $HWD/$filename.ps     
ps                                                                     
}                                                                      
#convert PS file to PDF   
#convert PS file to PDF                                          
ps(){                                                            
cd /u2/email/eps                                                 
ps2pdf $HWD/$filename.ps $HWD/$filename.pdf >>/tmp/ps2pdf.log    
cd $HWD                                                          
echo finished ps portion >>/tmp/delv.log                         
pdf                                                              
}                                                                
################Begin process################                    
IFS="~"                                                          
read UID TO SUBJECT MEMO discard                                 
IFS=" "                                                          
#UID=$1; export UID                                              
#TO=$2; export TO                                                
#SUBJECT=$3                                                      
#MEMO=$4                                                         
if [! -r $MEMO]                                                
then                                                             
        MEMO=/u2/email/default.memo                              
fi                                                               
HWD=`pwd`; export HWD                                            
ed $MEMO </u2/email/ff.inp >/dev/null                            
HDRS=/u1/ltrs/header.txt; export HDRS TRLR=/u1/ltrs/trailer.txt; export TRLR                           
files="$*"                                                       
echo To: $TO                                                     
echo From: $UID                                                  
echo Reply-to: $UID                                              
echo Subject: $SUBJECT                                           
echo Remarks: $req                                               
echo Mime-Version: 1.0                                           
echo "Content-Type: multipart/mixed;"                            
echo " boundary=\"PART-BOUNDARY.abcdefghijklmnopqrstuvwxyz\"\n" 
echo --PART-BOUNDARY.abcdefghijklmnopqrstuvwxyz                  
echo "Content-Type: text/plain; charset=us-ascii\n"              
cat $MEMO                                                        
echo                                                             
cat $TRLR                                                        
echo "\n"                                                        
echo `date` $req $files, all attachments >>/tmp/delv.log         
for tiff in $files                                               
do                                                               
EXT=`echo $tiff|cut -d"." -f2`       
 filename=`echo $tiff|cut -d"." -f1`           
#convert attachment based on extension
$EXT                                              
done                                              
echo --PART-BOUNDARY.abcdefghijklmnopqrstuvwxyz-- 

```

You will have to add a routine to convert a csv file and create a html section.
