Introduction
In this guide, we're going to streamline and optimise how you manage your inbox. We'll be using n8n, a brilliant workflow automation tool, combined with AI to automatically categorise your Outlook emails, keeping you on top of your inbox.
You can follow this guide as a stand alone article, or use is as an accompanying guide to the below YouTube video.
What You'll Learn
- Setting up a free static domain with ngrok
- Configuring Microsoft Azure for Outlook authentication
- Setting up n8n locally using Docker on Windows Subsystem for Linux (WSL)
- Integrating Ollama's AI models for email categorisation
- Creating and optimising the n8n workflow for email processing
- Handling errors and exceptions in the workflow
- Testing and refining the automation
Why This Setup?
Now, you might be wondering why we're going through all this trouble. Well, this automation setup is absolutely brilliant, and here's why:
✨ It saves you loads of time by automatically sorting and tagging emails
📈 Improves email management for those of us drowning in correspondence
🔧 It's customisable to fit your specific categorisation needs
🤖 Uses AI for more accurate and context-aware categorisation
🔒 It's a self-hosted solution, giving you better privacy and control
Prerequisites
Before we dive in, ensure you have the following components installed and set up:
Component | Requirement | Purpose |
---|---|---|
Operating System | Windows 10/11 | Base system |
Subsystem | WSL2 | Linux environment |
Container Platform | Docker Desktop | Service containerisation |
IDE | Visual Studio Code | Code editing |
Version Control | Git | Code management |
Cloud Account | Microsoft Azure | Authentication |
Email Service | Microsoft Outlook | Email processing |
Note: This guide assumes you're somewhat familiar with basic programming concepts. Even if you're new, don't worry! We'll walk you through each step. Make sure you have the following installed on your Windows machine:
-
Windows Subsystem for Linux (WSL2): Allows you to run Linux distributions on Windows. Follow Microsoft's WSL installation guide.
-
Docker Desktop: Needed to run Docker containers. Download it from Docker's website.
-
Visual Studio Code (VS Code): A powerful code editor. Download it from here.
-
Git: For cloning repositories. Download it from Git's website.
If you don't have WSL, Docker Desktop and VS Code set up, follow this guide first
Step-by-Step Guide
1. Setting Up the Environment
🎥 Watch this section 00:33 - 04:22
1.1 Set Up ngrok for a Static Domain
We'll use ngrok to expose our local environment securely to the internet. This is necessary for webhooks and authentication callbacks.
-
Sign Up for ngrok
- Go to ngrok's website and sign up for a free account.
-
Create a Static Domain
- After logging in, navigate to the Dashboard.
- Click on Reserved in the left-hand menu and select Reserved Domains.
- Click Create Domain.
-
Note Down the Static Domain URL
- This will be something like
https://yourname.ngrok.io
. - Keep this handy; we'll need it later.
- This will be something like
1.2 Prepare the n8n Environment
Now, let's set up n8n in your development environment.
-
Open Visual Studio Code
- If you haven't installed it yet, download and install it from here.
-
Connect to WSL
- Install the Remote - WSL extension in VS Code.
- Click on the green >< icon in the bottom-left corner of VS Code.
- Select New WSL Window.
- This will open a new VS Code window connected to your WSL instance.
-
Clone the n8n Setup Repository
-
Open the terminal in VS Code by pressing
Ctrl + Shift + \
(backtick) or navigating to Terminal > New Terminal. -
In the terminal, run the following command:
git clone https://github.com/WayneSimpson/n8n-ngrok.git
-
This will clone the repository into your WSL filesystem.
-
1.3 Configure the Docker Compose File
-
Navigate to the Cloned Repository Folder
- In the VS Code Explorer pane, open the
n8n-ngrok
folder.
- In the VS Code Explorer pane, open the
-
Open
docker-compose.yml
- This file defines the Docker services we'll be running.
-
Review the Defined Services
- PostgreSQL Database: For n8n's database.
- n8n: The workflow automation tool.
- Vector Database: Optional, for advanced AI features.
- ngrok: To expose your local services to the internet.
1.4 Set Up the Environment Variables
We’ll configure the .env
file, which will store necessary environment variables securely. Follow these steps:
-
Create Your
.env
File-
Open the terminal in Visual Studio Code.
-
Run the following command to copy the example file:
cp .env.example .env
-
This creates a new
.env
file from the example template.
-
-
Update the
.env
File- Open the
.env
file in VS Code for editing. - We’ll need to set several keys, as described below:
- Open the
-
Generate a Random Encryption Key
- Visit RandomKeygen and generate a 256-bit key.
- Copy this key and set it as
n8n_ENCRYPTION_KEY
in your.env
file.
-
Generate a Random JWT Secret
- At RandomKeygen, generate another key to use as your JWT secret.
- Set this value as
n8n_JWT_SECRET
in your.env
file.
-
Add Your OpenAI API Key (Optional)
- If you plan to use OpenAI’s API, set
OPENAI_API_KEY
to your OpenAI key in the.env
file. - If not, you can skip this step.
- If you plan to use OpenAI’s API, set
-
Set Your ngrok Authentication Token
- In the ngrok dashboard, under Setup & Installation, find your authentication token.
- Copy and set this as
NGROK_AUTHTOKEN
in the.env
file.
-
Set Your ngrok Static Domain
- Both
WEBHOOK_URL
andVUE_APP_URL_BASE_API
should be set to your ngrok static domain, e.g.,https://yourname.ngrok.io
.
- Both
Example .env
File:
POSTGRES_USER=root
POSTGRES_PASSWORD=your-super-secret-password
POSTGRES_DB=n8n
N8N_ENCRYPTION_KEY=your-n8n-encryption-key
N8N_USER_MANAGEMENT_JWT_SECRET=your-super-secret-jwt-token
N8N_AI_OPENAI_API_KEY=your-open-ai-key #if you want to use n8n ai for code nodes etc
NGROK_AUTHTOKEN=your-ngrok-auth-token
WEBHOOK_URL=https://your-nrok-url
N8N_HOST=your-nrok-url
1.5 Start the n8n Environment
-
Navigate to the Directory Containing
docker-compose.yml
- Ensure you're in the
n8n-ngrok
directory.
- Ensure you're in the
-
Start All Services
-
In the terminal, run:
docker-compose up -d
-
This will start all the services defined in
docker-compose.yml
.
-
-
Check the Logs
-
To ensure everything is running smoothly, check the logs:
docker-compose logs -f
-
Press
Ctrl + C
to exit the logs view.
-
-
Access the n8n Editor
- Open your web browser and navigate to your ngrok static domain URL, e.g.,
https://yourname.ngrok.io
. - You should see the n8n editor interface.
- Open your web browser and navigate to your ngrok static domain URL, e.g.,
2. Importing the Workflow Template
Now that n8n is up and running, we'll import a pre-built workflow template that automates email categorisation using AI. This template will be the foundation for the next steps.
2.1 Download the Template
- Download the template directly from this link.
2.2 Import the Template into n8n
- In the n8n editor, click on the menu icon (three horizontal lines) in the top corner.
- Select Import.
- Choose From File and select the downloaded template file.
- The workflow will be imported into your n8n instance.
2.3 Review the Workflow
- Take a moment to explore the imported workflow.
- You'll see various nodes configured to fetch emails, process them with AI, and update their categories.
- Don't worry if it looks complex; we'll guide you through configuring it to suit your setup.
3. Setting Up Ollama for AI Integration
🎥 Watch this section 04:22 - 06:50
To enable AI-powered email categorisation, we'll set up Ollama, which allows us to run local AI models.
3.1 Install Ollama for Windows
Ollama can be installed in multiple ways, but we'll focus on installing it directly on Windows.
Download and Install Ollama
- Visit the Ollama website and download the Windows installer.
- Run the installer and follow the on-screen instructions.
3.2 Download an AI Model
Depending on your system's resources, you can choose different AI models.
Select an AI Model
- For powerful systems:
- Gwen 2.5 (14B model) is recommended for better results.
- For less powerful systems:
- Llama 3.2 7B (7B model) or Llama 3.2 13B (13B model) can be used.
Pull the Model
Open Command Prompt or PowerShell and run:
ollama pull gwen2.5-14b
Replace gwen2.5-14b
with the model you choose.
3.3 Verify Ollama is Running
Ollama runs as a background service after installation.
- Ensure it's running by checking your system tray or services.
- To list available models, run:
ollama list
You should see the model you pulled listed.
4. Configuring the Workflow for Ollama
Now we'll connect the imported workflow to Ollama.
4.1 Update the AI Agent Node
- In the n8n editor, open the imported workflow.
- Locate the AI Agent node.
- Double-click the node to open its settings.
4.2 Connect to Ollama
- Under Model Provider, select Ollama.
- Host: Enter
localhost
.- If you're running n8n in a virtual machine or WSL, you might need to use your machine's IP address.
- To find your IP address, open Command Prompt and run
ipconfig
, then note your IPv4 Address.
- To find your IP address, open Command Prompt and run
- If you're running n8n in a virtual machine or WSL, you might need to use your machine's IP address.
- Port: Use the default port
11434
. - Click Refresh Model List.
- Select the model you pulled earlier from the Model dropdown.
4.3 Save and Test the Node
- Click Save to apply the changes.
- Optionally, you can execute the node to test the connection.
- The first response may take longer as the model loads into memory.
5. Setting Up Microsoft Azure for Outlook Integration
🎥 Watch this section 09:18 - 12:37
To interact with Outlook and automate email categorisation, we'll set up an application in Microsoft Azure.
5.1 Register a New Application in Azure
Access Microsoft Azure Portal
- Go to the Azure Portal and log in with your Microsoft account.
Navigate to App Registrations
- In the search bar at the top, type App registrations and select it.
Register a New Application
- Click + New registration.
- Fill in the details:
- Name:
n8n Outlook
. - Supported account types: Choose Accounts in any organizational directory and personal Microsoft accounts.
- Redirect URI: Set it to your n8n OAuth callback URL, e.g.,
https://yourname.ngrok.io/rest/oauth2-credential/callback
.
- Name:
5.2 Configure API Permissions
In your newly registered app, set the necessary permissions.
Add Permissions
- Click on API permissions in the left-hand menu.
- Click + Add a permission.
- Choose Microsoft Graph > Delegated permissions.
- Select the following permissions:
Permission | Type | Description |
---|---|---|
Mail.Read |
Delegated | Read user mail |
Mail.ReadWrite |
Delegated | Read and write mail |
Mail.Send |
Delegated | Send mail |
User.Read |
Delegated | Read user profile |
- Click Add permissions.
Grant Admin Consent
- Click Grant admin consent for your tenant.
- Confirm to grant permissions.
5.3 Create a Client Secret
- In the left-hand menu, click on Certificates & secrets.
- Click + New client secret.
- Add a description (e.g.,
n8n Secret
) and set an expiration. - Click Add.
- Copy the Value of the client secret. This is only shown once, so save it securely.
6. Connecting n8n to Microsoft Outlook
Now we'll set up the credentials in n8n to connect to Outlook.
6.1 Add Microsoft Outlook Credentials
- In the n8n editor, select an Outlok node and click on Credentials.
- Click + New and select Microsoft Outlook.
- Fill in the configuration details:
- Credential Name:
Outlook OAuth
. - Client ID: Paste your Azure app's Application (client) ID.
- Client Secret: Paste the client secret you generated.
- OAuth Callback URL: It should match the Redirect URI you set in Azure.
- Typically, this is
https://yourname.ngrok.io/rest/oauth2-credential/callback
.
- Typically, this is
- Credential Name:
6.2 Connect and Authorize
- Click Connect.
- You'll be redirected to log in to your Microsoft account and grant permissions.
- After successful authentication, your credentials should be saved.
7. Configuring the Workflow for Outlook
7.1 Update Outlook Nodes
- In your workflow, locate all the Microsoft Outlook nodes.
- For each node:
- Double-click to open the node settings.
- Under Credentials, select the Outlook OAuth credentials you just created.
- Verify other settings like Operation, Folder, and Fields as per your requirements.
7.2 Adjust Node Parameters
Get Emails Node
- Operation: Get Many
- Limit: Set to a number you're comfortable with (e.g., 10).
- Fields: Select fields like
flag
,from
,importance
,replyTo
,sender
,subject
,toRecipients
,body
,categories
,isRead
.
Apply Filters
-
To fetch only relevant emails, use the following custom filter in the Filters section:
flag/flagStatus eq 'notFlagged' and not categories/any()
-
Folder: Set to your Inbox or another folder as needed.
8. Customising Categories
🎥 Watch this section 15:51 - 16:26
The workflow includes predefined categories for email classification. You can customize these to suit your needs.
8.1 Update Categories in the Workflow
Locate the Category Node
- Find the Set node named varID & Category or similar.
- This node sets the categories used in the AI Agent and subsequent nodes.
Edit the Categories
-
Double-click the node to open its settings.
-
Find the field where categories are defined, typically a string or an array.
-
Update the categories to reflect those you want to use.
"action", "junk", "receipt", "SaaS", "community", "business", "other"
-
You can add or remove categories as needed.
8.2 Reflect Changes in the AI Agent Node
- Open the AI Agent node settings.
- Ensure the System Message and Prompt reflect the categories you've defined.
- Update any references to categories to match your customized list.
8.3 Update the Switch Node
-
Locate the Switch node that routes emails based on the category.
-
Add or modify rules to handle your new categories.
-
For each category, set a condition:
{{$json["output"]["category"]}} equals "YourCategory"
-
-
Ensure that all categories are accounted for, and add a fallback route if necessary.
8.4 Adjust Outlook Update Nodes
- For each category, there may be corresponding Microsoft Outlook nodes to update or move emails.
- Ensure these nodes are correctly configured to handle your customized categories.
- Update folder selections or actions as needed.
9. Finalising and Testing the Workflow
Now that all configurations are in place, it's time to test the workflow.
9.1 Run the Workflow Manually
- Set the Trigger node to Manual mode for testing.
- Click Execute Workflow at the top-right corner.
9.2 Monitor the Workflow Execution
- As the workflow runs, click on each node to view its input and output data.
- Pay special attention to the AI Agent node to see how emails are categorised.
- Check for any errors indicated by red nodes.
9.3 Verify in Outlook
- Log in to your Outlook account.
- Check if emails have been categorised and moved according to the workflow logic.
9.4 Troubleshooting
- If you encounter issues:
- Ensure all nodes have the correct credentials selected.
- Double-check the API permissions in Azure.
- Verify that Ollama is running and accessible.
- Review error messages for guidance on what needs adjustment.
10. Automating the Workflow
Once you're satisfied with the workflow, you can set it to run automatically.
10.1 Change the Trigger Node
🎥 Watch this section 25:32 - 26:06
- Replace the Manual Trigger node with an Outlook Trigger node.
- Set it to On Message Received.
- Configure any filters or options as needed to suit your requirements.
10.2 Save and Activate the Workflow
- Save the workflow.
- Toggle the Active switch at the top-right corner to activate the workflow.
11. Tips and Best Practices
11.1 Adjusting AI Prompts (Optional)
- If you wish to customize how the AI categorises emails, you can adjust the System Message and Prompt in the AI Agent node.
- Keep prompts clear and concise for better performance, especially with smaller models.
- Ensure that the output format is maintained to prevent errors.
11.2 Error Handling
🎥 Watch this section 20:04 - 21:08
- Implement error handling to ensure the workflow runs smoothly even if issues occur.
- Use a Set node or Function node to sanitize and validate the AI's output.
- For example, use regular expressions to clean the JSON output if the AI includes unwanted characters.
- Enable Continue On Fail in critical nodes to prevent the entire workflow from stopping due to a single error.
11.3 Managing System Resources
- Model Selection: Choose an AI model appropriate for your system's capabilities.
- Larger models offer better accuracy but require more resources.
- Batch Processing: Adjust batch sizes in the Get Emails node to prevent overloading your system.
- Start with smaller batches and increase gradually as you monitor performance.
11.4 Regularly Update and Monitor
- Keep your n8n instance and Ollama models up to date for the latest features and security patches.
- Monitor the workflow's performance and accuracy over time.
- Adjust AI prompts and categorization logic as needed based on email content changes.
Conclusion
Congratulations! You've successfully set up an advanced email categorisation system using n8n and AI. Here's what you've accomplished:
- Set up a local n8n instance with ngrok for secure, public accessibility.
- Imported and configured a workflow template for AI-powered email categorisation.
- Installed Ollama and integrated an AI model for processing emails.
- Configured Microsoft Azure and connected n8n to Outlook.
- Customized the workflow to suit your categorization needs.
- Tested and automated the workflow for continuous operation.
Additional Resources
n8n template
Try n8n cloud
Connect with me on LinkedIn
Book an appointment
nocodecreative.io
Discussion