Mail, send emails from the terminal of your Gnu / Linux computer

About mail command

In the next article we are going to take a look at the Mail command. Today there are many ways to send emails, either using the GUI, using the browser, or with an email client. But the options get more limited when it comes to the command line interface (CLI). That is why in this post we are going to see how send emails from the terminal of a Gnu / Linux system.

There are some options that we can use to send emails from the terminal, such as Sendmail, mother, etc. But in these lines, we will see how to use the Mail command to send emails from the terminal of our system. We will be able to use this command directly from the terminal or we can also use it when we program our BASH scripts.

Before starting to use it, we will have to install this mail command, in case our distribution does not have it by default.

Installation

As I say, many Gnu / Linux distributions, have the mail command by default, but if this is not the case on your system, say that it can be installed using a few commands. For this article I have to say that I am using Ubuntu 18.04 LTS. In case you have to install the program on Ubuntu / Debian / Linux Mint, we can use apt-get to install the command on our systems. We open a terminal (Ctrl + Alt + T) and write:

sudo apt-get install mailutils

Examples to send emails from the terminal

Send a simple email

To send a simple email, just with some content in the body, we will execute:

simple mail sent from the terminal

mail -s "Email simple enviado desde la terminal" nonaino@mail.com

In this command, the -s option is used to mention the subject of the email. Follow the email address to which we want to send the mail. Now after running the above command, we need to write the body content. Once we're done we will press the key combination CTRL + D to exit and send the mail.

In our mail manager we will receive something like the following. I have to admit that first, the mail I have found in the span folder.

mail simple mail received

We can also use the following command in a single line to send the mail:

mail -s "Email de prueba" nonaino@mail.com <<< "Este es el cuerpo del correo"

Send mail to multiple recipients

For esend emails to more than one user, we just have to mention all email addresses separated by a comma. An example of this would be:

Multiple recipient mail sent from the terminal

mail -s "Email a varios usuarios" usuario1@mail.com,usuario2@mail.com,usuario3@mail.com

The users to whom the mail is sent will receive something like the following:

mail multiple recipients received

Send an email with an attachment

Another option that we will have, will be to send an email with an attachment. For this we will use the option 'A' with the Mail command. I have to say that the file that I send in this example is in the same folder where I am while sending the mail. For example, if we wanted to send a file called 'text.txt', we would use the following command:

mail mail with attachment sent from the terminal

mail -s “Correo con archivo adjunto” nonaino@mail.com -A texto.txt

The user who receives the mail, will see in his mail manager something like what can be seen in the following screenshot:

email with attachment received

Send the content of a file in the mail

To send the content of a file using the mail command we will write in the terminal:

mail mail sending the content of a file from the terminal

mail -s “Salida del archivo” nonaino@mail.com < /home/sapoclay/texto.txt

The user you will receive the content of the file directly in the body of the email. As can be seen in the following screenshot, the file in question is not sent as an attachment, as was the case in the previous section.

mail with the content of a file sent from the terminal

Send an email with the output of a command

We will be able to send the output of a command as the content of the mail body. For example, if we need to send the output of 'cat /home/sapoclay/.config/mimeapps.list'we would use:

mail of a command with its result sent from the terminal

cat /home/sapoclay/.config/mimeapps.list | mail -s "Envío del resultado de un comando" nonaino@mail.com

Send an email with the echo command

We can also use the echo command to send an email electronic.

echo "Este es el cuerpo del email" | mail -s "Prueba de correo" nonaino@mail.com

Send an email with additional headers

To send an email with additional headers, we will use the 'to' option with the mail command.

mail with additional headers

mail -s "Correo con encabezados adicionales" -a From:sapoclay\<usuario1@casimailx.com\> nonaino@mail.com

The result received by the user would be the following:

mail additional headers received

With this we end the article. These were just a few common examples of the use of this command. Looking a little on the internet you can find some others. We can also find more documentation about this command making use of the man pages in terminal (Ctrl + Alt + T):

man command mail

man mail

4 comments, leave yours

Leave a Comment

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

*

*

  1. Responsible for the data: Miguel Ángel Gatón
  2. Purpose of the data: Control SPAM, comment management.
  3. Legitimation: Your consent
  4. Communication of the data: The data will not be communicated to third parties except by legal obligation.
  5. Data storage: Database hosted by Occentus Networks (EU)
  6. Rights: At any time you can limit, recover and delete your information.

  1.   edkalrio said

    I get the feeling that even the one who wrote this tutorial is not aware of what it means to not configure an MTA. So that novice readers understand me, this tutorial, as it is written, only serves to send emails to yourself although at no time is this major deficiency noticed.

    the tutorials of ubunlog They used to have better quality. It is a pity.

  2.   Javi Happy said

    The good thing about the article would be how to configure a mail server, or configure gmail ...

    I am new, but I have seen little things on the subject of emails, for gmail I had to configure something or other.

    It would be interesting to do something a little more specific and as I say, to mount a mail server or what files should be touched.

    Can you comment on MTA?

    Greetings and thank you.

  3.   darsie said

    query, and where is the mail configured to be able to send ??? do you place an smtp? I do not understand
    Magically the emails would not send themselves. An output reference is missing

  4.   Ernesto said

    At least I'm not the only asshole who realizes that this tutorial is not complete, I just installed ubuntu and none of the things that are said here work. SHIT!