Here's how you can use these options:
# echo 'Welcome to the world of Calvin n Hobbes' | mail -s 'Hello world' calvin@cnh.com -c hobbes@cnh.com -b susie.derkins@cnh.com
MUTT
One of the major returns in using mail commands is that it does not support sending attachments. However, the mutt command supports that. We have found that this feature is very useful for scripts to create non-original reports or small-sized backups. Mutt allows you to do a lot of work besides just sending attachments. It also has more features than mail commands. Let's go explore the basics that can be done with this command. Here's how you can attach a file to your mail:
# echo 'Sending an attachment.' | mutt -a backup.zip -s 'attachment' calvin@cnh.com
This will send a mail to calvin@cnh.com with the subject 'attachment', the body is 'Sending an attachment.' and includes an attachment backup.zip. Like the mail command, you can use the '-c' option to mark CC to another mail ID.
Send mail from a script
With the basics already introduced, it is possible to send mails from your scripts. This is a simple script that we introduce to read the usage space on the hard drive area and mail that data to you.
#! / bin / bash
df -h | mail -s 'disk space report' calvin@cnh.com
Save these lines in a file on the Linux server and run this file. You will receive an email containing the results of the commands in it. However, if you need to send more data, you need to write the data into a text file and enter it into the body of the mail while composing the mail. Here is an example of a script that performs the task of viewing the hard disk and memory performance, writing that data to a temporary file, and then importing it into the body of a mail and sending it:
#! / bin / bash
df -h> /tmp/mail_report.log
free -m >> /tmp/mail_report.log
mail -s 'disk and RAM report' calvin@cnh.com
There is a more complicated issue here. You have to get a back up of files and mail later. First the mail folder will be saved. It will then be sent as an email attachment with the mutt command. This is the script to do that:
#! / bin / bash
tar -zcf /tmp/backup.tar.gz / home / calvin / files
echo | mutt -a -s /tmp/backup.tar.gz 'daily backup of data' calvin@cnh.com
The Echo at the end of the last line will add a blank part to the body of the mail that will be set.