Friday, October 8, 2010

How to send email from the Linux command line

Use command mail & mutt :
MAIL command :
#mail -s “Test Only” inotri@gmail.com


To add content to the body of the mail while running the command you can use the following options.
# echo “This will go into the body of the mail.” | mail -s “Test Only” inotri@gmail.com

if you want mail to read the content from a file.
# mail -s “Test Only” inotri@gmail.com < /home/toni/test.only

Some other useful options in the mail command are:
-s subject (The subject of the mail)
-c email-address (Mark a copy to this “email-address”, or CC)
-b email-address (Mark a blind carbon copy to this “email-address”, or BCC)

Here’s how you might use these options:
# echo “Testing email from command-line” | mail -s “Test Only” inotri@gmail.com -c inotri@yahoo.com -b inotri@hotmail.com

MUTT command :

# echo “Sending an attachment.” | mutt -a backup.zip -s “Test with attach” inotri@gmail.com

SENDING MAIL FROM A SHELL SCRIPT

---Sample 1 ---
#!/bin/bash
df -h | mail -s “disk space report” inotri@gmail.com

---Sample 2 ---
#!/bin/bash
df -h > /tmp/diskfile_report.log
free -m >> /tmp/diskfile_report.log
mail -s “disk file report” inotri@gmail.com < /tmp/diskfile_report.log

---Sample 3 ---
#!/bin/bash
tar -zcf /tmp/backup.tar.gz /home/toni/files
echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” inotri@gmail.com