Mail with attachment from Shell

Today, for a specific need, I had to work with logwatch. After setting up logwatch, I needed to check the logwatch reports. But I can’t wait for it to mail me at next mailing time. But I can generate the report to see on screen by

logwatch –print

But it was too big! So, I redirected the output to a file by running:

logwatch –print > logwatch.txt

But still I am not comfortable reading such a big file from shell. So I thought to mail it. I wondered how! If I am not wrong, mail command can’t send attachment. Even I did not found anything in manual of mail command. So, started googling. I came to know about Mutt. After using it I see it is awesome and very easy.

Mutt is a small but very powerful text-based mail client for Unix operating systems.

Installation of Mutt is easy. I just hit

yum install mutt

Installation is complete. Now its time to use it.

mutt -s ‘mutt test’ -a ‘/root/logwatch.txt’ recipient@domain.com < /root/mail_message.txt

This command will send a mail to recipient@domain.com with a subject mutt test. It will also attach /root/logwatch.txt file with the mail and the mail body will be filled by the text found in /root/mail_message.txt.

But if you want compose the message rather then fetching it from a file, you can put -x instead of previous < /root/mail_message.txt. That means, you command should be:

mutt -s ‘mutt test’ -a ‘/root/logwatch.txt’ recipient@domain -x

Now, you will get prompt to enter your mail message. After completing the message, hit Enter key and type a single dot (.) and again hit Enter key. Your message will be sent to the recipient.

Simple and cool. Isn’t it?

Thanks to nixCraft

  • wow!
    I have little idea on MUTT. But attachment in mutt is new to me.
    Thanks Mr. Coder.
    😀