Categories: TutorialsUbuntu

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.

Sponsored

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.

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

Or you can also use :

Sponsored

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.

The post How to send one-liner emails from Linux/Ubuntu terminal – Embed email sending feature into your application appeared first on net2.

Ubuntu Server Admin

Recent Posts

Building RAG with enterprise open source AI infrastructure

One of the most critical gaps in traditional Large Language Models (LLMs) is that they…

4 hours ago

Life at Canonical: Victoria Antipova’s perspective as a new joiner in Product Marketing

Canonical is continuously hiring new talent. Being a remote- first company, Canonical’s new joiners receive…

1 day ago

What is patching automation?

What is patching automation? With increasing numbers of vulnerabilities, there is a growing risk of…

2 days ago

A beginner’s tutorial for your first Machine Learning project using Charmed Kubeflow

Wouldn’t it be wonderful to wake up one day with a desire to explore AI…

3 days ago

Ubuntu brings comprehensive support to Azure Cobalt 100 VMs

Ubuntu and Ubuntu Pro supports Microsoft’s Azure Cobalt 100 Virtual Machines (VMs), powered by their…

3 days ago

Ubuntu Weekly Newsletter Issue 870

Welcome to the Ubuntu Weekly Newsletter, Issue 870 for the week of December 8 –…

4 days ago