Declutter your inbox to achieve Zero Inbox with n8n
JoLo
Posted on April 11, 2023
n8n #automations #gmail
Are you feeling overwhelmed by a cluttered email inbox? Perhaps you're approaching the limits of your Google storage subscription, or simply wish your inbox could be decluttered without spending hours sorting through emails. In any case, it's important to practice good "digital hygiene" and take control of your emails. With n8n, you can do just that.
n8n (pronounced: nodemation) is a low-code automation platform that empowers users to build powerful workflows and automate their daily tasks. It is an open-source tool and a very good alternative to Zapier or Make. However, these tools require you to know a bit of coding.
You can use it to automate (boring) tasks, you can save yourself time and energy while keeping your inbox organized and under control. In this blog post, I will show how you can declutter your inbox by labeling your mails by the respective sender and achieving a Zero Inbox.
Problem
The problem is simple, you might have a Gmail account since forever, and you have old emails which you probably will never read again. But they are in your inbox.
Another problem is that you might have too many unread E-Mails, and it would be nice to identify important emails and send the unimportant ones directly to the trash. By labeling them, you can then decide for example to unsubscribe from that newsletter.
What also can happen, is that you might reach the end of your storage. At that time, you will be forced to delete older emails. That leads to a struggle by searching which one to keep and which not... sigh... wish you would have categorized or tagged, right? Or maybe you don't trust Gmail and want to keep as fewer data as possible.
It is like your daily washing routine, keep good hygiene.
In the following sections, we will set up an n8n workflow that will read messages, get their senders, use that value to create a label, and attach these to the messages.
Pre-requisite
If you follow along, you need to have n8n installed (I use Docker, but there is also a Desktop version) and a Google Developer account (use the tutorial of n8n to create your credentials).
You should also get familiar with n8n and also have some JavaScript knowledge. Luckily, n8n provides courses to get started.
Workflow
Below you can find my result workflow.
In order to use the Gmail- node, you need to create Gmail Credentials (Below is Google Calendar but it's pretty much the same).
The next subsections will explain each node.
Trigger
This is how every n8n- workflow starts. You can trigger a workflow on a scheduled basis, via webhooks, or in my case manually.
Pro-tip: n8n also offers a trigger whenever an E-Mail has been received. That would label your email immediately 😉
Get Messages
Once you are authenticated, you should be able to use all the functions from the Gmail Node. I recommend using a Limit
and execute in batches. I also put a filter but you could leave that out and receive the latest 500 mails. Here it labels the messages which are in your "INBOX". Later you should archive your mails otherwise they would be fetched again.
Get the sender/domain name of the "From"- value
This node contains a code snippet that extracts the sender domain from the From
- value (E-Mail). I save that information into a new column and to avoid a naming clash later, I also renamed the id
to mailId
and removed the id
- column.
for (const item of $input.all()) {
// This is how "From" looks like: Foo Bar <foo@bar.xyz>
// and this method returns bar.xyz
const sender = item.json.From.split('@')[1].slice(0,-1);
item.json.sender = sender;
item.json.mailId = item.json.id;
delete item.json.id;
}
return $input.all();
Creating Labels
From the previous node, the value sender
here will be set as a new Label.
Important: the node should continue running regardless if the label already exists. If you don't enable the settings, the node might fail. For setting the label's name, we use the Expression
function of n8n. That allows us to use a dynamic value depending on the sender's name.
Get all the labels
The next node is for retrieving all the labels.
But make sure that this is executed just once!
n8n is implemented in a way that it runs for each row the Get All Labels
- node. In my case, it would execute that node 500x.
Merge
The Merge- Node combines the mails with the labels accordingly.
So the upper path creates the labels according to the sender's information. Each label then gets a new LabelID
which needs to be combined with the E-mail. Below, you can find my option to achieve that.
Attach the Labels to the Mail
Now, we attach the label with the message by its Message ID
and Label Id
. Again here, we use the n8n's expression function. Remember that above, we have renamed the ID of the message to mailId
and the id
below is the ID of the label.
Review each label and then delete the corresponding labels
After each message got attached, it's now human intervention to identify which E-Mails you should safely delete and which you should archive. Below, you can see a preview of the attached labels and in the next section, I will list which kind of E-Mails you can safely delete.
Ideas of which kind of E-Mails to delete
Great, now you labeled many emails, and it's now up to you what kind of emails to delete.
I think it's good to label them first so that you as a human can now decide which emails to delete.
Are you a bit overwhelmed, what to delete?
Here are some emails you can delete for sure:
- Confirmation mails as they are outdated
- Notification mails by Google, Post courier, or any other providers which say e.g. that you have logged into a new browser as they just notify you
- RSVP of an invitation, well they are kinda Notification mails
- Messages which you also can find on their website or app (e.g. PayPal)
- Newsletter, by that time they are outdated
- Travel ternaries, if you haven't gone yet, why should you keep them? Hope you have taken great pictures
- Auto replies
Things to keep in mind
These are things you should keep in mind when building such a workflow.
You cannot run this workflow on all your E-Mails at once
Google might warn because of suspicious activities. They might also lock your account.
Recommendation: Run your workflow on 500 mails and do it twice a day. Also put a time filter here
Evaluate each label and try not to delete important mails
It is important not blindly to delete the emails but also to do a good review and not just a quick scan through them. Otherwise, you might delete important emails.
Put the mails into the archive
If you don't put the emails in the archive after reviewing them, they might be picked up again, and you will never finish decluttering your inbox.
The easiest way is to put everything to archive. However, that would still lead to some mails to archive which you might will never need again or which are simply outdated.
Full Workflow as Code
Below, you find the whole workflow as JSON which you can paste into your n8n.
{
"meta": {
"instanceId": "e842ae497bd2320305776c95ab48327e7541feb0365d1ecd05e0e0ca871ef6df"
},
"nodes": [
{
"parameters": {},
"id": "94603d9d-1f1a-4d77-8d0a-f1cfa9c0a7ab",
"name": "When clicking \"Execute Workflow\"",
"type": "n8n-nodes-base.manualTrigger",
"typeVersion": 1,
"position": [
940,
620
]
},
{
"parameters": {
"mode": "combine",
"mergeByFields": {
"values": [
{
"field1": "name",
"field2": "sender"
}
]
},
"options": {
"clashHandling": {
"values": {
"resolveClash": "preferInput1"
}
}
}
},
"id": "81cb45ca-bccb-4c21-95fb-6db89969acfc",
"name": "Merge",
"type": "n8n-nodes-base.merge",
"typeVersion": 2,
"position": [
1980,
600
]
},
{
"parameters": {
"operation": "getAll",
"limit": 500,
"filters": {
"labelIds": [
"INBOX"
],
"receivedBefore": "2022-02-28T23:00:00.000Z"
}
},
"id": "14b681ff-4ce8-4b5e-bc71-1c7cac34d13e",
"name": "Get Messages",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2,
"position": [
1160,
620
],
"notesInFlow": true,
"credentials": {
"gmailOAuth2": {
"id": "8",
"name": "Gmail account"
}
},
"notes": "Messages which have the Label \"INBOX\""
},
{
"parameters": {
"jsCode": "for (const item of $input.all()) {\n const sender = item.json.From.split('@')[1].slice(0,-1);\n item.json.sender = sender;\n item.json.mailId = item.json.id;\n delete item.json.id;\n}\n\nreturn $input.all();"
},
"id": "209f6aeb-ca56-4b27-8cf8-f5d3a029f523",
"name": "Get the Sender and Cleaning",
"type": "n8n-nodes-base.code",
"typeVersion": 1,
"position": [
1380,
620
],
"notesInFlow": true,
"notes": "The function which slices the value from the \"From\"- value to get the sender/its domain"
},
{
"parameters": {
"resource": "label",
"returnAll": true
},
"id": "daab0c5b-31bd-47c1-9054-fa40709c545b",
"name": "Get All Labels",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2,
"position": [
1760,
400
],
"executeOnce": true,
"credentials": {
"gmailOAuth2": {
"id": "8",
"name": "Gmail account"
}
}
},
{
"parameters": {
"operation": "addLabels",
"messageId": "={{ $json.mailId }}",
"labelIds": "={{ $json.id }}"
},
"id": "1d3c4cca-bb1f-46ee-9d13-d8fc9065b02e",
"name": "Attach Label to Mail",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2,
"position": [
2200,
600
],
"credentials": {
"gmailOAuth2": {
"id": "8",
"name": "Gmail account"
}
}
},
{
"parameters": {
"resource": "label",
"operation": "create",
"name": "={{ $json.sender }}",
"options": {}
},
"id": "44a02846-31a5-42e3-af1e-7378ff4f87f0",
"name": "Creating Labels",
"type": "n8n-nodes-base.gmail",
"typeVersion": 2,
"position": [
1560,
400
],
"alwaysOutputData": true,
"retryOnFail": false,
"credentials": {
"gmailOAuth2": {
"id": "8",
"name": "Gmail account"
}
},
"continueOnFail": true
}
],
"connections": {
"When clicking \"Execute Workflow\"": {
"main": [
[
{
"node": "Get Messages",
"type": "main",
"index": 0
}
]
]
},
"Merge": {
"main": [
[
{
"node": "Attach Label to Mail",
"type": "main",
"index": 0
}
]
]
},
"Get Messages": {
"main": [
[
{
"node": "Get the Sender and Cleaning",
"type": "main",
"index": 0
}
]
]
},
"Get the Sender and Cleaning": {
"main": [
[
{
"node": "Creating Labels",
"type": "main",
"index": 0
},
{
"node": "Merge",
"type": "main",
"index": 1
}
]
]
},
"Get All Labels": {
"main": [
[
{
"node": "Merge",
"type": "main",
"index": 0
}
]
]
},
"Creating Labels": {
"main": [
[
{
"node": "Get All Labels",
"type": "main",
"index": 0
}
]
]
}
}
}
Conclusion
This n8n- workflow declutters your inbox by creating labels for the senders.
The owner can then identify quicker which emails to be deleted and which one to keep/archive.
This approach usually takes a lot of time, and building a workflow like this saves you a lot of time. But keep in mind that this workflow does not delete, but just labels them. However, it took me weeks to declutter my inbox, as you cannot label it all at once. Take the section "Things to keep in mind" seriously.
The workflow is also adaptable to any other workflow automation tool, such as Zapier and Make.
It's like your body's hygiene. The cleaner and tidier your inbox is, the more relaxed and mind will be.
Start your healthy digital hygiene now!
If you want to integrate that yourself but struggle with the setup, I am happy to help.
Follow for more!
Posted on April 11, 2023
Join Our Newsletter. No Spam, Only the good stuff.
Sign up to receive the latest update from our blog.