[英]Multiple email attachment using mime and postfix
我有一個使用mime的電子郵件模板,其中帶有2個附件占位符:
--MixedBoundaryString
Content-Type: text/plain
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="${filename1}"
${attachment1}
--MixedBoundaryString
Content-Type: text/plain
Content-Transfer-Encoding: base64
Content-Disposition: attachment; filename="${filename2}"
${attachment2}
--MixedBoundaryString--
並創建了bash腳本來替換電子郵件內容和附件占位符,然后再發送。 bash腳本假定在每月的最后一天發送帶有1個附件的每日電子郵件和2個附件。
以下是我的腳本的一部分,當執行sed
時,我已經設置了FILENAME2=""
和ATTACHMENT2=""
,但是得到了一個名為ATT00001.txt的附件。
SUBJECT="TESTING"
FILENAME1="something"
FILENAME2=""
ATTACHMENT1=$(base64 attachment | tr -d '\n')
ATTACHMENT2=""
sed -e "s/\${subject}/$SUBJECT/" \
-e "s/\${filename1}/$FILENAME1/" \
-e "s/\${attachment1}/$ATTACHMENT1/" \
-e "s/\${filename2}/$FILENAME2/" \
-e "s/\${attachment2}/$ATTACHMENT2/"temp > email
`sendmail -f $SENDER $RECIPIENTS < email`
我該如何解決?
提前致謝
包gettext中可用的'envsubst'命令可能會有所幫助。 基本上,您可以創建一個模板文本文件,其中包含變量作為占位符。 我沒有使用它,因為我不會像這樣使用bash,但是我使用了一種叫做twig的東西,其作用類似。
#!/bin/bash
# sudo yum install gettext
mssg="This is a message"
filename="FILENAME"
ls /tmp/ > /tmp/result.txt
attach=$(base64 /tmp/result.txt)
email_file="/tmp/sendemail.txt"
template_email="/tmp/template-email.eml"
function build_email_temp() {
> "${template_email}"
echo "$mssg" >> "${template_email}"
echo "--MixedBoundaryString" >> "${template_email}"
echo "Content-Type: text/plain" >> "${template_email}"
echo "Content-Transfer-Encoding: base64" >> "${template_email}"
echo "Content-Disposition: attachment; filename=${filename}" >> "${template_email}"
echo "\${attachment}" >> "${template_email}"
echo "--MixedBoundaryString-- " >> "${template_email}"
echo "" >> "${template_email}"
}
build_email_temp
export from='$from' to="$to" mssg='${mssg}' filename='$filename' attachment="$(echo $attach)"
MYVARS='$mssg:$filename:$result:$attachment'
envsubst "$MYVARS" < $template_email > $email_file
cat "$email_file"
mail -s "test" "email@address.com" < $email_file
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.