我可以看到越來越多的人已經回答了你的問題,以便即時通訊與所有這個功能我只是做了分享,而我在你的.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' * –