Skip to content


Sending mails from command line

Often times, we want to send log files or other emails from command line or want to script them. In this tutorial, I will show you how to do that using two mail clients mail and mutt.

Sending mails using mail:

mail (mailx is the newer version) is a fantastic program that can be used for sending email from command line or from within scripts.

The following example will send an email to admin@example.com, with subject “Apache is down” and text “Please check Apache at host name of the server”

echo “Please check Apache at `hostname`” | mail -s “Apache is down” admin@example.com

We can cat the contents of any text file, for example, log file and it will be sent to the recipient specified

cat “/var/log/apache2/error.log” | mail -s “Apache is down” admin@example.com

To attach a file, other than a text one, we need to uuencode (unix to unix encode) it before sending

uuencode banner.jpg banner_out.jpg | mail webmaster@example.com

The banner.jpg is the name of input file and banner_out.jpg is the output uuencoded file that we will be sent by mail.

To have text sent alogwith the attachment, we can cat or echo that text too

(cat /var/log/apache2/error.log;uuencode banner.jpg banner.jpg) | mail -s pic webmaster@example.com


Sending mails from using mutt:

With mutt, its same as using mail.

echo “Please check Apache at `hostname`” | mutt -s “Apache is down” admin@example.com

or we can cat the contents of a text file to show as body text

cat /var/log/apache2/error.log | mutt -s “Apache is down” admin@example.com

OR

mutt -s “Apache is down” admin@example.com

To send an empty body mail, use an empty line as the mail body:

echo | mutt -s “Software upgrades for `hostname`” admin@example.com

To attach a binary file, its even easier with mutt, just use the -a option

echo | mutt -s “New logo for the company” -a logo.gif webmaster@example.com

Hope you this tutorial added to your knowledge.

Share The Knowledge:
  • Print this article!
  • Digg
  • del.icio.us
  • Facebook
  • Google Bookmarks
  • blogmarks
  • Diigo
  • E-mail this story to a friend!
  • LinkedIn
  • Live
  • Reddit
  • StumbleUpon
  • Twitter
  • Blogosphere News
  • Identi.ca
  • Slashdot
  • Technorati

Posted in Centos, Debian, Fedora, Mail, Network, Red Hat, Ubuntu.


5 Responses

Stay in touch with the conversation, subscribe to the RSS feed for comments on this post.

  1. Richard Thomas says

    Your formatting leaves a bit to be desired when viewed in IE7. The text is too long for the html pre code and forces a horizontal scroll bar to appear which completely covers the text which then causes a vertical scroll bar to be added.

    Look ok in Firefox though

  2. michaƫl says

    Good !

  3. stan porter says

    the formatting works a little better as a here document.

    mailx -s “archive tool failure” $(< ~/.primary) <<- EOF
    $err exit called in exit_arch function
    +—log file start—+
    $(<$my_log)
    +—log file end—–+
    EOF

  4. Vikki Roemer says

    There are also scrollbars in iceweasel 3.0. Very good tutorial though.

  5. amjad says

    Thanks for pointing out the scrollbars issue. I am doing a remake of the site which would take care of this. Currently, I will just remove the css class applied to pre tag.



Some HTML is OK

or, reply to this post via trackback.