Send email from command line using mail command

We are familiar with many graphical platforms to send out emails like Thunderbird, Outlook, Webmail etc. Here I am explaining about a tool used to send emails from the server command line on Linux based systems. It can be used via terminal as well as from bash scripts. 

  1. Installation

    We can install 'mail' tool using the following commands:

    On Centos based OS:
    yum install mailx -y

    On Ubuntu/Debian based:
    apt-get install mailx -y
  2. Once installed, we can send emails using the following commands:

    To send a simple mail with some content in the body:
    $ mail -s “test mail” test@example.com

    The -s option is used for mentioning the subject of the email followed by email address in which we need to send the email. After executing the above command, we need to enter the body content and once done, press CTRL + D to exit and send the mail.

  3. Sending emails to multiple recipients:
    $ mail -s “test mail” test@example.com,test2@example.com,test3@example.com
  4. Sending an email with an attachment:
    $ mail -s “File attached” test@example.com -A names.txt
  •  
  • 150 Users Found This Useful
Was this answer helpful?

Related Articles

How To Install and Use Docker on Ubuntu 16.04

Introduction Docker is an application that makes it simple and easy to run application processes...

Backup and Restore cPanel Accounts via SSH

a. How to create a backup of a cPanel Account via SSH?   1. Log-in to the SSH as the Root...

Change Main IP of the server :: Vesta Panel

If we are changing the main IP of a server installed with Vesta panel, we can use the following...

Change permissions using find command

On a Linux server, if you are in need of changing the permissions of a bulk amount of files or...

Change the time of your vps/dedi

It's very easy, you first need to remove the current time file rm /etc/localtime and replace it...