As a regular Ubuntu user, you might be well aware of the power of the command line. In this article, we will explore how you can use Gmail from within your Terminal in order to send emails, by configuring the msmtp client. Well, gmail has a pretty useful and catchy interface, so why do we want to opt for this command line approach?

Why use the Terminal for Sending Emails?

If you are a Terminal-savvy person, you wouldn’t want to leave the comfort of the command line and go somewhere else to do any of your daily technical activities. There is always a way to do almost all of our stuff right inside the Terminal. So, why should sending emails be any different! Using the Terminal makes certain tasks more efficient and even faster. The command line tools do not use too many resources and thus form great alternatives to the widely used graphical applications, especially if you are stuck up with older hardware. Sending emails from the Terminal becomes especially handy when you can write shell scripts to send emails and automate the whole process.

We have run the commands and procedures mentioned in this article on a Ubuntu 18.04 LTS system.

Please follow these steps, one by one, in order to install and configure msmtp so that you can send emails from your Terminal:

Step 1: Open the Terminal application

Open the Terminal application either by using the Ctrl+Alt+T shortcut or by accessing it through the Application Launcher search as follows:

Open Ubuntu Linux terminal

Step 2: Update the repository index

The next step is to update your system’s repository index through the following command:

$ sudo apt-get update

This helps you in installing the latest available version of a software from the Internet. Please note that only an authorized user can add, remove and configure software on Ubuntu.

Update package lists

Step 3: Install Msmtp client

Now you are ready to install the msmtp client from the Terminal; you can do so by running the following command as sudo:

$ sudo apt-get install msmtp-mta

Install Msmtp client

The system might ask you the password for sudo and also provide you with a Y/n option to continue the installation. Enter Y and then hit enter; the software will be installed on your system. The process may, however, take some time depending on your Internet speed.

Step 4: Configure msmtp for gmail

Now is the time to configure msmtp by telling it our gmail credentials, the port to use, the host, and some other authorization and connection details:Advertisement

Open a file named msmtprc in one of your favorite text editors. I am using the famous Nano editor to open the file as follows:

$ nano ~/.msmtprc

Then, copy the following code in the empty file:

#Gmail account
defaults
#change the location of the log file to any desired location.
logfile ~/msmtp.log
account gmail
auth on
host smtp.gmail.com
from [email protected]>
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
user [email protected]>
password 
port 587
#set gmail as your default mail server.
account default : gmail


Tip:
Instead of typing the entire code in your file, you can copy it from here and paste in the Terminal by using the Ctrl+Shift+V, or by using the Paste option from the right-click menu.

Now, exit the file through the Ctrl+X shortcut and save the file on the “Save modified buffer?” prompt by typing Y and then hitting Enter.

Saving your password in text format is any of your files is never a good idea. So, you can secure the file by running the following command:

$ chmod 600 .msmtprc

Step 5: Install heirloom-mailx

At this point, we have configured our computer to talk to the remote Gmail server. What we need to do now is, set up a command-line interface that will let us compose emails to be sent. Mailx is the program that will let us do all this, and here is how we can install it:

$ sudo apt-get install heirloom-mailx

Install heirloom-mailx

The system might ask you the password for sudo and also provide you with a Y/n option to continue the installation. Enter Y and then hit enter; the software will be installed on your system. The process may, however, take some time depending on your Internet speed.

Important: If you are not able to find the package in your already added repositories, open the sources.list file as follows:

$ nano /etc/apt/sources.list

Then, add the following line to add the rusty-security main universe repository from where we will install the mailx utility.

deb http://security.ubuntu.com/ubuntu trusty-security main universe

Also, do not forget to run the following command before performing the installation:

$ sudo apt-get update

Step 6: Configure Mailx

Open a file named .mailrc through one of your favorite text editors.

$ nano ~/.mailrc

Then, add the following lines in that file and save it.

set sendmail="/usr/bin/msmtp"
set message-sendmail-extra-arguments="-a gmail"

Step 7: Send an Email through the Terminal

We are now ready to send an email through our configured gmail account to a receiver on any domain. Following is the basic syntax for sending such an email:

$ mail -s "subject" -a "attachment-if-any" "[email protected]"

Sending an attachment along with the email is optional.

I used the following command to send an email:

Send an Email through the Terminal

As you hit Enter, you will be allowed to enter the body of the email. Once you are done with entering the email body, hit Ctrl+D. This will mark the end of the email body and send it to the respective receiver ID.

Authenticate as admin

The EOT at the end of the output will indicate that your email has been sent.

However, you might encounter the most common error, same as I did:

This error is mostly encountered when you have not allowed access to less secure apps on your gmail. This security setting can be changed through the following link:

https://myaccount.google.com/lesssecureapps

When you do so, a notification will be sent to you (mostly on your phone, when you have configured your phone number with gmail). When you permit this change of setting, gmail will allow access to less secure apps such as the one we are using.

Try sending the email again through the CLI and your email will be successfully sent to the receiver from your gmail ID.

You can now incorporate this method in your bash scripts to make it more useful and time & bandwidth saving.

How to use Gmail from the Ubuntu Terminal to send Emails