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:

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:

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

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:

To get started, run mutt in the terminal:

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:

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

10 comments

  1. to add, one can quickly pipe output from a command to mail and use it as a body of the message. for example:

    ps auwxf | mail -s ‘ps output’ mail@my.domain.com

    admin
    RoseHosting.com

    • You just have to specify the filepath to the .compress file and use this
      syntax:

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

  2. A follow up to this article would be to show how you can send out using smtps since port 25 is blocked by most consumer ISP’s. Then another would be to add authentication to smtps if it’s not part of the previous suggestion.

Comments are closed.

Sponsored Stories