Three ways to Send Email from Ubuntu Command Line

If you know the real power of the command line, you wouldn't want to leave the comfort of the Terminal and go somewhere else to do any of your daily technical activities. There is always a way to do almost all of our stuff right inside the Terminal. So, why should sending emails be any different! Using the Terminal makes certain tasks more efficient and even faster. The command-line tools do not use too many resources and thus form great alternatives to the widely used graphical applications, especially if you are stuck up with older hardware. Sending emails from the Terminal becomes especially handy when you can write shell scripts to send emails and automate the whole process.

In this article, we will describe three ways through which you can send email on the Ubuntu command line (from your configured email ID).

  • ssmtp command
  • sendmail command
  • mutt command

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Open the Terminal application either through the application launcher search bar, or the Ctrl+Alt+T shortcut, and then use one of the following methods for sending emails.

Method 1: Send email with ssmtp command

ssmtp is a send-only sendmail emulator for machines that normally pick their mail up from a centralized mail hub (via pop, imap, nfs mounts or other means). It provides the functionality required for humans and programs to send mail via the standard or /usr/bin/mail user agents. If your system does not have this utility installed, run the following command to install it:

$ sudo apt- get update

And then,

$ sudo apt-get install ssmtp

The following command can then be used to compose and then send an email:

$ ssmtp [email protected]

Hit Enter and then input the subject in the following format:

Subject: sample subject comes here

As you hit Enter, you will be allowed to enter the body of the email. Once you are done with entering the email body, hit Ctrl+D. This will mark the end of the email body and send it to the respective receiver ID.

Method 2: Use sendmail command

Sendmail is a general-purpose internetwork email routing facility that supports many kinds of mail-transfer and delivery methods, including the Simple Mail Transfer Protocol (SMTP) used for email transport over the Internet. Sendmail being an smtp server requires that you have smtp installed on your system. You can then use it in the following manner. Create a text file in the following format:

Subject: Email subject comes here
Email Body Line 1
Email Body line 2
.
.
.

Save the file and then you can use the file name in the following command:

$ sendmail [email protected] < filename.txt

Method 3: Using the mutt command

Mutt is a small but very powerful text-based program for reading and sending electronic mail under UNIX operating systems, including support for color terminals, MIME, OpenPGP, and a threaded sorting mode.

This is the syntax you would be using in order to send an email without an attachment:

$ mutt -s "Subject comes here" [email protected] < /dev/null

In case you want to attach a file with your email, use the following format to specify the location of that attachment:

$ mutt -s "Subject comes here" -a /path/to/file [email protected] < /dev/null

Your email will then be sent to the specified receiver.

There is a similar command called mail which is pretty much used in the same manner as mutt in order to send emails from the command line.

You can also use telnet to send emails from within the Terminal. Although Linux administrators commonly use it to connect to remote ports and servers we will cover the procedure of how you can use it to send emails in another article on. Till then, you can try the above methods and see what works for you.