Did you know that you can send mail from Linux command line? That’s not surprising, I believe. The main question is why would you take the trouble of sending emails from Linux terminal?
There could be various reasons for that. Suppose you have a Linux server that you need to keep secured. You use Fail2Ban on it to thwart unwarranted logins. With the mail setup, it can automatically send you emails in case there is an alert. This is one of the many use cases where sending mails from Linux can help you big time.
Let’s see more about sending emails from Linux command line.
What is SSMTP and how does it work?
SMTP (Simple Mail Transfer Protocol) is one of the protocols (set of rules and formats for data and communication) used in sending and receiving emails. Though vendors like Outlook, Gmail usually use proprietary protocols, they provide SMTP services so that user can access their emails from any client.
There are two software pieces needed: SMTP server (hosted by email vendor) and SMTP client that you have in your system.
SSMTP is one of such client programs that can help you to send the emails. When you send a mail, your client forwards it to your vendor’s SMTP server which then sends it to recipient’s email vendor. Finally, it will be forwarded to the recipient.
If you want to know more about SMTP, please use this wiki article about SMTP.
Let’s get started with SSMTP right way.
Sending emails from Linux terminal using SSMTP
Let’s see how you can send mail in Linux command line with the help of SSMTP.
SSMTP is available as a package in most of the Linux distributions. Browse your distro’s repo to make sure. In Ubuntu 18.04, you can install SSMTP as follows:
sudo apt install ssmtp
After this is done, we need to configure SSMTP.
Since this article main focus is to send a mail, we are sticking to the basic setup of SSMTP. You can use this to only send a mail. Each user has to specify email and password each time they send a mail. Let’s assume, everyone uses the same SMTP server.
Some of the other articles you might find online will guide you to store username and password in the configuration file (/etc/ssmtp/ssmtp.conf). It will enable all the users to send an email using the credentials in the config without forcing each user to have own email and password.
“Linuxhandbook advise you not to do any such configuration unless you are very sure.”
You need to edit the conf file:
sudo gedit /etc/ssmtp/ssmtp.conf
mailhub=smtp.gmail.com:587 UseTLS=YES UseSTARTTLS=YES
Even if you delete the entire config and replace it with just the above snippet, it should it work perfectly. If not tell us in the comments.
If you use any other email vendor, replace mailhub in the above snippet with the appropriate server:port value. If you face difficulties, please be sure to let us know in the comments.
Sending Email in Linux through SSMTP
Let’s construct a message that needs to be sent in a file named mail.txt.
To: email@example.com Subject: Linux is best Linux stood the test of time. We thank everyone who ever contributed, advocated, used or did anything that made what Linux is now. Thanks and regards, Linux Folks
In order to sent this mail, you need to use the format as follows.
ssmtp -au EMAIL_ADDRESS -ap EMAIL_PASSWORD firstname.lastname@example.org < mail.txt
Replace EMAIL_ADDRESS, EMAIL_PASSWORD with the values. You might notice one thing email@example.com is mentioned both in the message and command.
This is because the address used in ssmtp command will be used as bcc and the address mentioned in the message will be used as to address.
I hope you found this article useful. If you liked it, please share it on social media. If you have suggestions, feel free to let us know in the comments.