Automated Email Classifier Project in n8n
n8n is an open-source automation tool that integrates with a wide variety of services and APIs to help automate workflows. Using n8n, we can build an email classifier that automates the sorting of incoming emails based on the content such as scheduling of meeting or leave request. Lets build a n8n based project which labels the mails according to their category such as a meeting information or leave request and many more.

API Authentication
Firstly we need to get API Authentication for our Gmail API and Google Calendar API. Follow the steps to get them.
Step 1: Go to the official website of google cloud console and login.
Step 2: After successful login, create a New Project.

Step 3: Find and enter the APIs & Services tab.

Step 4: From the menu, select Enabled APIs & Services. Search for Google Calendar API and Gmail API.
Step 5: Enable the APIs.
Steps to extract Google Gemini API Key
Step 1: Go to the official website of Google Gemini API.
Step 2: Click on Create New API Key.

Step 3: Select Your Project or Create a new project.

Step 4: Copy the created API Key and save it for use.

Workflow Creation on n8n
Follow the steps to build the email classifier agent.
For setting up your n8n refer to: What is n8n?
Creating Gmail Trigger
Step 1: Create a new workflow.

Step 2: Click on add first step button.

Step 3: Search for Gmail.

Step 4: Select the On message received trigger.

Step 5: The Gmail trigger will open.

Step 6: Select Create new credential under the Credentials to connect with.

Step 7: Enter the Client ID and Client Secret that was extracted from google cloud console.

Step 8: If entered details are correct and the login was successful, a pop-up will notify that account connected and you can also verify by executing the Gmail trigger.
Creating Text Classifier Node
Step 1: Add another node, on the right top corner of the canvas and search for text classifier.

Step 2: Inside the text classifier node, select the expression option from the fixed and expression.

Step 3: Select the snippet from the schema of previous node and add it to the Text to classify box.

Step 4: Define the required categories such as Meeting Request and Leave Request.

Step 5: Give the description of the categories.

Step 6: Click on Add Option and select System Prompt Template from the drop box.
Step 7: Execute step and check for any issue.

Creating Chat Model Node
Step 1: From the top right corner of canvas, click on add node option and search for Chat model.

Step 2: Choose the model that we want to use in our workflow, here we are going to use Google Gemini Chat Model.
Step 3: Click on the Create New Credential.

Step 4: Enter the API Key extracted from the Google Gemini API key website.
Step 5: Select the model which we want to use in the workflow. Here we are using google gemini 1.5 flash.

Step 6: If the details are correct then it will run successfully.

Creating Meeting Request and Leave Request Nodes
Step 1: From the add new node option, search for Gmail and click on it.
Step 2: From the list of actions, select Add label to message.

Step 3: Attach the same Gmail account that was used in the Gmail trigger Credentials.
Step 4: Select Message in the Resource option. And select Add Label operation.

Step 5: In the message ID Dialog box, paste ID from the Input schema.

Step 6: Add the required label from the drop box.

Step 7: Execute the step to check for any issues, if the workflow works fine the output will be shown.
Creating the Google Calendar Node
Step 1: From the add new node option, search for Google Calendar.
Step 2: Search for Create an event under the Event Actions.

Step 3: Attach the same Gmail account that was used in the Gmail trigger Credentials.
Step 4: Select the Create operation, select the Calendar and define the Start and End.

Workflow Execution

We can also check our Gmail to see for changes.
Before execution.

After execution.

Workflow Activation
After the workflow is created, we can activate it and now it will automatically perform its task on a fixed interval of time.
