While working with the command line, there are times when you might want to manually send an email to communicate one-liner information, say a complex command or an important note to yourself or a friend. Normally, that requires you to open a web browser, log in to your email account, frame an email containing the required information, and then send it.
That’s too long of a process for a small thing, isn’t it? What if you could do that from the command line itself? Yes, it is possible, and we’ve already discussed a couple of ways to do that in Linux. In this article, we will discuss three more ways to send email from the command line in Linux.
Sendmail, as you might know, is the classical SMTP server from the world of UNIX. It usually comes pre-installed on many Linux distributions, but if that’s not the case with your Linux box, you can easily install it from standard repositories. For example, users of Debian-based systems, like Ubuntu, can download and install it using the following command:
sudo apt-get install mailutils
Once installed, you can easily and quickly send an email using the
sendmail command. Here is an example:
echo "This is a test email again"| sendmail [receivers-email-address]
The above command sends an email containing the text “This is a test email again” to “[receivers-email-address]” (make sure to replace it, including the brackets, with the actual email address of the receiver].
When I used my email address in the above command, I got the following email:
As in my case, the email could land in your spam folder, so be sure to check there.
The command we just used contained only the body of the email. You can also add more details like subject, from, and more. For example, the following command contains these extra fields, and can be run as a script:
echo "From: email@example.com To: firstname.lastname@example.org Subject: Test HTML e-mail This is a test email."| sendmail -t
Here is the snapshot of the email I received when the above command was run:
You can also use the Telnet command to send an email from the command line. To do so, run the following commands (make sure to replace “[senders-email-address]” and “[receivers-email-address]” with sender’s and receiver’s email address, respectively):
telnet localhost smtp ehlo gmail.com mail from: [senders-email-address] rcpt to: [receivers-email-address] data
At this point, you should see a prompt where you need to enter the body of the email and make sure the message ends with a newline followed by a dot (.). Once done, press enter to send the email, and then type
quit to exit the application.
In my case, I received the following email:
SSMTP is a tool which delivers email from a local computer to a configured mailhost (mailhub). Debian users can download and install the program using the following command:
sudo apt-get install ssmtp
Once installed, open the
/etc/ssmtp/ssmtp.conf file and edit it to make sure that it contains the following information:
mailhub=smtp.gmail.com:587 UseSTARTTLS=YES AuthUser=[senders-email-address] AuthPass=[senders-email-login-password] TLS_CA_File=/etc/pki/tls/certs/ca-bundle.crt
As always, make sure to replace “[senders-email-address]” and “[senders-email-login-password]” with appropriate values. Now, to send an email using
ssmtp, use the following command:
ssmtp [receivers-email-address] Subject: [subject-line-here] [Body of email]
Again, replace “[receivers-email-address]”, “[subject-line-here]”, and “[Body of email]” with appropriate values and send the email by pressing “Ctrl + D.”
If you receive the following error,
ssmtp: Authorization failed (534 5.7.9 http://support.google.com/accounts/bin/answer.py?answer=185833 bk8sm8525341pad.28 - gsmtp)
then that’s likely because Gmail is not treating
ssmtp as a secure application. To get rid of this error, you need to change a particular setting in your Gmail account – for more information on how to do that, and its potential risks, refer to this Google support page.
When I executed the above command, I got the following email:
With this, we have discussed a total of 5 command line tools that you can use to send email from the Linux command line. An important point worth mentioning here is that aside from using these commands to send emails manually from the command line, you can also use them to generate notification emails from scripts.
Have you ever used any of these tools? How was your experience? Share your thoughts in the comments below.