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

时间:2018-06-07 02:14:28

标签: bash sed mime

我使用带有2个附件占位符的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脚本假设每日电子邮件附带1个附件,而当月份的最后一天发送2个附件。

以下是我的脚本的一部分,我在执行FILENAME2=""时设置了ATTACHMENT2=""sed,但获得了名为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`

我该如何解决?

提前致谢

1 个答案:

答案 0 :(得分:0)

&#39; envsubst&#39;包gettext中提供的命令可能会有所帮助。基本上,您可以创建包含变量作为占位符的模板文本文件。我没有使用过这个,因为我不会像这样使用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