Send e-mails from command line on Linux Shell

send emails from command line

Send e-mailsend emails from command lines from command line on Linux is very useful for many reasons, ffor example to receive results or logs from your scheduled scripts like virus scan, check free space on the hard disk, backup, … or maybe for manage some kind of alert when happen an event.

To do it you can use the mail command. But to use it you need an MTA (Mail Transport Agent): a SMTP mail server.

There are many SMTP server on Linux: sendmail, qmail, postfix, … and so on.

In this post I use Postfix like SMTP Server.

I install it by this command:

sudo apt-get install postfix

it require the following information:

  • Type of configuration: choose Satellite System in this way the server relay the e-mails to another public server
  • System mail name: it’s the name you want to assign to the server. It’s better it’s a FQDN (Fully Qualified Domain Name), for example myserver.local
  • SMTP relay host: it’s the name of the public SMTP Server where your server relay the e-mails. It’s better specify the SMTP Server of your provider or use another SMTP server with authentication like Gmail

Postfix installationPostfix installationPostfix installation

When the installation is completed, the SMTP service will be started.

Now, to use the mail command you need to install the heilroom-mailx package with the following command:

sudo apt-get install heirloom-mailx

Finally, you can send an e-mail using the mail command in the following way:

echo "e-mail body text" | mail -s "Subject" -r (sender e-mail) (recipient email 1) (recipient email 2) … (recipient email n)

For example:

echo “Hi, this is the body of the e-mail” | mail -s "My first e-mail from command line" -r

IMPORTANT: in the example before I used the echo command to put the e-mail body text but you can use many other command: their output will be the e-mail body text.

For example, if I want to know the state of my disk every morning I can schedule this command:

df | mail -s "System Disk Space Usage" -r

in this way the output of command df will be the text of the e-mail.

Another way to input the text of the e-mail is putting it in a text file (for example “myemailbody.txt”) and use the command cat like in the following example:

cat /path-of-the-file/myemailbody.txt | mail -s"Another Test" -r

Obviously is possible to send an email with attachment from the command line simply using the -a option with the path of the file to attach:

echo “Here is it the attachment” | mail -s "Test to send and attachment" -a /tmp/my_attachment.tar.gz -r

How you can see, the mail command is very powerful. The limit is your fantasy 😉


  1. Pingback: Configure Postfix to relay emails through Gmail STMP Server

  2. FirstElvia

    I see you don’t monetize your site, don’t waste your traffic, you can earn extra cash every month because
    you’ve got high quality content. If you want to know how to make
    extra $$$, search for: Boorfe’s tips best adsense alternative

Leave a Reply

Your email address will not be published. Required fields are marked *