In this short tutorial, you will learn how to send one-liner emails using sSMTP from your Linux Ubuntu/Debian terminal. But why on earth would we want to send Emails from the terminal ? Well there could be many reasons. For instance you would want to build an application which sends Emails … You will also be able to attach files to the Email.
Before introducing sSMTP, let’s first mention the SMTP protocol. The Simple Mail Transfer Protocol (SMTP) is a protocol used to send and receive emails. Every time you send an email using for instance Gmail, Yahoo and the like, this protocol or service is invoked. Gmail for example uses smtp.gmail.com as their smtp server in order to allow users to access their Email service from other clients.
SSMTP is a simple and lightweight MTA (mail transfer agent) that is used to deliver mails from a machine to a mail hub (that is, SMTP server). It is a client program that forwards your mail to the vendor’s SMTP server which reroutes it to the recipient’s email vendor before sending it to the recipient.
Open up your terminal in order to Install ssmtp using the commands below:
sudo apt-get update
This is to update your repository index of packages. Now issue the command below to install the SMTP client, .i.e. sSMTP :
sudo apt-get install ssmtp
Hit Y when prompted.
In order to configure SSMTP, edit the /etc/ssmtp/ssmtp.conf in your preferred text editor and insert the following lines :
Save and exit. Make sure to assign the right permissions to this file since it contains your password or otherwise you may want to create a new email @ gmail for the sole purpose of sending emails from the terminal.
Now we are ready to send an email. To do this, run the command below :
echo “your text” | ssmtp firstname.lastname@example.org
Or you can also use :
printf “your text” | ssmtp email@example.com
If like me , you get the error shown below :
The error above is mostly likely due to you not having allowed access to less secure apps on your gmail account. You would have also received a notification from gmail related to this. This security setting can be modified through the following link:
Once you have done this, you will get a notification (probably by phone if not by email). Once you enable the change above, gmail will then allow access to less secure apps.
Now try again to send the email from your terminal and this time it will go though.
In order to send an attachment file, you would first to install mpack:
sudo apt-get install mpack
Now run the command below :
mpack -s “your text” /your/file/path/here firstname.lastname@example.org
If you receive this error then you may want to refer to the previous section.
Sending emails from the command line can be useful when you need a shell script to send emails manually and automatically. You have seen how to use sSmtp to send one-liner emails as well as how to attach a file using mpack. If you have other methods that enable users to send email, do not hesitate to mention them in the comments section.