2

I am doing a MySQL database backup script and I would like it to report to me via e-mail.

So far I was able to make it to have a subject and a body without attachment. Code:

cat << EOF | mail -s "MySQL backups for $(date +%d.%m.%Y\ %H:%M)" mymail@mydomain.com
'text in the body'
text outside of the quotes
$value
EOF

Also separately than the above, I was able to make it to send e-mail with subject and attachment, but without body. Code:

gzip -c test.sh | uuencode test.sh.gz | mail -s "MySQL backups for $(date +%d.%m.%Y\ %H:%M)" mymail@mydomain.com

When I try to combine them as below, I am receiving an e-mail with an empty body.

cat << EOF | gzip -c test.sh | uuencode test.sh.gz | mail -s "MySQL backups for $(date +%d.%m.%Y\ %H:%M)" mymail@mydomain.com
'text in the body'
text outside of the quotes
$value
EOF
Anton Todorov
  • 1,117
  • 1
  • 8
  • 14
  • Possible duplicate of [How do I send a file as an email attachment using Linux command line?](https://stackoverflow.com/q/17359/608639) – jww Dec 23 '19 at 20:57

1 Answers1

1

in the piped command, you override the body of the message with the encoding of attachment.

try something like:

cat << EOF | mail -s "MySQL backups for $(date +%d.%m.%Y\ %H:%M)" mymail@mydomain.com
'text in the body'
text outside of the quotes
$value

$(gzip -c test.sh | uuencode test.sh.gz)
EOF
m47730
  • 2,061
  • 2
  • 24
  • 30
  • Fantastic! It works as expected. Now I may need to find out how to do the same thing without archiving the file... it will be greatly appreciated if you could still help. – Anton Todorov May 22 '15 at 14:09
  • Are you archiving to a file? `gzip -c` writes to standard output, not `test.sh.gz`, and since the standard input of `uuencode` is connected to the output of `gzip` via the pipe, it is ignoring the file name you give it as an argument. – chepner May 22 '15 at 14:15
  • Well I tried with `$(gzip -c test.sh | uuencode test.sh)` and it sent me a file, but it was not readable. I tried with `$(test.sh)` but it did not attach anything. I also tried with `$test.sh` but it did not attach anything. – Anton Todorov May 22 '15 at 14:33