A script that automatically labels emails in your Gmail inbox based on their sender.
- Ensure you have python and git installed on your local machine.
- Clone the repository to your local machine.
- Create a virtual environment (.venv) within the repository and activate it
- Within the virtual environment install the required dependencies by running
pip install -r requirements.txt
. - Create a file named
Config.json
in the root directory of the project. - Create the folders
creds
andlogs
in the root directory of the project. - Open the
Config.json
file and indicate the sender-label pairs you want to use as well as the number of days you want the script to look back. Seeexample-config.json
for an example. - Go to the Google Cloud Console
- Create a new project titled
Gmail-Auto-Labeler
- Enable the Gmail API within the project
- Create Oauth 2.0 credentials and download the credentials json file
- Ensure the email address you want to use is added as a Test user on the
OAuth Consent Screen
tab of theAPIs & Services
section of the project - Move the file with the credentials into the
creds
folder and rename the file tocredentials.json
- Run the
Gmail_Auto_Labeler.py
script
- Open the Task Scheduler
- Click
Create Task...
- Give the task a name and description
- On the Triggers tab, click
New...
- Select the type of trigger you want to start the script. This can be on a daily schedule, on workstation unlock, etc.
- Click
OK
- On the Actions tab, click
New...
- Select
Start a program
- In the
Program/script
field, enter the path to the python executable in your virtual environment (.venv). This should be the full path to the python.exe file in the Scripts folder. - In the
Add arguments (optional)
field, enterGmail_Auto_Labeler.py
- In the
Start in (optional)
field, enter the path to the root directory of the project. Ex. C:\Users{username}\Desktop\Gmail-Auto-Labeler\ - Click
OK
- Click
OK
- Test that the script works based on the trigger set in steps 4-6. If it doesn't, repeat this process until you find a trigger that you are happy with. If it does, then you are all set and should see your new emails with the proper labels attached after the script executes.
- A token.json file will be generated in the
creds
folder. This file is used to store the access token for the user and is reset on a regular cadence. If you run the script and find that you need to authorize this project through Gmail, simply authorize the project on Gmail and the token.json file will be updated. - The function
authenticate_gmail
is coded with Windows file paths ('\'). If running on Mac/Linux you must update the file paths to use '/' instead. - The logs folder contains the logs generated from each run of the script in the event that something wrong happens you can go here to triage any issues.
- SENDER_LABELS is the json variable that holds the sender emails you want to label and the label you want to apply to all emails received from those senders. Ex. If I want to label everything from [email protected] with the label 'Venmo' then I would set it up in the config like "[email protected]":"Venmo".
- DAYS_TO_LOOK_BACK is the number of days you want the script to look back in your inbox for emails to label. Ex. If I want to look back at the most recent 30 days, then I would set the value like "Days":"30".
- Add parallel processing for all emails from a given sender
- Add parallel processing for all senders
- Add functionality to add labels to emails based on the subject line
- Increase the amount of time before requesting a new access token
Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.
See LICENSE