使用sendmail命令发送附件
在自动化中经常需要将日志文件发送到指定用户组,于是记录一下使用sendmail发送邮件及附件的shell脚本模板
#!/bin/bash
MAILFROM="noreply@`hostname -f`"
MAILTO="mail.to@hotmail.com"
SUBJECT="Sendmail templete test"
ATTACHMENT="XXXXXXX20190227.log"
MAILPART=`uuidgen` ## Generates Unique ID as boundary
MAILPART_BODY=`uuidgen` ## Generates Unique ID as boundary
(
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=UTF-8"
echo "This is TEXT part and below is HTML part"
echo "--$MAILPART_BODY"
echo "Content-Type: text/html; charset=UTF-8"
echo ""
echo "<html><body><div>THIS IS HTML PART</div></body></html>"
echo "--$MAILPART_BODY--"
echo "--$MAILPART"
echo 'Content-Type: text/plain; name="'$(basename $ATTACHMENT)'"'
echo "Content-Transfer-Encoding: base64"
echo ""
openssl base64 < $ATTACHMENT;
echo "--$MAILPART--"
) | sendmail -t