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,281FansLike
386FollowersFollow
16SubscribersSubscribe

LATEST ARTICLES

MUST READ

Buyers who wish to go for a machine that is based on Linux often show interest in Chromebooks due to the form factor and extended battery life capabilities. Although ChromeOS power these machines, users can still miss out on a more genuine Linux experience. For those who happen to agree, the new Lemur Pro by System76 might get some heads turning.
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.

How to dual-boot Ubuntu and Fedora on your PC

Every Linux distribution has its capabilities and preferences as per the user requirements. That brings forth the need to run different distros on one laptop. For example, a CyberSecurity enthusiast might prefer to use Ubuntu for development and Kali Linux for penetration testing.

The 5 Best Open Source Password Managers

It won't be wrong to say that managing passwords on your own could be a tad tricky, especially if you're frequently registering on new websites. Although your web browser's built-in password manager could do the trick, your passwords could still come into jeopardy in case you log in to your main account from another computer and forget to log out.

25 Bash Script Examples

An operating system has two major core components Kernel and Shell. A kernel is the brain of the operating system that controls everything in the system. To protect the kernel from direct user interaction, there is an outer wrap called Shell.

Top 20 must-have apps for your Ubuntu PC

OK, this one is going to be a long one, so grab a cup of coffee and scroll through the best apps that we think are must-have for your Ubuntu PC. We have hand-picked each one of these considering the most common categories that suit an average Linux user.