[英]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.