Thursday , March 30 2017
Home / How-To's / How to use gmail form terminal (Linux)

How to use gmail form terminal (Linux)

gmailGreat news gmail our favorite mail service can be accessed form command line!!

Now you can access your favorite mail service -gmail form terminal in a linux system with a command line interface. People may wonder why anyone would want to use gmail from the command line, when Google has created such a nice user friendly interface for its users. There are a lot of reasons why one would want to use a CLI (command-line interface) for sending mail. One of the main reasons being the power of shell scripting in a Linux machine. You can automate the entire process by writing small shell scripts and make life much easier. The other reason why I use the command line way is because it uses very less bandwidth. Due the recent D-DOS (Distributed Denial-of-Service) attack on the domain name system by a group of really talented Hackers my computer seemed to take ages to load even Facebook that was when the terminal way of sending mails came in handy.

Lets get started. I use Ubuntu (current version) I’m having issues with this method. If you have some other flavor of Linux you might have to port the commands to work on your system.

Step1 :  Open a new terminal and type the following command to install the package from the Ubuntu repositories.

You have to enter your system password to provide administrative rights. Say ‘y’ when it asks for you choice after the size of the file to be downloaded is determined.
Step2 : Now fire up you favorite text editor by typing

If Vim is not already installed do a ‘sudo apt-get install vim’ install vim and proceed to typing the above command.
Vim is one of the most powerful text editors in the Linux Environment consider mastering it. once inside the file you have to hit ‘i’ to enter the insert mode. then copy the following code into the editor.

then press ESC to enter into the command mode in vim then type ‘:wq’ to save and quit the editor. Now if you do a ‘ls -al’ you should see the file in your home directory.

Step3 : Now this is lame.. any one who has access to you computer could open this file to see your password. So you have to modify the access permit for this file to the root only (you). So type the following command

For more details see manual page of chmod.

Step4 : Now we have configured the gmail settings and your computer should not have any troubles talking to the remote server. We have to set up a command-line email program to talk to the mail-server. So install mailx.

Step5 : Now configure Mailx by creating a file called ‘.mailrc’ in your home directory.

follow the same procedure as in step2 to copy the following code into the newly created text file.

This should be enough to set up gmail to work form the command line. If you are having any problems with the above procedure leave a comment and I will get back to you shortly.

How to use it?

Type this command and hit enter.

Now you have to type your message here.

then hit enter and come to a new line and press CTRL+D you should see a EOT at the line this indicates that you have successfully sent the mail. The -a < attachment > is completely optional so you can omit it. you can also use IO redirection operators in in linux to transfer the contents of a file into a mail like,

This should send the contents of the text file to the receiver. If you are really concerned about your privacy then you have to become the superuser and perform the above steps in the root home directory.

Hope this helps some one out there. Please leave your feedback.

About Siddharth

Siddharth is a Firmware Engineer, techie, and a movie-buff. His interests include, Programming, Embedded Systems, Linux, Robotics, CV, Carpentry and a lot more. At times, you could see some of his sunday projects converge on release quality. You get to know him on the following social channels.

Check Also

breadboard power supply cover photo

DIY: Don’t Buy Breadboard Power Supply Make it!

Most of the time your on a breadboard will just need a 5 volt supply …

  • I get this error, when I try to send the mail:

    /home/chetan/.msmtprc: line 5: unknown command <em

    • Dear Chetan,

      Sorry for inconvenience. The problem was caused because of the HTML code that was visible along with the text. Now I have made the necessary changes. Please copy the content again it will work properly. Feel free to contact me if you are having any other issues.

  • pankaj

    using gmail from terminal is deficult task but if you are a linux user you would love to do that and its really fun to
    and in terms of less bandwith use its also quiet usefull so it’s worth a try. Nice post waiting for the next one

    • Thanks Pankaj. I will do my best to keep you engaged.

  • MSK

    This is so helpful! Thank you!!

  • Ongun Arısev

    I really wonder if this will work when the two-step authentication for Gmail is active, do you have any ideas?

    • Ongun, It doesn’t work with two step authentication.

      • Ongun Arısev


Keep in touch with the current trends!
Did you like this article? Sign up and get our latest posts delivered to your inbox!
  We hate spam and never share your details.