How to send G-Mail from Linux Terminal

In this post related to Linux that will help you to send the G-mail right from your powerful terminal. 5 minute workout will setup everything here. see more head how
Great!! You choose to either send mail or learn to send the mail from the most powerful tool in Linux, i.e. Terminal. Linux terminal is the smart powerful command prompt like application which simulates the commands enter by the user and able to perform almost anything on the single screen console.

In this post I am going to tell you how you can send Mails especially Gmail from your Linux terminal.

Ensuring Security

Before you go ahead to configure your Linux to do all this, you need to hold the updated security certificate from Gmail to ensure the privacy and perform SSL data encryption and transportation. To check the certificate file Equifax_Secure_CA.crt file under the path /usr/share/ca-certificates/mozilla/ . If you have this file, we can talk ahead, else you have to see step 6-7 here. This will help you to know and fix this problem.

Installing tools

For doing this, you need to install two simple tools in your Linux distribution. First one is Msmtp, which is a tool to use SMTP, and mailx, a utility to send and receive the mails.
Step to install these tools
In Debian Packages(Linux Mint, Ubuntu, ) use the following commands

user@system~#: sudo apt-get install msmtp heirloom-mailx        [Enter]

In Red hat distributions you can use the following command
user@system~#: sudo yum install msmtp mailx                [Enter]


Configuring the Mail Account

After installation, you need to create the configuration file for your account to work with. For this you need to make the configuration file in your home directory.
Perform the following command
user@system~#: cd
user@system~#: touch .msmtprc

Now open this file with your favorite text editor and paste the following lines in it

# Gmail account starts
# account name which must be unique for each account
account gmail1
auth on
#Gmail SMTP host name
port 587
#sets Transport Layer Security on
tls on
# location of tls certificate file for Gmail (change this parameter if your certificate file is stored at some other location in your File-system)
tls_trust_file /usr/share/ca-certificates/mozilla/Equifax_Secure_CA.crt
#your email id here (e.g.
#your password here
#email id of the sender that is you again
# Gmail account end
#So we have added one account. Similarly we can add more accounts by repeating and modifying above code for each new account. Make sure you give unique name to each account.
#set default account to be used when no account is specified (Not necessary for single account)
account default: gmail1

In the above text, don’t forget to replace the capital latter string with appropriate details
Save this file and make the permission to use by owner only.
For this run the following command

user@system~# chmod 600 .msmtprc

Now you need to configure the Mailx utility. To create the mailx configuration, you need to create a file .mailrc in your home directory.
After creating the file, open it with any text editor and paste the following lines in it

set from="YOUR_EMAIL_ID"                    #your email id here
set sendmail="/usr/bin/msmtp"       #location of msmtp's binary executable
set message-sendmail-extra-arguments="-a gmail1" #additional arguments to msmtp goes here (optional) (-a indicates account name to be used to send mails)

Again don’t forget to replace the capital string with the detail it asks for.
Now you are ready to send the e-mail from your terminal
Let’s see how you can do this.

These files are to be saved in user’s home directory with permission such that only user and root can view the files


Sending Emails

Lets start with first mail
Run terminal and let the following command flow.

user@system~$: mailx RECIPIENT_EMAIL_ID

Replace RECIPIENT_EMAIL_ID with the mail id you want to send message to
After this, you will be prompt to enter the subject and body of the mail you are going to send.
Start typing the message and once you are finished typing your message, press enter and [Ctrl]+D to send and exit.
Wait, if you aren’t end with any error message, your mail has been sent successfully.

Some useful Mailx tips

I’m listing some of the most usable command in mailx utility and its argument.
-s specify the subject of the mail.
mailx –s “This is subject of mail”
-a to attach the files to the outgoing mail, need to mention the path
mailx –a /document/script.txt username@hostname
you Can see more usage of the mailx by either using man pages or help topics
for that run following
user@system~$: man mailx
user@system~$: mailx –help

Want to remove the mail setting from your computer?
Delete the configuration files you have made earlier and remove the packages as follows
For debian distribution

user@system~$: sudo apt-get remove msmtp heirloom-mailx

And for Red hat distribution

user@system~$: rm ~/.mailrc ~/.msmtprc

if you love the terminal and gmail, you defiantly liked this post.
[IMPORTANT:: if you are using Two step verification, you may need to generate the application specific password Learn more. ]

Subscribe Author for Updates
Dheeraj thedijje

Dheeraj thedijje

Powered by Blogger.