The Linux terminal allows us to perform a large number of tasks with a few keystrokes. For those who spend most of their time on the terminal, they can also send emails directly from the terminal. This guide will guide you how to use various methods and tools to send email directly from the Linux terminal.
Note: To send email from the terminal, ensure you have a mail server set up and working correctly.
The first and easiest way to send email from a Linux terminal is to use the mail utility. This simple utility allows you to specify the recipients, the subject of the email, and even add attachments with various options.
By default, the mail utility is pre-installed on most Linux distributions. You can check if you have installed it by running which command, as shown below:
If you get a result similar to “/ usr / bin / mail”, then it is already installed on your distribution. If your Linux distribution does not have mail installed by default, you can install it using your distribution’s package manager. For example, on Debian, you would run the following command:
sudo apt-get update && sudo apt-get install mailutils
To use mail to send an email, use the
-s option and specify the email subject. For example, to send a message stored in the file “message.txt”, use the command:
mail -s "Hello world" [email protected] < message.txt
The above command will read the contents of the file and use it as the message body.
You can also pass the message body from a command such as
echo. For example:
echo "This is the message body" | mail -s "Hello world" [email protected]
To add attachments to the email, use the
-A option. For example:
echo "Sample odt file" | mail -s "Attachments" [email protected] -A ~/Documents/sample.odt
The next utility that you can use to send emails from the terminal is Sendmail, which is a simple and powerful utility that can help you send emails from the terminal.
If you do not have the Sendmail utility installed, you can install it:
sudo apt-get install sendmail sendmail-cf -y
To use this utility, start by creating a file containing the following as email content:
Subject: Hello World! This is the message body .... ..... .... ... close.
The Sendmail utility will locate the subject header and use it as the subject title for your email. You can pass this by using the command:
cat sendmail.txt | /usr/sbin/sendmail [email protected]
For those who spend a lot of time working on remote servers, telnet can be a tool for sending emails. To use it, first start the terminal and enter the command:
telnet test.server.net 25
If the mail server is running on a different port, replace 25 with the destination port. After connecting, use telnet to greet the server:
Note that some servers will also reply to
ehlo instead of
helo or sometimes either.
Next, set the email sender:
MAIL FROM: [email protected]
Set the recipient of the email:
RCPT TO: [email protected]
Compose the mail with the following format:
DATA Subject: Hello world Hello world, This is the body of the email Proceed here and terminate with . Finally, close the telnet session with quit. QUIT
Mutt is another useful utility for sending and reading emails from the terminal. You may find it similar to the mail command. To install it, run the following command:
sudo apt-get install mutt
To send an email with mutt, use the command:
cat sendmail.txt | mutt -s "Hello world" [email protected]
The above command passes to the mutt utility the content of the sendmail.txt as the email body.
Related: What’s the Difference Between Bash, Zsh, and Other Linux Shells?