5 ways to send emails using the command-line in Linux

Did you master using the command-line in Linux? There is no limit to what one can do via the Linux Terminal. One of such things we are going to discuss today are methods of sending an email using the command-line.

Once you become used to the marvelous Linux Terminal, you practically now want to do everything there in the Linux style. In this tutorial, we are going to show you how you can send an email from the Terminal and even include file attachments.

We are all used to the web interfaces and applications like Thunderbird Mail in Ubuntu which provide us with fantastic Graphical User Interfaces (GUI) to send our emails. Here, we will show you how to send emails technically from the console.

Sending an email using command-line

Sending mails from the Terminal is mainly useful when we need to write scripts that create emails and send them automatically. It also helps when we need to create cron jobs to manage bulk mails.

There are several command-line mail clients that we will use. Below are some which we will use for this tutorial. We will also show you how you install them.

  • Sendmail
  • Mail
  • Mutt
  • SSMTP
  • Mpack

It’s also good that you understand some of the options that we will use with the mail clients above.

  • s -represents the subject of the mail
  • a -used to denote the file attachment to send along with the email
  • c -Stands for Copy Email Address (CC)
  • b -Stands for Blind Copy email address (BCC)

Let’s look at five ways in which you can send an email to a recipient from the Linux Terminal.

1. Sendmail

Sendmail is one of the popular SMTP servers present in most Linux systems used to send emails from the command line. It does not come installed as a default utility in all systems. However, you can easily install it with the package manager.

Install Sendmail on RHEL/CentOS:

sudo yum install sendmail

On Ubuntu/Debian distributions:

sudo apt-get install sendmail

On Fedora:

sudo dnf install sendmail

Install sendmail
Install Sendmail

Using Sendmail

Sending emails using Sendmail utility is a straightforward process.

Step 1) Create a text file with the email content. Remember to include the subject, as indicated below.

cat sampleemail.txt

Subject: Send an email with SENDMAIL
Hello world
That is the sendmail utility

sendmail email contents
Sendmail email contents

Step 2) Use the Sendmail utility to send the mail using by executing the command below.

sendmail user@example.com < sampleemail.txt

2. Mail

We can also send emails from the Linux Terminal using the Mail command. First, let’s look at how you install this utility in different Linux distributions.

Install Mail on Ubuntu/Debian distributions:

sudo apt-get install mailutils

On RHEL/CentOS:

sudo yum install mailx

On Fedora:

sudo dnf install mailx

Install mailutils in Ubuntu/Debian
Install mailutils in Ubuntu/Debian

Using mail

The following command will send an email without an attachment.

echo "Hello World" | mail -s "Send an email with MAIL" usera@example.com

To send an email with a file attachment, use the command below.

echo "Hello World" | mail -a image1.img -s "Design Assignment" daygeek@gmail.com

Note, as described above, option -s represents the email subject and -a represents file attachments.

3. Mutt

Mutt is a powerful Linux command used to send emails using the Terminal. It works by reading mails from local user mailboxes and POP/IMAP servers. It also supports color Terminals, threaded sorting mode, MIME, and OpenPGP.

Install Mutt on Ubuntu/Debian distributions:

sudo apt-get install mutt

On RHEL/CentOS

sudo yum install mutt

On Fedora

sudo dnf install mutt

Install mutt in Ubuntu
Install mutt in Ubuntu

Using Mutt

Sending an email with the mutt command is a straightforward process. You can also send a blank mail with mutt by adding < /dev/null right after the destination mail address.

mutt -s "Hello world" user@example.com < /dev/null

4. SSMTP

SSMTP enables you to send email from a local computer to a configured mailhost/mail hub.

Install SSMTP on Ubuntu/Debian distributions:

sudo apt-get install ssmtp

On RHEL/CentOS:

sudo yum install ssmtp

On Fedora:

sudo dnf install ssmtp

Install ssmtp in Ubuntu
Install ssmtp in Ubuntu

Using SSMTP

Use the command below to create the email you want to send.

echo -e "Subject: Email Subject\nHELLO WORLD" > /tmp/ssmtp-sampleMail.txt

To send the email, run the command below.

ssmtp user@example.com < /tmp/ssmtp-sampleMail.txt

5. Mpack

Mpack is also another commonly used command to send emails from the Terminal. It works by encoding the emails and attachments into one or more Multipurpose Internet Mail Extensions (MIME) before sending them to the required destination. Mpack also does not come pre-installed in most systems; however, you can install it from the package manager.

Install mpack on Ubuntu/Debian distributions:

sudo apt-get install mpack

On CentOS/RHEL

sudo yum install mpack

On Fedora

sudo dnf install mpack

Install Mpack in Ubuntu
Install Mpack in Ubuntu

Using mpack

Execute the command below to send an email without an attachment.

echo "HELLO WORLD" | mpack -s "Testing Mpack" user@example.com

If you wish to send a file attachment along with the email, use the syntax in the command below.

echo "This is the mail body" | mpack -s "Subject" daygeek@gmail.com -a test1.txt

Conclusion

Those are five ways you can use to send emails from the Linux Terminal. Feel free to share information about other useful utilities you have used using the comment section below. Also, you can share your experience working with the commands we discussed in this tutorial. If you find this post resourceful, feel free to share the link with your friends.

Arun Kumar
Arun did his bachelor in computer engineering and loves enjoying his spare time writing for FOSS Linux. He uses Fedora as the daily driver and loves tinkering with interesting distros on VirtualBox. He works during the day and reads anything tech at night. Apart from blogging, he loves swimming and playing tennis.

LEAVE A REPLY

Please enter your comment!
Please enter your name here

STAY CONNECTED

23,648FansLike
360FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

How to install CMake on Ubuntu

CMake is a cross-platform free and open-source software tool designed to build, test, and package the software. CMake uses a simple platform and compiler-independent configuration files to control the software compilation process.

How to install Lightworks on Ubuntu

Even though Linux may not get a native installer of video editing software like Adobe Premiere or Final Cut Pro, that doesn't mean there are no industry standards tools available. Lightworks is non-linear editing (NLE) video mastering app for Windows, Linux, and macOS. Installing it on Ubuntu is simple due to deb package availability.

How to install DaVinci Resolve on Fedora

Davinci Resolve is a professional application used for color correction, video editing, visual effects, and motion graphics. It is one of the extensively used software by movie industries located in Hollywood.

The 10 Best Programming Languages for Hacking

One of the significant entities we have in Cyber Security is Ethical Hacking (ETH). It is the process of detecting and finding flaws or vulnerabilities in a system that a hacker would exploit.

5 Ways to Open a Terminal in Ubuntu

Even though Ubuntu supports many applications with amazing Graphical User Interfaces (GUI), there are always reasons why users prefer using the Terminal to perform different tasks.

How to install Wine on Fedora Workstation

Linux distributions are becoming more and more popular every day, and Fedora Workstation is not left behind. This popularity brings forth the need to run Windows applications on Linux distros like Fedora. Windows has quite some excellent Software that is not available for Linux.

MUST READ

Linux is growing faster than ever. As per the latest report, there is a drop in the Windows 10 market share for the first time, and Linux's market share has improved to 2.87% this month. Most of the features in the list were rolled out in the Pop OS 20.04. Let's a detailed look into the new features, how to upgrade, and a ride through video.
Elementary OS 5.1 Hera has received a point release with a handful of new features and bug fixes, and we will be reviewing the significant changes in this article. For those new to elementary OS, this Ubuntu-based Linux distribution uses their inhouse built Pantheon desktop environment and AppCenter.

All about Ubuntu editions and which version should you use?

Ubuntu is one of the most popular Linux distributions developed and released by Canonical, and not without reason. It has very enriched repositories, with support for all the programs you could ever need.

How to create a Live Linux USB drive using Etcher

In the Linux community, one thing is common between almost all of the users: trying out different Linux distributions. Most of the Linux based distributions have a great way of being tested as they provide ISO images for creating a Live USB.

VIDEO: MX Linux 19 Features and Desktop Tour

MX Linux is a Linux distribution based on Debian stable and using core antiX components. The distributions MX Tools is very popular among users and combined with other several ready-to-use tools, it is great for users who prefer to tweak their distro to their liking. In this video, we will take you through the features tour showing casing MX Linux 19.

3 Best Ways to Uninstall Software on Ubuntu

Uninstallation of programs can be done by graphical way using the Ubuntu Software Center, and the Synaptic Package manager. Command-line way of doing it is also possible using apt-get and aptitude commands. We shall discuss each one of them in detail.