The Gmail AI attachment document classifier

Published on
Authors
backlit computer
Table of Contents

Earlier this month (November 2023) a friend of mine mentioned they struggled organising their work, by this I mean they have 3 different note taking apps, they do not have a central place where they keep they're documents and so on. The latter really struck a nerve, a friend of mine as well had specifically mentioned they'd also want to have a system that would automatically save and classify all the documents they send via email for better management.

Which is why I build this mini-application to read my Gmail, if there's an attachment on a specific email automatically upload the attachment to my Google Drive to a specific subdirectory. Which directory the attachment is uploaded is determined by what the email subject and the name of the attachment.

An example of this would be an email with subjectz Booking Receipt Email #12NIG5 and attachment name Booking Receipt Email #12NIG5 will be uploaded to the finance subdirectory and not health subdirectory.

Project and OpenAI Setup

The code for this is available on my personal github on doc-classifier. Clone the code to your machine. (I am running my code on an EC2 instance)

git clone https://github.com/ZwangaMukwevho/doc-classification
  1. Go installation is required for this project. Setup can be found on. Golang Installation. (Maybe an extension of this is getting it to run on a Docker Container)

  2. You will also be required to setup an OpenAI acount. In order to set it up, create an account with openai. and navigate to the api-keys to create the api key. Create a .env file and paste your api key in .env file.

touch .env
  1. inside the .env the key should look like (Where xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx is your api-key):
  OPENAI_API_KEY=xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 

Google Setup

  • Since this project using Google API's and Service which uses OAuth 2.0 for authentication. The steps below will outline setting this up.
  1. Set up an account with the Google Cloud Platform from here
  2. Once in the home page, open the lunchbar menu on your top left, Navigate to APIs & Services then Enabled APIs & Services.
backlit computer
  1. This will prompt you to choose a project. If you don't have one create one or use an existing one.
  2. Go to the Credentials tab (Can be seen on image on step 2), then click the + CREATE CREDENTIALS tab then choose OAuth Client ID.
  3. When prompted for application type choose Desktop App and Give it a name.
  4. Click Create then download OAuth2.0 credentials file to the root folder of your project.
  5. Create another .env file inside the setup folder and add content below. Where GOOGLE_AUTH_FILE is the name of the OAuth2.0 Credentials file from above. (i.e replace google_api_services.png with the name of your file)
GOOGLE_AUTH_FILE=name_of_oath2.0_file
GMAIL_TOKEN_FILE=token_gmail.json
G_DRIVE_TOKEN_FILE=token_g_drive.json
  1. The next step is to setup the Gdrive directories in which the documents will be uploaded in when they are classified. Login to your Googledrive and make new directories. I personally have 8 directories which are:
1. Education        5. Home  
2. Entertainment    6. Personal
3. Finance          7. Work
4. Health           8. Default
  1. Once that is done, Navigate to the Setup directory and run the setup.go.
cd setup
go run setup.go
  1. You will be promted to setup you Gmail and Gdrive credentials on CLI. Click on the link that is outputed on the CLI.
  2. Choose the specific google profile you want to use for your GMAIL and follow the instructions.
  3. You will get to a localhost connection error as below after finishing with the setup:
backlit computer
  1. Copy the URL from your browser and it will look like the snippet below. As you can see the scope path parameter is 4444444xxxx (Look for the code=4444444xxxx, but yours will be different). Copy that, paste it on your command line and press enter.
http://localhost/?state=state-token&code=4444444xxxxx&scope=https://www.googleapis.com/auth/gmail.readonly
  1. You will repeat the step 11-14 again for setting up google drive. (There will be a prompt on the command line for setting up google drive).
  2. Once this is done it will create the pkg/common/directories.json, token_gmail.json and token_g_drive.json files.

Running the code

  1. Once this is done, Navigate to the root folder and run the main.go
go mod tidy
go run main.go
  1. The code is setup to run a cron every midnight