Fetch Emails from Gmail via POP3 or IMAP on Ubuntu 22.04

Getmail is a powerful email retrieval program for Ubuntu Linux that can be used to download and store emails from remote mail servers. It supports POP3, IMAP, and SDPS protocols, so you can configure it to work with your current email provider to download messages and store them on your local computer or server. Additionally, it has the ability to filter out certain emails based on predefined criteria, making it an ideal solution for those who want full control over their inbox.

This guide will show you how to fetch emails from Gmail using getmail on Ubuntu 22.04.

Install Getmail

The first step is to install getmail, unless you have installed it on your system already. We will do the installation on the shell, open a Terminal to proceed. Run the following command to install Getmail:

sudo apt update sudo apt-get install getmail4

Configure Gmail account

Gmail has some security settings that we must tweak to allow Getmail to access your Gmail account. To enable "Less Secure Apps" in your Gmail account, go to the following link and enable the "Allow less secure apps" setting:

https://myaccount.google.com/lesssecureapps

Configure Getmail

To create a getmail configuration file we first have to create a new directory to store the configuration:

mkdir -p ~/.getmail

Now we create a new configuration file named getmailrc inside the newly created directory:

touch ~/.getmail/getmailrc

Next, open the file with a text editor, I will use nano here:

nano ~/.getmail/getmailrc

Add the following configuration to the getmailrc file, replacing <your_email> and <your_password> with your actual Gmail email address and password:

[retriever]
type = SimplePOP3SSLRetriever
server = pop.gmail.com
username = <your_email>
password = <your_password>

[destination]
type = Maildir
path = ~/Maildir/

[options]
delete = false
message_log = ~/.getmail/log

The file should look similar to this screenshot:

Getmail configuration

Save the file and exit the editor.

Change the permissions of the getmailrc file so that it can be accessed only by your current user:

chmod 600 ~/.getmail/getmailrc

Here we are using the POP3 protocol with SSL to retrieve emails. But you can use IMAP as well if you prefer that. The emails will be stored in the ~/Maildir/ directory. The delete = false option ensures that the emails are not deleted from the Gmail server after being fetched. The message_log option specifies a log file to store the getmail logs.

Before running Getmail, we have to create the Maildir directory to store your fetched emails:

mkdir -p ~/Maildir

Run Getmail

Now it's time to test the configuration. Run the getmail command without parameters, it will use the getmail configuration that we created earlier in this tutorial automatically.

getmail

The fetched emails will be stored in the ~/Maildir/ directory.

Remember that storing your email password in plain text in the getmail configuration file can be a security risk.  Also, be aware that enabling "Less Secure Apps" in your Gmail account might expose your account to potential security risks.

Leave a Comment