Welcome toVigges Developer Community-Open, Learning,Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.0k views
in Technique[技术] by (71.8m points)

shell - Using sendmail for HTML body and binary attachment

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="[email protected]"
export MAILTO="[email protected]"
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.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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="[email protected]"
export MAILTO="[email protected]"
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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to Vigges Developer Community for programmer and developer-Open, Learning and Share

2.1m questions

2.1m answers

63 comments

56.6k users

...