Forget Mail Clients, Send Email from the Command Line [Linux]

Sending an email is something you often don’t have to think twice about. Simply fire up your email client, be it web or desktop-based, compose a message, enter the receipent’s email address and click “Send”. What if there is a need for you to send email from the command line, say to report the progress (or failure) of a backup process?

In Linux, sending emails from the terminal is really a piece of cake. You will need to setup a mail server though (Postfix or Sendmail). To make it easier, you can just install “mailutils” which will then install Postfix for you and allow you to send email using the “mail” command.

In Ubuntu (or Debian-based) distro, install mailutils with the command:

sudo apt-get install mailutils

It will then prompt you to configure Postfix (if it is not already installed).

mailutils-configure-postfix

mailutils configure postfix more steps

And the last thing to configure is the FQDN, which will then be used as the domain name in the “From” field.

mailutils configure postfix fqdn

Once you have installed “mailutils“, you can start to send email from the terminal using the following syntax:

mail -s "Subject" "recepient's email address" <<EOF
message here
EOF

For example, to send an email to “damien@mte.com” with the subject “Send email from terminal”, the command to use is:

mail -s "Send email from terminal" damien@mte.com <<EOF
Enter the email content here. You can write paragraphs of text here if you want. 
EOF

mail-command-syntax

And this is what you will see in your email inbox:

mail received in gmail

Mutt is yet another text-based mail client that you can use to send emails from the Terminal. What makes it better than “mail” is that it comes with additional features like:

  • color support
  • message threading
  • MIME support (including RFC2047 support for encoded headers)
  • PGP/MIME (RFC2015)
  • POP3 and IMAP support
  • etc.

To install mutt, simply use the command:

sudo apt-get install mutt

To get started, run mutt in the terminal:

mutt

This will load your email “inbox”.

mutt inbox

Press “m” to compose a new email. It will prompt you to enter the recipent’s email address.

mutt-enter-email-address

Next, it will prompt you to enter the Subject.

mutt-enter-subject

After that, it will open up a nano text editor where you can compose your message. Click “Ctrl + o” to save and “Ctrl + x” to exit.

Lastly, type “y” to send the email. You should see a “Mail sent” message.

mutt-mail-sent

Optionally, you can also attach a file to your email with the “a” keyboard shortcut, or type “c” to add a CC field.

To quit mutt, type “q”.

In addition to the “GUI” you see above, mutt can also be used in Bash script via the command line. To send an email using the mutt command:

 mutt -s "Subject" -a /path/to/file/attachment recipent@email.com < /path/to/email/message.txt

Did you notice that how similar it is to the “mail” command?

Mutt works with a config file that you can use to pre-configure your mailbox’s detail. You can make use of muttrc builder to quickly generate a “.muttrc” file and save it to your Home folder.

Sending email from terminal is not a difficult task, and in some situations, it is a necessity. The good thing is that Linux comes with useful tool that you can use to send email from the terminal. We have covered mail and mutt, but they are not the only programs available. There are still several other applications that you can use to send email from terminal. Let us know which one is your preferred choice.

Image Credit: Tim Morgan