Using fetchmail to download emails from your Gmail account

May 30, 2005
In Linux there is a wonderful program called fetchmail. This program is used to connect to a remote mail server, login to your email account and retrieve new mails from your inbox. Here I will explain how you can retrieve mails from your Gmail account using fetchmail.
First you need the necessary data like the pop server address, the port number and so on of the remote mail server. For Gmail, the values are as follows:

Incoming mail (POP3) server : pop.gmail.com
Port No: 995
Use SSL : YES

Outgoing mail (SMTP) server : smtp.gmail.com
Use authentication : YES
Use STARTTLS (SSL) : YES
Ports : 465 or 587

Account Name : gmail username (including @gmail.com)
Email address : username@gmail.com
Password : Your gmail password.
Now check if you have fetchmail installed in your machine :
$ rpm -q fetchmail
fetchmail-6.2.5-2
If you don't have fetchmail installed, now is the right time to install it. I am not going to go into the details of installing fetchmail here. Usually in majority of the Linux distributions, you will have fetchmail installed by default.
Assuming you have fetchmail installed on your computer, create a file by name .fetchmailrc in your home directory by issuing the following commands :
$ cd
$ touch .fetchmailrc
Make sure the permissions for the file .fetchmailrc are read-write-execute for the user , execute for the group and none for others.
$ chmod 710 .fetchmailrc
Now fire up your favourite editor and enter the code shown below in the just created .fetchmailrc file .
#File : .fetchmailrc
set postmaster "local_user"

poll pop.gmail.com with proto POP3 and options no dns
user 'username@gmail.com' is 'local_user' here options ssl
In the above listing, we set the the postmaster to the name of the local user - it could be either 'root' or your local user account name. It means that any undelivered email send to postmaster will be redirected to local_user. The next line asks fetchmail to connect to the gmail account using ssl and to retrieve the mails in username@gmail.com account and store it in the spool of the local_user . 'no dns' specifies that it is a dial-up account and your machine is allocated a dynamic IP address.
Now when ever you want to retrieve email, first connect to the internet and then just open a terminal , and type :
$ fetchmail -v --keep
This will ask for your Gmail account password and then fetch your mail from the server and store it in your machine's mail spool. now you can use any email client - mutt, pine etc - to read your mail offline.
Note : There are a lot of options that you can use in fetchmail. To know more about the options, check the man page of fetchmail.

3 comments:

  • This is a nice easy way to trace Fetchmail:

    strace -e trace=open fetchmail -vc -L /dev/stderr -u username@gmail.com --ssl -P 995 pop.gmail.com

    This command runs Fetchmail to check if there is any new mail, in the foreground, verbosely. Choosing different options and re-running the above command is a very quick way to verify things work!

  • thanks! worked nice.
    i just needed to install postfix too.

    iuri