Categories Articles

Sending email from Linux terminal: Efficient and powerful solution

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.

Shutterstock

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

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” user@techgenix.com

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” user@techgenix.com

mutt

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” user@techgenix.com

To know all the options that the mutt command supports, click here.

mpack

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” user@techgenix.com

And to add an attachment to the mail, you can use this command:

$ echo “this is the mail body” | mpack -s “mail subject” user@techgenix.com -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.

sendmail

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 user@techgenix.com

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 user@techgenix.com < mail.txt

ssmtp

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 user@techgenix.com
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

Sukesh Mudrakola

Sukesh is a Technology Consultant by profession and an IT enterprise and tech enthusiast by passion. He holds a Masters degree in Software Engineering and has filled in various roles such as Developer, Analyst, and Consultant in his professional career. He holds an expertise in mobile and wearable technologies and is a Certified Scrum Master.

Share
Published by
Sukesh Mudrakola

Recent Posts

Azure Windows Virtual Desktop: Avoid the fresh hell of stale user sessions

This tutorial on Azure Windows Virtual Desktop and stale users can help you cut down…

41 mins ago

Phishing campaign spoofs texas.gov domain, targets computer vendors

A convincing-looking phishing campaign purportedly from a Texas government agency is targeting computer vendors in…

4 hours ago

Top 5 cybersecurity innovations and why they’re drawing in the money

With attackers making use of every vulnerability, our sense of security has turned into insecurity.…

7 hours ago

Have you really tested your disaster recovery plan?

How do you simulate a disaster to see whether your disaster recovery plan is ready…

1 day ago

Using conditions in ARM templates when deploying infrastructure-as-code

This Quick Tip shows you a neat little coding trick that will help you when…

1 day ago

Full circle: On-premises Exchange to Microsoft 365 — and back again

Migration from on-premises Exchange to Microsoft 365 may not be a one-way street. What about…

1 day ago