How to send one-liner emails from Linux/Ubuntu terminal – Embed email sending feature into your application

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.

What is sSMTP?

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.

Read: How to Configure Sendmail on Ubuntu 22.04

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.

Read: How to quickly send a text from Ubuntu to Android using a three-line Bash script

sSMTP Installation

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.

Configuring sSMTP

In order to configure SSMTP, edit the /etc/ssmtp/ssmtp.conf in your preferred text editor and insert the following lines :

root=your_email@gmail.com
mailhub=smtp.gmail.com:465
FromLineOverride=YES
AuthUser=your_email@gmail.com
AuthPass=your_password
UseTLS=YES

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.

Read: How to tweet from the Linux Command Line

Sending an email

Now we are ready to send an email. To do this, run the command below :

echo “your text” | ssmtp your_receiver@gmail.com

Linux send email command

Read: How to set up an SMTP server on Ubuntu 22.04

Or you can also use :

printf “your text” | ssmtp receiver@gmail.com

If like me , you get the error shown below :

Ubuntu send mail

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:

https://myaccount.google.com/lesssecureapps

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.

Sending a file as an attachment

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 abc@domain.com

If you receive this error then you may want to refer to the previous section.

Read: How to display Images in the command line in Linux/Ubuntu 

Conclusion :

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.


If you like the content, we would appreciate your support by buying us a coffee. Thank you so much for your visit and support.

 

Nikolaus Oosterhof

Nikolaus holds a degree in software development and has a strong passion for all things tech-related, especially gadgets with screens. Though he is nostalgic for older phone models, he's a retired gamer and continues to enjoy programming in open-source environments. Additionally, Nikolaus enjoys writing about Linux, macOS and Windows and has experience designing web pages.

This Post Has 2 Comments

  1. ifred

    How to send a one line email:

    > mail recipient@address

    Type subject. Type one line message. Press enter. Type a single fullstop and press enter.

    If you wish to attach a file:

    mail -a $filename recipient@address

    Simples. 😉

  2. Monty

    Oh gosh.. a perfect tutorial on how to send email via the command line.. I have been hacking around for several hours trying different things.. This guide did it right. And it had the extra information on dealing with the error (no app password generated) and a link where to go to resolve it..

    You are appreciated!

Leave a Reply