10

Objective: To send mail (using sendmail) with HTML body and binary attachment.

Followed the guidelines specified in the following links

http://www.unix.com/shell-programming-scripting/159522-sendmail-html-body-attachment-2.html

http://www.unix.com/shell-programming-scripting/58448-sendmail-attachment.html

It is working to the extent that, either HTML body or the binary attachment with uuencode, but not both.

Given below is a snippet of the shell script to sendmail. With this, the HTML body is coming fine, but the attachment is getting encoded/decoded wrongly and unable to view the same.

Please advise.

#!/usr/bin/ksh

export MAILFROM="noreply@site.dom"
export MAILTO="somebody@somesite.com"
export SUBJECT="Test PDF for Email"
export BODY="email_body.htm"
export ATTACH="file.pdf"
export MAILPART=`uuidgen` ## Generates Unique ID
(
 echo "From: $MAILFROM"
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: multipart/mixed; boundary=\"-$MAILPART\""
 echo "---$MAILPART"
 echo "Content-Type: text/html"
 echo "Content-Disposition: inline"
 cat $BODY
 echo "---$MAILPART"
 echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: base64"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 uuencode -m $ATTACH $(basename $ATTACH)
 echo "---$MAILPART--"
) | /usr/sbin/sendmail $MAILTO

I am using HP-UX ia64. Have searched through the forum and web and found references mostly to PHP, Python, etc.

compuneo
  • 351
  • 1
  • 3
  • 13
  • Using `mutt -a` is much easier: http://www.mutt.org/doc/man_page.html – ceving Jun 21 '12 at 09:09
  • Thanks, but the current server setup restricts me to sendmail only :-(. Also, mutt is not installed/setup on the server – compuneo Jun 21 '12 at 10:12
  • Send HTML body email with a plain text attachment with sendmail: http://stackoverflow.com/questions/17359/how-do-i-send-a-file-as-an-email-attachment-using-linux-command-line/14213935#14213935 – Eric Leschinski Jan 16 '13 at 04:19

2 Answers2

13

Changing the Content transfer encoding type within the email from base64 to uuencode resolved the issue. Thanks for the inputs so far.

Given below is the revised script that works.

#!/usr/bin/ksh

export MAILFROM="noreply@domain.com"
export MAILTO="mail.to@gmail.com"
export SUBJECT="Test PDF for Email"
export BODY="email_body.htm"
export ATTACH="file.pdf"
export MAILPART=`uuidgen` ## Generates Unique ID
export MAILPART_BODY=`uuidgen` ## Generates Unique ID

(
 echo "From: $MAILFROM"
 echo "To: $MAILTO"
 echo "Subject: $SUBJECT"
 echo "MIME-Version: 1.0"
 echo "Content-Type: multipart/mixed; boundary=\"$MAILPART\""
 echo ""
 echo "--$MAILPART"
 echo "Content-Type: multipart/alternative; boundary=\"$MAILPART_BODY\""
 echo ""
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/plain; charset=ISO-8859-1"
 echo "You need to enable HTML option for email"
 echo "--$MAILPART_BODY"
 echo "Content-Type: text/html; charset=ISO-8859-1"
 echo "Content-Disposition: inline"
 cat $BODY
 echo "--$MAILPART_BODY--"

 echo "--$MAILPART"
 echo 'Content-Type: application/pdf; name="'$(basename $ATTACH)'"'
 echo "Content-Transfer-Encoding: uuencode"
 echo 'Content-Disposition: attachment; filename="'$(basename $ATTACH)'"'
 echo ""
 #uuencode -m $ATTACH $(basename $ATTACH)
 uuencode $ATTACH $(basename $ATTACH)
 echo "--$MAILPART--"
) > email_`date '+%Y%m%d_%H%M%S'`.out
| /usr/sbin/sendmail $MAILTO
compuneo
  • 351
  • 1
  • 3
  • 13
0

try adding a new line after uuencode

and try also without -m

Bhavesh G
  • 3,000
  • 4
  • 39
  • 66
Nahuel Fouilleul
  • 18,726
  • 2
  • 31
  • 36
  • uuencode -m is adding a new line at the end automatically. I had already tried with -m and it did not work either. I have tried again both suggestions from you and the same problem (attachment comes, but invalid). – compuneo Jun 21 '12 at 12:33