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
- 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
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
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
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
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
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
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.