A set of python scripts that integrate with GMail API and perform some rule based operation on emails
The program authenticates a user by performing the installed application oauth flow. A list of emails is then fetched from the authenticated user's GMail mailbox and stored in the local relational database (Postgres). The emails are then fetched from the database and processed against a set of rules specified as JSON file and subsequent actions are performed on them accordingly. Default behaviour regarding the number of emails fetched, location of the rules file and fetch labels is specified in the config.py file which can be overriden by providing commmand line arguments.
Python: Python 3.6
Database: Postgres
Libraries Used
- SQLAlchemy: for database related operations
- google-python-api-client: for integrating with GMail API
- google-auth-oauthlib: for authentication
- google-auth-httplib2: for authentication
- python-dateutil: for date related operations
-
Clone the repository
-
Create a Google Cloud Platform project with the GMail API enabled
-
Create authorization credentials for a desktop application i.e. oauth client IDs. Save the generated secret file as client_credentials.json inside the root of the repository directory.
-
Install dependencies
pip install -r requirements.txt
-
Database setup
- Create a postgres database named maildb
- Export postgres user and password
export POSTGRES_USER = <your postgres username under which the db has been created> export POSTGRES_PASSWORD = <your postgres password>
- Ensure the database server is running on port 5432
-
Running
python main.py --help
to get a list of optional argumentspython main.py
to run the program
Note The first time the program is run, complete the oauth flow according to the prompts. For subsequent runs, the token is fetched from the file token.json generated during the first run.