Tutorial: Sending Mail Through Command line

Valic —  August 6, 2010 — 2 Comments

I will show you get started with sending mails from the Linux command line. Will also cover sending attachments from the command line. We will begin with the “mail” command.

First we need to test the sendmail application to be installed and working correctly executing the following command:

echo “This is the body of  mail.” | mail -s “Subject” [email protected]

Other command line option:

-s subject Specify subject on command line.
-c email-address Mark a copy to this “email-address”, or CC.
-b email-address Send blind carbon copies to list. List should be a comma-separated list of names.

Below are few examples that you can use with the mail command:


mail -s “System Log” [email protected]

This command sends a message  with a subject line of “System Log” and the text of the message read from the system logfile /var/log/messages.

df -h | mail -s “Disk Space” [email protected]

This command sends a message  with a subject line of “Disk Space” an the text of the output read from command df -h.

free -m | mail -s “Free RAM” [email protected]

This command sends a message  with a subject line of “Free RAM” an the text of the output read from command free -m.

Valic

Posts Twitter Facebook

Editor in Chief at Debian-Tutorials, Linux enthusiast.

2 responses to Tutorial: Sending Mail Through Command line

  1. Filtering emails
    ============

    # cat /var/log/mail.log | grep ’11:[0-5][0-9]‘ | grep ‘Jun 28′

    # grep ‘smtpd\[.*client=’ /var/log/mail.log | wc -l

    # grep ‘postfix/smtp\[.*status=sent’ /var/log/mail.log | grep -v ‘relay=127.0.0.1′

    # cat /var/log/mail.log | grep ‘from’ | grep ‘Jul*’ | wc -l

    # cat /var/log/mail.log | grep ‘to’ | grep ‘Jul*’ | wc -l

    # tail -f /var/log/squid3/access.log | gawk ‘{print strftime(“%d/%m/%Y %H:%M:%S”, $1), $3, $7}’

    # cat /var/log/mail.log | grep ‘from=<ewb*' | grep "Ja*" | wc -l

    by Jurandir

  2. Looking form “words” inside squid compressed files
    =========================================

    # zcat /var/log/squid3/access.log.1.gz | awk ‘{print $3 “-” $7 “-” $11}’ | grep face* | wc -l

    # zcat /var/log/squid3/access.log.1.gz | awk ‘{print $3 “-” $7 “-” $11}’ | grep face*

Leave a Reply