How to attach several files in mail using unix command?

127 views Asked by At

I'm implementing a script to backup my MySQL database. All process is OK and I send an email when it finish. But I want to attach the file in that email and I don't know how to do it.

My command line is:

mail -s "$1" -a "MIME-Version: 1.0;" -a "Content-type: text/html;" root@$domain -c [email protected] < $2

Where $1 = My subject and $2 = my message body

Thanks!

1

There are 1 answers

0
Raptor On BEST ANSWER

You are very close. You can use mail command to send 1 attachment as follow (you'd better TAR / ZIP your files before sending):

echo "$2" | mail -s "$1" -a /path/to/file.tar.gz [email protected]

Next, if you want to have more features, you can use mutt (install with apt-get install mutt):

mutt -s "$1" -a /path/to/file1.tar.gz -a /path/to/file2.tar.gz -a /path/to/file3.tar.gz [email protected] < /tmp/mailbody.txt

where:

  • file1.tar.gz to file3.tar.gz are file attachments
  • [email protected] is recipient
  • mailbody.txt is the contents of email

or use uuencode (install with apt-get install sharutils):

uuencode /path/to/file.tar.gz /path/to/file.tar.gz | mailx -s "$1" [email protected]

Note:

  • you have to repeat the file.tar.gz twice (read uuencode documentation for more information)
  • mailx is a newer version of mail, but still an ancient command

to send multiple attachments with mail command (well, if you insist):

$ uuencode file1.tar.gz file1.tar.gz > /tmp/out.mail
$ uuencode file2.tar.gz file3.tar.gz >> /tmp/out.mail
$ uuencode file3.tar.gz file3.tar.gz >> /tmp/out.mail
$ cat email-body.txt >> /tmp/out.mail
$ mail -s "$1" [email protected] < /tmp/out.mail

Hope the above helps.