Article

Linux mail command examples – send mails from command line

Topic: LearningPublished September 12, 2019

Legacy signals

Legacy popularity: 912 legacy views

There are plenty of ways for sending an email while using GUI, using a browser or with an email client. But options are limited when it comes to command-line interface aka CLI. In this tutorial, we are going to discuss how to send mail from terminal or CLI of a Linux system. rnThere are plenty of commands that can be used to send mail from the terminal, like Sendmail, mutt, etc but for this tutorial, we will be discussing the use of MAIL command to send mail from the terminal. Mail command can be used directly from the terminal or we can also use it in our BASH scripts.rnSo let’s start the tutorial with the installation of mail command, Recommended Read mail-command-in-linux-with-examples On many Linux distributions, mail command is available by default but if that’s not the case with your Linux system, we can easily install it using the following commands, CentOS/RHEL/Oracle Linux/Amazon LinuxrnWe can use yum for the installation mail command on any of the Linux distros mentioned above, # yum install mailx Ubuntu/Debian/Linux MintrnFor these Linux distros, we can use apt-get to install mail command on our systems, # Sudo apt-get install mailutils FedorarnFor installing mail command on fedora, execute the following command from the terminal, # dnf install mailx Now let’s discuss some examples on how to use mail command for sending mails from CLI, Examples to send mail from terminalrn1- Sending a simple mailrnTo send a simple mail with some content in the body, execute $ mail -s “test mail” admin@linuxteacher.com here, -s option is used for mentioning the subject of mail followed by email address on which we need to send the mail. Now after you execute the above command, we need to enter the body content & once we are done, press CTRL + D to exit and send the mail. We can also use the following single line command to send mail, $ mail -s “test mail” admin@linuxteacher.com <<< “This is the body of the mail” 2- Send mail to multiple recipientsrnFor sending mail to more than once user, mention all email addresses followed by a comma. For example, $ mail -s “test mail” user1@linuxteacher.com,user2@linuxteacher.com.,com,user3@linuxteacher.com 3- Sending a mail with attachmentrnNow to send a mail with a file as an attachment, we will use option ‘A’ with the mail command. For example, we need to send a file named ‘test.txt’, so we will use the following command, $ mail -s “File Attached” admin@linuxteacher.com -A test.txt 4- Sending a file content with mailrnTo send the contents of the file using mail command, we will use the following, $ mail -s “File output” admin@linuxteacher.com< /home/linuxteacher/file.txt 5- Sending a mail with an output of a commandrnWe can also send the output of a command as the body content of a mail. For example, we need to send output of ‘du -h’ on a remote system, use $ du -h | mail -s “HDD USAGE”admin@linuxteacher.com Similarly, we can also make use of echo command to send a mail, $ echo “This is a body of the mail” | mail -s “test mail” admin@linuxteacher.com 6- Sending mail with additional headers like from addressrnTo send a mail with additional headers, we will use the option ‘a’ with the mail command. For example, we need to mention the from address & send it with our mail, $ mail -s “Test mail “ -aFrom:DAN\ admin@linuxteacher.com That’s it guys, we now end this tutorial on how to use the mail command to send mail from terminal or CLI. These were some common examples only, there are plenty more applications for the mail command that you can achieve. Please let us know if you need something specific you want to achieve, you can send your queries using the comment box below

Further reading

Further Reading

4 total

Article

Design has been at the centre of our perception of information, brands and entertainment in the world that is now digital first. Whether you browse social media, watch YouTube videos, or pass a billboard, you probably have encountered both visual communication design and graphic design, yet most likely, you were not aware of the distinction between the two. Although these two fields are closely related and in many instances overlap, they have different purposes and require di

July 21, 2025

Article

Dr. Ann Marie Egloff understands that engaging in philanthropy isn’t about just donating funds; it’s about the values and intention behind that action. Whether you’re donating money, volunteering time, or sharing your skills, giving back can take many forms and fit into any lifestyle. According to Dr. Ann Marie Egloff, the most rewarding experiences often come from aligning your efforts with causes that resonate personally. By clarifying your motivations, setting though

May 27, 2025

Article

Learning to play the guitar is an exciting and rewarding experience. Whether you’re a complete beginner or have been playing for years, guitar lessons can help you unlock your full musical potential and take your skills to the next level. But what exactly do guitar lessons offer, and how can they benefit you? Let’s dive in! 1. A Structured Learning Path One of the biggest advantages of taking guitar lessons is the structure they provide. While it’s easy to get lost in the s

February 1, 2025

Article

SVPT Saraswati School is widely regarded as one of the Best CBSE Schools in Thane, offering a holistic and well-rounded education that goes beyond academics. Situated in a peaceful and conducive learning environment, this institution provides a blend of modern infrastructure, dedicated faculty, and extracurricular activities, ensuring that students develop both academically and personally. A Legacy of Excellence in Education:rnSVP Saraswati School has built a reputation over

November 26, 2024