Bash 使用mime和postfix的多个电子邮件附件

Bash 使用mime和postfix的多个电子邮件附件,bash,sed,mime,Bash,Sed,Mime,我有一个使用mime的电子邮件模板,其中有两个附件占位符: --MixedBoundaryString Content-Type: text/plain Content-Transfer-Encoding: base64 Content-Disposition: attachment; filename="${filename1}" ${attachment1} --MixedBoundaryString Content-Type: text/plain Content-Transfer-E

我有一个使用mime的电子邮件模板,其中有两个附件占位符:

--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脚本假设每天发送一封电子邮件,在当月最后一天发送两封电子邮件

以下是我脚本的一部分,我在执行
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`
SUBJECT=“测试”
FILENAME1=“某物”
FILENAME2=“”
附件1=$(base64附件| tr-d'\n')
附件2=“”
sed-e“s/\${subject}/$subject/”\
-e“s/\${filename1}/$filename1/”\
-e“s/\${attachment1}/$attachment1/”\
-e“s/\${filename2}/$filename2/”\
-e“s/\${attachment2}/$attachment2/”temp>电子邮件
`sendmail-f$发件人$收件人<电子邮件`
我如何解决它


提前感谢

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
#/bin/bash
#sudoyum安装gettext
mssg=“这是一条信息”
filename=“filename”
ls/tmp/>/tmp/result.txt
attach=$(base64/tmp/result.txt)
email_file=“/tmp/sendmail.txt”
template_email=“/tmp/template email.eml”
函数生成\电子邮件\临时(){
>“${template_email}”
回显“$mssg”>>“${template_email}”
echo”--MixedBoundaryString“>>“${template\u email}”
echo“内容类型:文本/普通”>>“${template\u email}”
echo“内容传输编码:base64”>“${template\u email}”
echo“内容处置:附件;文件名=${filename}”>>“${template\u email}”
回显“\${attachment}”>>“${template\u email}”
echo“-MixedBoundaryString-->“${template\u email}”
echo“>>”${template\u email}”
}
生成电子邮件
export from='$from'to=“$to”mssg='${mssg}'文件名='$filename'附件=“$(echo$attach)”
MYVARS='$mssg:$filename:$result:$attachment'
envsubs“$MYVARS”<$template\u email>$email\u文件
cat“$email_文件”
邮件-s“测试”email@address.com“<$email\u文件

如果
MYVARS
不包含$filename2和$attachment2,我在
maillog
中获取事务失败<代码>表示:554事务失败:Base64流无效。(回复数据结束命令))。有什么想法吗?@hobbyking我写了一个测试脚本,打算使用mail命令,因为我将它用作客户端,并通过我的isp发送,但它不起作用,上面的代码已更新以测试它,但我现在无法测试它。