How to use Gmail from an Ubuntu terminal to send emails

As a regular Ubuntu user, you may be well aware of the power of the command line. In this article, we will look at how you can use Gmail from your terminal to send email by setting up the msmtp client. Ok, gmail has a pretty useful and catchy interface, so why do we want to use this command line approach?

Why use a terminal to send letters?

If you are a terminal savvy person, you will not want to leave the command line and go somewhere else to perform any of your daily technical activities. There is always a way to do almost all of our things right in the Terminal. So why sending emails should be different! Using the Terminal makes certain tasks more efficient and even faster. The command line tools do not consume too many resources and, thus, form excellent alternatives to widely used graphical applications, especially if you are stuck on old hardware. Sending emails from the Terminal becomes especially convenient when you can write shell scripts to send emails and automate the entire process.

We ran the commands and procedures mentioned in this article on the Ubuntu 18.04 LTS system.

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

Step 1: Open the Terminal app

Open the “Terminal” application using the keyboard shortcut Ctrl + Alt + T or by accessing it through a search in the Application Launcher as follows:

Step 2: Update Repository Index

The next step is to update the repository index of your system with the following command:

$ sudo apt-get update

This will help you install the latest available software version from the Internet. Please note that only an authorized user can add, remove and configure software in Ubuntu.

Updating Package Lists

Step 3: Install the Msmtp Client

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

$ sudo apt-get install msmtp-mta

Install Msmtp Client

The system may ask you to enter a password for sudo, as well as provide you with the option Y / n to continue the installation. Type Y, then press Enter; software will be installed on your system. However, the process may take some time depending on the speed of your internet.

Step 4. Configure msmtp for gmail.

Now it is time to configure msmtp by telling him our gmail credentials, the port used, the host, and some other authorization and connection information:

Open the file called msmtprc in one of your favorite text editors. I use the famous Nano editor to open the file as follows:

$ nano ~/.msmtprc

Then copy the following code to an 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 
auth on
tls on
tls_trust_file /etc/ssl/certs/ca-certificates.crt
user 
password 
port 587
#set gmail as your default mail server.
account default : gmail

Tip: Instead of entering all the code into your file, you can copy it from here and paste it into the terminal using Ctrl + Shift + V or using the Paste option in the right-click menu.

Now exit the file using the keyboard shortcut Ctrl + X and save the file in the “Save modified buffer?” Window by typing Y, and then press Enter.

Saving a password in text format – any of your files will never be a good idea. Thus, you can protect the file by running the following command:

$ chmod 600 .msmtprc

Step 5: Install heirloom-mailx

At this point, we configured our computer to communicate with the remote Gmail server. Now we need to configure the command line interface, which will allow us to compose emails for sending. Mailx is a program that will allow us to do all this, and here is how we can install it:

$ sudo apt-get install heirloom-mailx

Set family mailx mailx

The system may ask you to enter a password for sudo, as well as provide you with the option Y / n to continue the installation. Type Y, then press Enter; software will be installed on your system. However, the process may take some time depending on the speed of your internet.

Important: If you cannot find the package in already added repositories, open the sources.list file as follows:

$ nano /etc/apt/sources.list

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

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

Also, be sure to run the following command before completing the installation:

$ sudo apt-get update

Step 6: Configure Mailx

Open the file with the name .mailrc through one of your favorite text editors.

$ nano ~ / .mailrc

Then add the following lines to this file and save it.

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

Step 7: Send Email Via Terminal

Now we are ready to send an email through our configured Gmail account to a recipient in any domain. The following is the basic syntax for sending such an email:

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

Sending an attachment with a letter is optional.

I used the following command to send an email:

Send a letter through the Terminal

When you press Enter, you will be allowed to enter the body of the letter. When you finish typing the message body, press Ctrl + D. This will mark the end of the message body and send it to the corresponding recipient ID.

Authenticate as administrator

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

However, you may encounter most common mistakesame as me:

How to use Gmail from an Ubuntu terminal to send emails

This error most often occurs when you do not allow access to less secure applications in your Gmail. This security setting can be changed at the following link:

https://myaccount.google.com/lesssecureapps

When you do this, a notification will be sent to you (mainly on your phone when you set up your phone number using gmail). If you allow this setting change, gmail will allow access to less secure applications, such as the one we use.

Try sending the email again through the command line interface and your email will be successfully sent to the recipient from your Gmail ID.

Now you can include this method in your bash scripts to make it more useful and save time and traffic.

How to use Gmail from an Ubuntu terminal to send emails

Sidebar