I've got a email template using mime with 2 attachments placeholder in it:
--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. The bash script suppose to send with 1 attachment for daily email and 2 attachment when it is last day of the month.
Following is part of my script, I have set the FILENAME2=""
and ATTACHMENT2=""
when doing the sed
, but getting a attachment named 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. 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.
#!/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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.