Last active
October 21, 2015 20:25
-
-
Save sahilsk/bab0d629f155b14a97fc to your computer and use it in GitHub Desktop.
Send mail using command line utility:Sendmail with html encoding and attachments
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/ksh | |
# source: http://stackoverflow.com/questions/11134857/using-sendmail-for-html-body-and-binary-attachment | |
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#/bin/sh | |
# commands. | |
SENDMAIL=/usr/lib/sendmail | |
ECHO=/bin/echo | |
GZIP=/bin/gzip | |
CAT=/bin/cat | |
RM=/bin/rm | |
# attachment file info. | |
ATTACH_FILE=attachmail.txt | |
ATTACH_PATH=/tmp/$ATTACH_FILE | |
# mail head settings. | |
MAIL_BODY=/tmp/mail_body.txt | |
MAIL_FROM=xxx@abc.com | |
MAIL_TO=yyy@abc.com | |
MAIL_SUBJECT="TEST ATTACHMENT MAIL." | |
MAIL_BOUNDARY=`date +%Y%m%d%H%M%N` | |
# make attachment file (test data). | |
$ECHO "1,2,3,4,5" > $ATTACH_PATH | |
$ECHO "6,7,8,9,10" >> $ATTACH_PATH | |
# attachment compression. | |
cd /tmp | |
$GZIP $ATTACH_FILE | |
#make body. | |
$ECHO "this is Body." > $MAIL_BODY | |
# send mail. | |
$SENDMAIL -t << EOF | |
From: ${MAIL_FROM} | |
To: ${MAIL_TO} | |
Subject: ${MAIL_SUBJECT} | |
MIME-Version: 1.0 | |
Content-type: multipart/mixed; boundary=${MAIL_BOUNDARY} | |
Content-Transfer-Encoding: 7bit | |
--${MAIL_BOUNDARY} | |
Content-type: text/plain; charset=iso-2022-jp | |
Content-Transfer-Encoding: 7bit | |
`${CAT} ${MAIL_BODY}` | |
--${MAIL_BOUNDARY} | |
Content-type: application/zip; | |
name=${ATTACH_FILE}.gz | |
Content-Transfer-Encoding: base64 | |
Content-Disposition : attachment; | |
filename=${ATTACH_FILE}.gz | |
`${CAT} ${ATTACH_PATH}.gz | base64` | |
--${MAIL_BOUNDARY}-- | |
EOF | |
$RM -fr ${ATTACH_PATH}.gz |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment