我可以看到越来越多的人已经回答了你的问题,以便即时通讯与所有这个功能我只是做了分享,而我在你的.bash_profile建议把方便访问。这个函数解决了带有附件的html邮件头痛的问题。有一个关于如何在代码中使用它的评论示例。对于您的使用情况,您可以将其用作:
SENDMAIL "THE.SENDER" "[email protected]" "$(date) - This is a subject" "<html>This is <b>bold</b></html>" "test.txt"
要使用此功能,您需要在计算机上安装unix2dos。
function SENDMAIL() {
#this is an example
# SENDMAIL "THE.SENDER" "[email protected]" "This is a subject" "<html>This is <b>bold</b></html>" "test.txt"
function get_mimetype(){
file --mime-type "$1" | sed 's/.*: //'
}
rm -f -r /tmp/mail_smd.html
from="$1"
to="$2"
subject="$3"
body="$4"
boundary="=== Boundary ==="
if [ -z "$from" ] || [ -z "$to" ] || [ -z "$subject" ] || [ -z "$body" ]
then
echo "ERROR!!! ";
echo "All parameters are mandatory (except for the attachments).";
echo "First parameter : FROM";
echo "Second parameter : TO";
echo "Third parameter : SUBJECT";
echo "Fourth parameter : BODY";
echo "Fifth parameter: FILES TO ATTACH (This is optional)"
echo "Example : SENDMAIL \"IAMTHESENDER\" \"[email protected]\" \"This is a subject\" \"<html>This is <b>bold</b></html>\" \"./smd_files/input_prod_tte.txt\" ";
return 1;
fi
attached="$5"
declare -a attachments
attachments=($attached)
echo "From : $from"
echo "To : $to"
echo "Subject : $subject"
echo "Body : $body"
echo "Attachments : $attached"
# Build headers
echo "From:$from" > /tmp/mail_smd.html
echo "To:$to" >> /tmp/mail_smd.html
echo "Subject: $subject" >> /tmp/mail_smd.html
echo "Mime-Version: 1.0" >> /tmp/mail_smd.html
echo "Content-Type: multipart/mixed;boundary=\"$boundary\"" >> /tmp/mail_smd.html
echo "--${boundary}" >> /tmp/mail_smd.html
echo "Content-Type: text/html;charset=iso-8859-1"$'\r'$'\n' >> /tmp/mail_smd.html
echo "$body"$'\r'$'\n' >> /tmp/mail_smd.html
for file in "${attachments[@]}"; do
[ ! -f "$file" ] && echo "Attachment $file not found, omitting file" >&2 && continue
mimetype=$(get_mimetype "$file")
echo "--${boundary}" >> /tmp/mail_smd.html
echo "Content-Type: application/octet-stream; name=\"$(basename $file)\"" >> /tmp/mail_smd.html
echo "Content-Transfer-Encoding: base64 " >> /tmp/mail_smd.html
echo "Content-Disposition: attachment; filename=\"$(basename $file)\" "$'\r'$'\n' >> /tmp/mail_smd.html
unix2dos $file
echo "$(/usr/bin/base64 $file)" >> /tmp/mail_smd.html
done
# print last boundary with closing --
echo "--${boundary}--" >> /tmp/mail_smd.html
/usr/sbin/sendmail -t -oi < /tmp/mail_smd.html
}
谢谢!
你有什么尝试吗?看看'日期' – fzd
我试图把我的变量放在主题(“$ Y”)中,但它以主题相同的文本 –
我也试过*'日期+%d /%m /%Y' * –