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

Canonical announces 12 year Kubernetes LTS

Canonical’s Kubernetes LTS (Long Term Support) will support FedRAMP compliance and receive at least 12…

20 hours ago

Ubuntu Weekly Newsletter Issue 878

Welcome to the Ubuntu Weekly Newsletter, Issue 878 for the week of February 2 –…

2 days ago

How your feedback shapes the way we support open source software

At Canonical, we firmly believe that delivering an outstanding, customer-centric support experience is impossible without…

2 days ago

How To Install osTicket v1.14 On Ubuntu 20.04

I want to share how to install osTicket v1.14 for Ubuntu 20.04 server. osTicket written…

3 days ago

How To Install WordPress On Ubuntu 20.04

Now I want to share how to install WordPress on ubuntu 20.04 server. WordPress is…

3 days ago

How To Install DNS Server (Bind9) On Ubuntu 20.04

Now I want to share the DNS server installation process on your Ubuntu 20.04 server.…

3 days ago