简体   繁体   English

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

[英]Multiple email attachment using mime and postfix

I've got a email template using mime with 2 attachments placeholder in it: 我有一个使用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--

and created bash script to replace the email contents and attachment placeholder before sending it. 并创建了bash脚本来替换电子邮件内容和附件占位符,然后再发送。 The bash script suppose to send with 1 attachment for daily email and 2 attachment when it is last day of the month. bash脚本假定在每月的最后一天发送带有1个附件的每日电子邮件和2个附件。

Following is part of my script, I have set the FILENAME2="" and ATTACHMENT2="" when doing the sed , but getting a attachment named ATT00001.txt. 以下是我的脚本的一部分,当执行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`

How can I resolve it? 我该如何解决?

Thanks in advance 提前致谢

'envsubst' command available in the package gettext may help. 包gettext中可用的'envsubst'命令可能会有所帮助。 Basically you can create a template text file that contains the variables as placeholders. 基本上,您可以创建一个模板文本文件,其中包含变量作为占位符。 I've not used this because I wouldn't use bash like this but I've used something called twig which acts similar. 我没有使用它,因为我不会像这样使用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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM