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.
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.
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
sudo dnf install 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
Step 2) Use the Sendmail utility to send the mail using by executing the command below.
sendmail firstname.lastname@example.org < sampleemail.txt
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
sudo yum install mailx
sudo dnf install mailx
The following command will send an email without an attachment.
echo "Hello World" | mail -s "Send an email with MAIL" email@example.com
To send an email with a file attachment, use the command below.
echo "Hello World" | mail -a image1.img -s "Design Assignment" firstname.lastname@example.org
Note, as described above, option -s represents the email subject and -a represents file attachments.
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
sudo yum install mutt
sudo dnf install 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" email@example.com < /dev/null
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
sudo yum install ssmtp
sudo dnf install 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 firstname.lastname@example.org < /tmp/ssmtp-sampleMail.txt
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
sudo yum install mpack
sudo dnf install mpack
Execute the command below to send an email without an attachment.
echo "HELLO WORLD" | mpack -s "Testing Mpack" email@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" firstname.lastname@example.org -a test1.txt
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.