Linux
mail Command in Linux/Unix with 10+ Examples [2023]
Let’s Learn mail Command in Linux/Unix with Practical Examples :
The mail command in linux/unix is a powerful tool once system administrators get to know their way around it. You can use the shell to monitor processes, automate backups, and parse data.
Some command line utilities can be used for convenience when manipulating applications from the somewhat unfriendly screen. When the need to send an email via the command line option, there are numerous ways to go about it.
In this article, you will learn how to send and read emails using the popular commands and learn how to send mail using a shell script. It is also vital that you learn how to send Linux mail attachments. The root user should is the only authorized user who can install these mail packages.
The Common Mail Package Installations:
Command | On Debian/Ubuntu | On CentOS/RedHat |
---|---|---|
mailutils | $ sudo apt-get install mailutils | # yum install mailx |
mutt | $ sudo apt-get install mutt | # yum install mutt |
mpack | $ sudo apt-get install mpack | # yum install mpack |
telnet | $ sudo apt-get install xinetd telnetd | # yum install telnet-server telnet |
curl | $ sudo apt-get install curl | # yum install curl |
sendmail | $ sudo apt-get install sendmail | # yum install sendmail |
swaks | $ sudo apt-get install swaks | # yum install swaks |
Some of the command line options taken by most mail commands are:
- -s denotes the mail’s subject
- -a for denoting attachment
- -c for the copy email address (CC)
- -b for the blind copy email address (BCC)
1. Using mail command in Linux
The Linux mail command in Linux/Unix is quite popular and is commonly used to send emails from the command line. Mail is installed as part of mailutils and mailx packages on Debian and Redhat systems respectively. The two commands process messages on the command line.
To install mail command in Debian and Ubuntu Systems, run
1 |
sudo apt-get install mailutils |
For RedHat & CentOS systems run
1 |
yum install mailx |
If the command is successfully installed, test the application by using the following format and press enter:
1 |
# mail –s “Test Email” you@youremailid.com |
Replace you@youremailid.com
with your email address.
After pressing ‘Enter’, you’ll be prompted for a Carbon Copy (Cc:)
address.
If you wish not to include a copied address, proceed and hit ‘Enter’
Next, type the message or the body of the Email and hit ‘Enter’
Finally, Press Ctrl + D simultaneously to send the Email.
Alternatively, you can use the echo command to pipe the message you want to send to the mail command in Linux/Unix as shown below.
1 |
# echo “sample message” |mail -s “sample mail subject” you@youremailid.com |
To send an email to many recipients run
1 |
# mail –s “test header” you@youremail.com you@yourmailaddresstwo.com |
Let’s assume you have the message that you want to send in a text file. How do you send it ?
To accomplish this, run the command below
#mail –s “test header” you@yourmail.com < body.txt
You can create a text file as shown
1 |
# touch message.txt |
Then add content to the file
1 |
echo “Hello Kanishka! nice to meet you” > message.txt |
Finally, send the message content
mail -s “Hey Kanishka” you@youremail.com < message.txt
To send an attachment use the command below
1 |
mail -s "subject" -a message.txt you@youremail.com |
The -a
flag defines the file attachment.
2. Using the mailx Command in unix/linux
Mailx is the newer version of Linux mail command and was formerly referred to as nail in other implementations. Mailx has been around since 1986 and was incorporated into POSIX in the year 1992.
Mailx is part of the Debian’s mail compound package used for various scenarios. Users, system administrators, and developers can use this mail utility. The implementation of mailx also takes the same form as the mail command line syntax.
To install mailx in Debian / Ubuntu Systems run
1 |
sudo apt-get install mailx |
To install mailx in RedHat & CentOS run
1 |
yum install mailx |
You may use the echo command to direct the output to the mail command without being prompted for CC and the message body as shown here:
1 |
# echo "message body" | mail -s "subject" your@youremailid.com |
3. Using the MUTT Command
Mutt is a lightweight Linux command line email client. Unlike the Linux mail command that can do basic stuff, mutt can send file attachments. Mutt also reads emails from POP/IMAP servers and connecting local users via the terminal.
To install mutt in Debian / Ubuntu Systems run
1 |
sudo apt-get install mutt |
To install mutt in Redhat / CentOS Systems run
1 |
yum install mutt |
Here is how to use it. When using the mutt command, you can send an empty message with the < /dev/null
right after the email address.
# mutt -s “Test Email” you@youremailid.com < /dev/null
The mutt command supports the Linux mail attachment by using the –a
option in this format.
# mutt -s “Sql logs” -a home/Documents/backup.sql you@youremailid.com < /dev/null
The above example sends an attachment containing sql logs to the specified email address.
Mutt command recognizes file type and therefore will not send HTML file as plain text. For instance:
# mutt -s “Email subject” you@youremailid.com < email.html
4. Using mpack Command
The mpack command is used to encode the file into MIME messages and sends them to one or several recipients, or it can even be used to post to different newsgroups.
To install mpack in Debian / Ubuntu Systems run
1 |
sudo apt-get install mpack |
To install mpack in Redhat / CentOS Systems run
1 |
yum install mpack |
Using mpack to send email or attachment via command line is as simple as:
1 |
# mpack -s "Subject here" -a file you@youremailid.com |
5. Using SSMTP Command
The sSMTP command allows the user to send emails from the SMTP server via the command line. For example, to send a test email to you@youremailid.com use the following syntax:
1 2 3 4 |
# ssmtp you@youremailid.com Subject: Test SSMTP Email Email send test using SSMTP via SMTP server. |
Hit CTRL+D to send the email.
6. Using the telnet Command
Telnet is a favourite of the system Administrators who use it to test remote port connectivity. It can also be used to login to the server remotely. Telnet is useful for Linux network troubleshooting email problems.
To install telnet in Debian/ Ubuntu Systems run
1 |
sudo apt-get install xinetd telnetd |
To install telnet in RedHat / CentOS Systems run
1 |
yum install telnet telnet-server |
To listen to a port, you use the following format:
1 |
# telnet localhost smtp |
7. Using Sendmail
This command is another popular SMTP server used in many distributions.
To install sendmail in Debian/ Ubuntu Systems run
1 |
sudo apt-get install sendmail |
To install sendmail in RedHat / CentOS Systems run
1 |
yum install sendmail |
You can use the following instructions to send email using the command:
# sendmail you@youremailid.com < /tmp/email.txt
The above command sends a file saved as email.txt in the temp folder.
8. Using the CURL Command
This utility is known for file transfers between servers and supports many protocols like the SMTP, POP3, HTTP, and the FTP. It mainly used because of its native PHP implementation meaning it supports the native server-side scripting language.
To install Curl in Debian/ Ubuntu Systems run
1 |
sudo apt-get install curl |
To install Curl in RedHat / CentOS Systems run
1 |
yum install curl |
Sending an email with CURL will force you to set up the SMTP connection and turn on access for less secure apps.
1 2 3 |
# curl --url 'smtps://smtp.gmail.com:465' --ssl-reqd \ --mail-from 'you@youremailid.com' --mail-rcpt 'user@niceperson.com' \ --upload-file mail.txt --user 'you@youremailid.com:your-accout-password' |
The above command uses the –-url –-user
flags to define SMTP connection settings. When using the –-ssl-reqd
flag you must ensure that the connection is running on SSL or TLS. The command option requests the connection to be terminated if the server does not support a secure connection, i.e. SSL /TLS.
The –-mail-from
flag specifies a single address that the issued email address should be used to send the mail, in this case it is you@youremailid.com
. The –-mail-rcpt
flag denotes the recipient address, in our case, the mail will be delivered to user@niceperson.com
. You can use this flag multiple times for different addresses.
The command option --upload-file
is used to transfer the specific file “mail.txt” to a remote URL. If no file name is issued you must use a trailing / after the last directory to indicate no file name was given.
9. Using the Swaks Command
The swaks command is flexible, scriptable and a simple SMTP test utility. Very useful in handling SMTP features like the TLS and authentications.
To install swaks in Debian/ Ubuntu Systems run
1 |
sudo apt-get install swaks |
To install swaks in RedHat / CentOS Systems run
1 |
yum install swaks |
A simple implementation of the swaks command is to use the following flags:
- Server –s
- The user –au
- The password –ap
- The address of the recipient
- The –tls is necessary if you are connecting to port 587
1 2 |
#swaks --to recipient@emailaddress.com -s smtp.gmail.com:587 -tls -au -ap |
10. Sending Mail from a Shell Script
With all the email command line basics covered, you should be ready to send your first mail using a shell script. Here is an example of a shell script that can send an email. The output of the command sends the status of your PC to the chosen email.
1 2 |
#!/bin/bash df -h | mail -s “disk space report” you@youremailid.com |
Save the content above on your Linux server and run it to send the output of the df –h
command to your email. A better way of sending emails using a shell script is to let the script write the data to a text file and send it to the specified email. For example:
1 2 3 4 |
#!/bin/bash df -h > /tmp/mail_report.log free -m >> /tmp/mail_report.log mail -s “disk and RAM report” you@youremailid.com< /tmp/mail_report.log |
If you can remember the mutt command, here is how you can use it to send attached systems backups. The script below will send the directory as archived and use the mutt command to send it as an attachment. In this case, “echo” is used to add a blank space to the body of the email.
1 2 3 |
#!/bin/bash tar -zcf /tmp/backup.tar.gz /home/Linux-PC/files echo | mutt -a /tmp/backup.tar.gz -s “daily backup of data” you@youremailid.com |
11. How to Read Mails in Linux
Reading emails from the terminal may not be as enjoyable as sending emails. The mail command in linux/unix will give you version of the mail program installed. All Linux messages are installed in personal mailboxes, for example if your name was Linus, your incoming mails will be stored in the /var/spool/mail/
directory. On the other hand, mails sent to the root user are by default sent to the /var/spool/mail/root
folder. Reading mails from this folder utilises the command cat in the following format:
1 |
# cat /var/spool/mail/root |
The most commonly used syntax utilizes the mail command in this format:
1 |
# mail |
A simple output from the above command gives:
1 2 3 4 5 6 7 8 |
# mail Heirloom mailx version 12.5 6/20/10. Type ? for help. "/var/mail/enlightened": 7 messages 3 unread O 1 Enlightened Sat Dec 6 11:33 21/658 This is the subject O 2 Enlightened Sat Dec 6 11:34 773/25549 This is the subject U 3 Mail Delivery Syst Sat Dec 6 16:50 74/2425 Undelivered Mail Returned to Sender ? |
At the end of the output is a question mark that prompts for your input in the form of a command. Key in the number that you want as listed on the screen above and press enter. When done reading, press on letter Q to load the previous screen. When you are entirely done press letter Z followed by the enter key to bring your back to the list of emails. The mail command reads all emails from the “var/mail/” directory. Always make sure that you are reading from the correct user directory if your server hosts multiple domains.
All these commands should get you started with sending emails from the Linux terminal and understand the basic knowledge of using executable shell scripts. More details on these email commands and their respective flags are found on the “man page.”
How to Troubleshoot Mail Problems
Diagnosing email problems covers a broad range of services. Some errors are easy to handle while others may require some sort of analysis. If everything seems to be working okay, the next step is to check the email service logs found in the /var/log/maillog
file. To view the last part of an email log, use the tail command line utility (by default tail without any option will give the last ten lines). To set the number of lines to view, use the following format:
1 |
# tail –n 30 /var/log/maillog |
The above command displays the last 30 lines of the maillog content. Use the log file to identify any visible error.
Checking mails that are sent through the system can be traced to the /var/spool/postfix
directory. Within this directory, there are subdirectories each denoting an active, deferred or bounce state. Here are some of the common commands used when probing email issues:
1 2 3 4 |
# mailq prints all queued mail # poscat –vq [message-id] prints a particular message # postqueue –f process the queued mail immediately # postsuper –d ALL deletes all queued mails |
-
DevOps55 years ago
Saltstack Tutorial for beginners [2023]
-
DevOps55 years ago
How to build a Docker cron job Container easily [2023]
-
DevOps55 years ago
Docker ADD vs COPY vs VOLUME – [2023]
-
DevOps55 years ago
How to setup Pritunl VPN on AWS to Access Servers
-
Linux55 years ago
Grep Command In Unix/Linux with 25+ Examples [2023]
-
Linux55 years ago
How To setup Django with Postgres, Nginx, and Gunicorn on Ubuntu 20.04
-
Linux55 years ago
Find command in Unix/Linux with 30+ Examples [2023]
-
Linux55 years ago
Whereis command in Linux with 10+ Examples [2023]