/Python-Gmail-IMAP

Python code and step by step instructions on how to get Gmail IMAP API to work.

Primary LanguagePythonMIT LicenseMIT

Python Gmail IMAP

Code based on own experience and some codes I couldn't find the sources, sorry. Python Gmail IMAP was written to help community with code that isn't clearly documented. You can find a step by step on how to get the credentials you'll need to run the code.

It's not meant to be an IMAP client, instead, just a piece of code to built in your projects.

Usage

Firstly, clone the repository and access it's directory:

git clone https://github.com/rcbonz/Python-Gmail-IMAP.git
cd Python-Gmail-IMAP
python3 pythonGmailImap.py

Getting it to work

Follow the steps to get the required credentials on Google Console here and with your Client ID and Client Secret, run the following command on terminal:

python2 oauth2.py --generate_oauth2_token --client_id=<your client id> --client_secret=<your client secret>

The original oauth2.py file can be found here.

This command will generate a link to allow the IMAP to be used on you e-mail account, as follows:

Access the link in a browser and follow the steps till you get the Authorization Code and copy it. Go back to the terminal and paste it:

Now you have the refresh token that you'll need to run the IMAP client.

Paste all credentials in the pythonGmailImap.py and run it:

python3 pythonGmailImap.py

Creating Google Credentials

Access the Google Console page and follow the steps:

  1. Create a new project and search for the Gmail API on the API Library tab, then open it and Enable it.

  1. In the Gmail API details tab, click on Credentials and than in Create Credentials. Choose the OAuth client ID

  1. Select Desktop app as the application type and finish creating it.

  1. There are your Client ID and Client Secret.