6 Methods to Send Email From Command Line in Linux
Sending Email from Command Line?! It must be interesting for Linux administrators, developers, and users. Join us with this tutorial to learn How to Send Email From Command Line to make easier this essential task of the digital age. Also, you will learn to send Linux email attachments with the help of mail commands. There are enough reasons to use the command line tool to send email in Linux. Tasks would be completed faster without using graphical applications which use too many sources. You just need to access an SMTP server and write a shell script. If you need to freshen your knowledge, refer to the Install SMTP Server on ubuntu tutorial.
In the end, you will know how to use ssmtp, muut, sendmail, mail, xmail, and mpack commands to send emails in Linux.
Multiple Ways to Send Email in Linux
It’s been years since online communications are being done with sending and receiving Emails. One of the first actions after buying Linux VPS is to find an ideal Linux Mail server to start having digital conversations with your considered companies, coworkers, and friends all over the world.
Stay with us to review 6 different methods of sending email in Linux using the Command line from your configured email ID. Once you selected any one of the following methods to send email from command line, open your Terminal application and use your preferred method.
Method 1. Using ssmtp Command in linux
As the first way to send email from command line in linux ubuntu, let’s start with ssmtp command. ssmtp is a send-only sendmail emulator for computers that ordinarily get their mail from a centralized mail hub. If this tool is not already installed on your system. use the commands below to install it:
$ sudo apt- get update
$ sudo apt-get install ssmtp
To compose and send an email using this method, run:
$ ssmtp [email protected]
After pressing Enter, type the subject in the following format:
Subject: Linux topics of operavps blog
When you press Enter, you will be able to type the email’s body. Once you’ve finished typing the email body, press Ctrl+D. This will close the email content and send it to the appropriate receiver ID.
Method 2. Using muut Command
Mutt is a lightweight and simple but capable text-based program for reading and sending electronic mail under UNIX operating systems, with color terminal support, MIME, OpenPGP, and a threaded sorting mode. Mutt is mostly used to read emails from local user mailboxes from the Linux terminal.
To send email from command line using muut command, you need to install it first:
$ sudo apt install mutt
If you want to send an email without an attachment, use the following syntax:
$ mutt -s "Your Subject" [email protected] < /dev/null
Mutt can also be used to attach a file, as shown below.
$ mutt -s "Your Subject" -a /path/to/file [email protected] < /dev/null
Method 3. Using sendmail Command
Sendmail is a popular general-purpose internetwork email routing facility that supports a wide range of mail-transfer and delivery techniques, including the Simple Mail Transfer Protocol (SMTP), which is used to transmit email over the Internet.
Run the command below to install sendmail in ubuntu/debian Systems:
$ sudo apt install sendmail
Sendmail is an SMTP server, so you must have SMTP installed on your machine. You can then put it to use as follows. Make a text file with the following format:
$ sendmail email_address < file
Method 4. Using mail Command in Liux
Another method to send email from command line in Linux is to use mail command. The Linux mail command is well-known and is frequently used to send emails from the command line. If you use mail
from command line, you do not need to configure an SMTP server anymore.
On Debian and Redhat systems, mail is installed as part of the mailutils and mailx packages.
Run the command below to install mailutils in Debian and Ubuntu Systems:
$ sudo apt install mailutils -y
Also, you can install the mail command on CentOS and RedHat distributions:
$ yum install mailx
To check if the mail command is installed successfully, you can test it by running the following format syntax and hitting enter.
$ mail –s "Test Email" email_address
Keep in mind to replace email_address
with your email address.
After pressing the “Enter” key, you’ll be asked for a Carbon Copy (Cc:) address. Proceed and press ENTER if you do not want to include a copied address. Then, input the message or body of the email and press ENTER. Finally, press Ctrl + D at the same time to send the email.
If you have a file to attach, you can send it using the syntax below.
$ mail -s "subject" -A message.txt email_address
-A
in mail command helps to attach a file.
Method 5. Using mailx Command
Mailx is also known as a Mail User Agent program. It is a newer version of the mail command, which has been around since 1986 and was included in POSIX in 1992. This mail utility is available to users, system administrators, and developers for sending and receiving mail. The mailx implementation follows the same format as the mail command line syntax.
To install mailx in Debian/Ubuntu systems, use the following command:
$ sudo apt install mailx
As one of the methods to send email from command line, mailx is used to direct the output to the mail command without being prompted for CC, and the message body:
$ echo "message body" | mail -s "subject" email_address
Method 6. Using mpack Command
Linux send email from command line is fast and easy. The latest explained command of this tutorial is mpack. The mpack command encodes the file into MIME messages and transmits them to one or more recipients, or it can even post to several newsgroups. run the command below to install mpack in Debian / Ubuntu systems:
$ sudo apt install mpack
You can simply run the following command to send email with or without attachments using mpack command:
$ mpack -s "Your Subject" -a file email_address
Command Line Options to Send Email From Command Line
Command Line options | Description |
-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) |
That’s that! At this point, you got familiar with 6 different methods you can simply use to Send Email From Command Line in Linux.
FAQ
What is -a in muut command syntax?
It identifies the attachment(s).
Conclusion
In this article, you reviewed 6 Methods to Send Email From Command Line in Linux. You may now feel that using the command line emails clients is simpler than other methods. By the way, system administrators usually use the telnet command to test remote port connectivity or to remotely log in to the server. Most Linux newbies are unaware that sending email over telnet is also possible, which is a better technique to troubleshoot email-sending issues.
If you use any one of the explained methods in this tutorial, then you can send email from command line without any errors but do not hesitate to contact us if you encounter any problems. Our technical support team will try their best to solve your problems. Also, if you know some commands to send email in ubuntu using command line, then the comment section is all yours.