Configuration¶
While the gmailsorter package is currently still under development, there are three possible configuration:
The easiest is currently to use the hosted gmailsorter.com email service. This service is based on the Oracle cloud infrastructure.
The second variant is running a personal gmailsorter instance as docker container. In this configuration your data remains on your own docker container and no data is shared with third party providers.
Finally, for developers gmailsorter can be installed in any Python environment. This simplifies the development process but is restricted to a single email address to be sorted.
At the current stage the docker container based version is recommended.
Gmailsorter.com¶
The gmailsorter.com web service is currently in private beta. If you are interested to try it
out please email jan.janssen@outlook.com and ask for access to the private beta.
Docker container¶
The docker container based version is currently recommended for all users.
Setup Google Cloud Application¶
The following GIF gives an overview of the required steps, they are explained in more detail below:

Login to the Google cloud console.
Click on the top menu to open the
select a projectdialog.In the
select a projectdialog, click on theNew projectbutton in the top right corner.In the
New Projectdialog you can specify a name of your project, afterwards the project can be created by clicking on thecreatebutton.Once the project is created, you are redirected to the overview page again. Here you again open the
select a projectdialog to select your newly created project. After you selected your new project you should see theselect a projectbutton in the top left displaying the name of your selected project.Use the
Quick Accessmenu to navigate to theAPI & Servicessection.In the
API & Servicestab click the+ Enable APIs and servicesbutton to add the Google Mail API.In the
API Librarysearch forGMail APIand select the entryGMail APIfrom the search results.On the
Product detailspage for theGMail APIclickenableto enable this API for your project. Afterwards you are redirected to theAPI & Servicestab again.From the left menu select the
OAuth consent screenentry.On the initial page of the
OAuth consent screendialog selectUser TypeasExternal. The optionInternalis only available forGoogle Workspaceaccounts, so it is not discussed in more details here. Afterwards clickcreateto start the four step process to create anOAuth consent screenfor your project.On the first page you have to enter the
App information. This includes the name of the application, the selection of anuser support emailfrom the dropdown menu and finally you want to enter your email in theDeveloper contact informationfield. Afterwards complete the first step by clicklingsave and continue.In the next step the
Scopesfor the project can be defined. You can add additional scopes by clicking onadd or remove scopes. In theupdate selected scopesdialog, select the following scopes:.../auth/userinfo.email,.../auth/userinfo.profile,openid,.../auth/gmail.modifyand.../auth/gmail.settings.basic. The last two are on pages three and four respectively. Once all five scopes are selected you can clickupdateat the bottom of theadd or remove scopesdialog.Back on the
Scopespage you can again clicksave and continue.On the
Test userspage you can add yourself as test user by clicking on+ add users, entering your email address and confirming withadd. Once you added your users you can again clicksave and continueto progress.On the final
Summarypage all the information are summarized. So you can scroll to the bottom and return to the main menu by clickingBack to dashboard.Finally, select the entry
Credentialsfrom the left menu and open theCredentialsdialog.In the
Credentialsdialog click+ Create credentialsto create a newOAuth client ID.In the
Create OAuth client IDdialog selectWeb applicationas application type.Afterwards you can specify the name of your application, in addition to the
Authorized Javascript originsandAuthorized redirect URIs. AsAuthorized Javascript originsaddhttp://localhost:8080and asAuthorized redirect URIsenterhttp://localhost:8080/oauth2callback. This URI changed from the version in the GIF above fromhttp://localhost:8080/auth2callbacktohttp://localhost:8080/oauth2callback. Finish the creation of the oauth client ID by clickingcreate.From the
oAuth client createddialog click theDownload JSONbutton to download the credentials file and save it to yourDownloadsfolder on your local hard drive.
At this stage the configuration of the Google project is completed and you can exit the Google cloud console.
Download and start the docker container¶
The following GIF gives an overview of the required steps, they are explained in more detail below:

At the current stage no pre-build docker images are available. So the docker image is build from the git repository directly:
Clone the git repository from Github using
git clone https://github.com/jan-janssen/gmailsorter.Navigate into the folder of the repository
cd gmailsorter.Create a
tmpdirectory in the repositorymkdir tmp. This folder is mounted to the docker container to provide easy access to the core files. These contain the credentials JSON filecredentials.json, the SQLite databaseemail.dbas well as the log filesfetch.err,fetch.out,update.errandupdate.out. All these files can be accessed directly from the host operation system rather than requiring access via the Docker container.Move the credentials file from configuring the google cloud application to the
tmpfoldermv ~/Downloads/*.json credentials.json.Navigate back to the root of the repository
cd ...Start the build process of the docker image using
docker build -t gmailsorter .. Depending on your internet connection this step can take a couple of minutes.Finally, once the docker container is build it can be started with
docker run -d -p 8080:8080 -v /Users/jan/Desktop/gmailsorter/tmp:/tmp -e MAILSORT_ENV_SECRET_KEY='e0f9eb0f0a16667771fa697ccbfaec952c410c6eaab54868' gmailsorter. In this command-dstarts the docker container in daemon mode.-p 8080:8080redirects the port8080from the docker container to the same port of the host system.-v /Users/jan/Desktop/gmailsorter/tmp:/tmpmounts thetmpdirectory of the repository/Users/jan/Desktop/gmailsorter/tmpto the/tmpdirectory of the docker container. The parameter-e MAILSORT_ENV_SECRET_KEY='e0f9eb0f0a16667771fa697ccbfaec952c410c6eaab54868'specifies the secret used to encrypt the login authentication, so please do not use this key but rather create your own one.Once the docker container is started successfully, you can navigate to localhost:8080 to access the web interface.
By default the docker container is designed to update all registered email accounts every five minutes. Depending on the number of users and emails per user this can require quite some resources, in particular limited memory can result in unexpected crashes.
Link Google account to gmailsorter¶
The following GIF gives an overview of the required steps, they are explained in more detail below:

The web interface for gmailsorter is designed to be minimalistic.
Navigate to the gmailsorter web interface localhost:8080.
Click the
sign in with Googlebutton.This redirects you to the
Sign in with Googledialog where you can select the Google account you want to use with gmailsorter.On the following
Google hasn't verified this appscreen you are informed that gmailsorter is not yet verified. Still you can click theContinuebutton in the bottom left corner to continue.Afterwards you are informed about the required access, mark the
Select alloption to use gmailsorter with your Google account. At the bottom of the page confirm your choice by clickingcontinue.You are then redirected to the gmailsorter webinterface.
In the background gmailsorter starts to initialize your email account, depending on your internet connection and number of emails in your account, this can take up to an hour. You can leave the gmailsorter web interface at this point, just make sure the docker container continues running.
Python interface¶
To allow developers to implement their own algorithms for email sorting, gmailsorter provides an additional python interface. This is recommended for advanced users only.
Install gmailsorter package¶
The gmailsorter is available on the conda-forge or pypi repositories and can be installed using either:
conda install -c conda-forge gmailsorter
or alternatively:
pip install gmailsorter
Setup Google Cloud Application¶
Read the section above for the docker container, the configuration of the Google cloud console project is identical.
Start the web application¶
The gmailsorter python application requires three environment variables:
export MAILSORT_ENV_CREDENTIALS_FILE='/Users/jan/Desktop/gmailsorter/tmp/credentials.json'
export MAILSORT_ENV_DATABASE_URL='sqlite:////Users/jan/Desktop/gmailsorter/tmp/email.db'
export MAILSORT_ENV_SECRET_KEY='e0f9eb0f0a16667771fa697ccbfaec952c410c6eaab54868'
The credentials file MAILSORT_ENV_CREDENTIALS_FILE points to the credentials file you downloaded at the end of the
previous set. The database url MAILSORT_ENV_DATABASE_URL defines the connection to the database as connection string.
By default gmailsorter uses a simple SQLite database, but most SQL databases are supported. Finally, the secret key
MAILSORT_ENV_SECRET_KEY for the encryption of login information is exported as environment variable as well. Again,
please do not use the secret provided here, but rather generate your own.
Once the environment variables are specified gmailsorted can be started using python -m gmailsorter.webapp . The
application can then be accessed via the web interface at localhost:8080.
Link Google account to gmailsorter¶
Read the section above for the docker container, the linking of the Google account to gmailsorter is identical.
Sort emails with gmailsorter¶
In contrast to the webservice and the docker container the python packages does not include periodically scheduled updates of the database or tasks to sort your emails. Instead these can be triggered on the command line using:
gmailsorter-daemon -s -c ${MAILSORT_ENV_CREDENTIALS_FILE} -d ${MAILSORT_ENV_DATABASE_URL}
and:
gmailsorter-daemon -u -c ${MAILSORT_ENV_CREDENTIALS_FILE} -d ${MAILSORT_ENV_DATABASE_URL}
Here the command line options refer to the following settings:
-c/--credentialspath to credentials file provided by Google e.g.credentials.json.-d/--databaseconnection string to connect to database e.g.sqlite:///email.db.-u/--updateupdate the local email database and retrain the machine learning model.-s/--scheduledsort emails to email folders using the previously trained machine learning model.
With these commands the gmailsorter service can be configured on bare metal without the need for any containerization solution.