Monday, May 28, 2012

Sending Email With Attachments From Unix / Linux Command

If you need to send an email with a text file (or binary file) as attachment using shell script or command prompt in Unix or Linux; try mutt - a terminal-based e-mail client for Unix-like systems.

mutt -s "Test mail" -a /tmp/shahzeb.tar.gz shahzeb143@gmail.com < /tmp/mailmessage.txt

Where,
  • shahzeb143@gmail.com   - is the recipient.
  • /tmp/mailmessage.txt - is the main body of the e-mail (read message from the file "mailmessage.txt").
  • -a /tmp/shahzeb.tar.gz - is an attachment.
  • -s "Test mail" - is a subject line of email Message.

If MUTT is not available???

If mutt is not installed, use the apt-get or yum or up2date commands as follows (you must login as a root user). Debian / Ubuntu Linux user type the following command to install mutt client:
# apt-get install mutt
OR
# yum install mutt 

Some useful resources