Email is still one of the most used features across the Internet for communication purposes. Email not only serves as a great means of quick communication — it is reliable and secure when used correctly. Thanks to the modern GUI-based operating systems and applications, sending email is now a breeze. However, things can get a tad complex when you need to send an email using the Linux terminal.
Sending emails from the command line is needed, especially when you have to create, draft, and send emails from a shell script. Sending emails from the command line also has a few advantages; it can be beneficial for people already working on the Linux command line and for people who need to extract logs from the systems.
Before we proceed with various ways of sending emails through the Linux terminal, we need to understand that every mailing package or application needs a mail server and SMTP to be set up. The process of configuring SMTP for different email providers might be a little bit different from one another. Moreover, if your system does not have any of these commands mentioned below installed, you can download and install them using any of your favorite package managers such as yum, DPKG, RPM, or DNF. Or you can simply run any of these commands mentioned below, and you will be prompted with a message and a command to install it. Simply copy that command and run it to begin the installation process.
Here are the top five most widely used and easy ways to send email through the command line in Linux.
mail is one of the easiest and probably the most straightforward way of sending email from the Linux terminal. Below is an example command to send an email with the subject. The option “s” allows users to send an email with the subject. Users can also specify CC and BCC using “c” and “b” options, respectively.
$ echo “This is the mail’s body” | mail -s “mail subject” [email protected]
To add an attachment to the mail, users can use the “a” option. Here is an example of sending an email with an attachment.
$ echo “this is the mail’s body” | mail -a path/to/file/fileToBeSent.txt -s “mail subject” [email protected]
mutt is another terminal email client command to send mails from Linux’s command-line interface. Mutt supports POP and IMAP protocols for receiving emails and is pretty straightforward. Below is an example command to send an email using mutt command.
$ echo “This is the mail’s body” | mutt -s “mail subject” [email protected]
To know all the options that the mutt command supports, click here.
mpack is a powerful Linux command that encodes files into MIME (multipurpose mail extension) format. It encodes the named files into one or more MIME messages. These resulting messages can be mailed to multiple recipients as a simple mailing command. Using mpack is also a very straightforward approach. Below is an example usage of mpack command to send an email without an attachment.
$ echo “this is the mail body” | mpack -s “mail subject” [email protected]
And to add an attachment to the mail, you can use this command:
$ echo “this is the mail body” | mpack -s “mail subject” [email protected] -a file.txt
Below are some of the important options supported by mpack:
- -s “subject” -> Replace subject with the mail subject.
- -s maxsize -> you can also specify the maximum number of characters allowed in any encoded file. If the file is larger than maxsize, it will then split into segments.
- -n newsgroups -> Using this option, we can send the encoded file to multiple users. Simply replace newsgroups with comma-separated email addresses.
- -o outputfile -> By replacing the outputfile with the file name, the mpack will write the output to that particular file.
As the name itself suggests, sendmail command is used to send an email to one or more recipients. The sendmail command uses whatever network is configured on the system to route the information. The sendmail command can be used extensively to deliver a quick preformatted message.
Here is a simple command to send a mail without subject using sendmail command:
$ echo “Mail body” | sendmail [email protected]
If you wish to add a subject as well as body with multiple lines, you can do the following:
Save your subject and mail body in a file (mail.txt)
Subject: Applying for a job
I would like to apply for the listed job. I feel I will be a great fit for the role
considering my previous experiences and skillset.
Then use the mail.txt to send mail using sendmail command.
$ sendmail [email protected] < mail.txt
ssmtp is a send-only sendmail emulator that picks the email from a centralized mail hub such as POP, IMAP, and NFS mounts. This command uses SMTP to send emails to users from the Linux command line. Like all the tools to send mail using the Linux command line, ssmtp comes with a very simple and straightforward approach.
Below is the code to send a mail using ssmtp with subject and mail body. After you are finished typing the email body or the message, simply hit (CTRL+d) to send the email.
$ ssmtp [email protected] Subject: mail subject This is email body, which can be on multiple lines.
To know more about this command or to check out all the options it offers, click here.
There are several ways to send an email from the Linux command line, and some of these tools can be quite beneficial for you. If you face any difficulties using these commands to send an email, you can check out the official command documents or post a comment to me below and I’ll try to help.
Featured image: Shutterstock
More Linux articles
- Data transfer between Linux and Windows: Step-by-step guide
- Setting up static IP address for Linux: Easier than you think
- Merging and sorting files in Linux: Easier than you think
- Using the Linux command line for basic file handling
- PsychOS: A retro-styled Linux distro for your old PCs