0

I am trying to send a mail with both a body and attachment to a specific mail, it is working fine on the command line.

However, when trying to run as part of a bash script it sends to mails that I have not even included and the subject is completely different as indicated and the attachment is also not present.

Tried to replicate this Bash script sending an e-mail with attachment, body and subject but not working as expected either.

Bash Script

#!/bin/bas

DATA_DIR=/Data
RUN_DIR=`basename "$DATA_DIR"`
REPORT_DIR=$DATA_DIR/Data/Reports


EXPERIMENT=$(awk -F',' '$1 == "Experiment " { print $2 }' $DATA_DIR)

COMPRESSED_DIR=$(tar -zcvf $REPORT_DIR.zip $REPORT_DIR)
#Mail Variables
SUBJECT="${EXPERIMENT} Report"
RECIPIENTS=user@domain.com

#Mail Body
cat > $PWD/message.txt << EOF
Experiment Name: ${EXPERIMENT}

Please find attached the associated reports

EOF

cat $PWD/body-message.txt | mail -s $SUBJECT -a $COMPRESSED_DIR $RECIPIENTS

echo "Mail Sent"

It does send the body and to the first e-mail I assign it. It then takes the arguments included in the body message as default mails. Not sure what is happening.

Thanks

  • The content of your variable `SUBJECT` contains a space! – user1934428 Oct 01 '21 at 12:27
  • 1
    check your script with shellcheck – KamilCuk Oct 01 '21 at 12:27
  • 1
    Also, the variable `COMPRESSED_DIR` contains the standard output of the `tar` command. – user1934428 Oct 01 '21 at 12:28
  • To send mail with attachment you must structurise the mail with MIME- multi-part. I suggest installing mailutils and using the `mailx` command that know how to properly structurise a MIME message with attachment and required encoding of binary data with base64. You would be able to do it in Bash and using base64 and other tools, but having `mailx` is a more appropriate option. – Léa Gris Oct 01 '21 at 12:40
  • 1
    I actually just tried sending via mutt and had a successful run :) – machine_apprentice Oct 01 '21 at 12:41

0 Answers0