{
  "id": "kAiLbJeChaiOyg6T",
  "meta": {
    "templateCredsSetupCompleted": true
  },
  "name": "Organize Email Attachments in Google Drive Folders",
  "tags": [],
  "nodes": [
    {
      "id": "f43882ce-f96b-4706-8428-6ff4d1689a88",
      "name": "Gmail Trigger",
      "type": "n8n-nodes-base.gmailTrigger",
      "position": [
        112,
        -656
      ],
      "parameters": {
        "filters": {},
        "pollTimes": {
          "item": [
            {
              "mode": "everyMinute"
            }
          ]
        }
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "ff5b5a09-be36-45a3-b48d-3b24270117a9",
      "name": "When clicking \u2018Execute workflow\u2019",
      "type": "n8n-nodes-base.manualTrigger",
      "position": [
        112,
        -464
      ],
      "parameters": {},
      "typeVersion": 1
    },
    {
      "id": "061aa9ae-cea6-4df3-b3cc-bc1f6b746c43",
      "name": "Loop Over Items",
      "type": "n8n-nodes-base.splitInBatches",
      "position": [
        752,
        -464
      ],
      "parameters": {
        "options": {
          "reset": false
        }
      },
      "typeVersion": 3
    },
    {
      "id": "4f749dc4-e450-4150-9d97-0dd2ef450783",
      "name": "Split Out",
      "type": "n8n-nodes-base.splitOut",
      "position": [
        1392,
        -448
      ],
      "parameters": {
        "options": {},
        "fieldToSplitOut": "$binary"
      },
      "typeVersion": 1
    },
    {
      "id": "7a0281bf-1e45-4c00-acde-aa89f79ee578",
      "name": "Merge",
      "type": "n8n-nodes-base.merge",
      "position": [
        2272,
        -432
      ],
      "parameters": {
        "mode": "combine",
        "options": {},
        "combineBy": "combineAll"
      },
      "typeVersion": 3.2
    },
    {
      "id": "4827d788-9982-42f0-bf98-767ea5c890cc",
      "name": "Create folder",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        1792,
        -16
      ],
      "parameters": {
        "name": "={{ $('Get email').item.json.from.value[0].address }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "list",
          "value": "1EitwWVd5rKZTlvOreB4R-632zfEOyWzP",
          "cachedResultUrl": "https://drive.google.com/drive/folders/1EitwWVd5rKZTlvOreB4R-632zfEOyWzP",
          "cachedResultName": "Email Attachments"
        },
        "resource": "folder"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "b77f08ca-70f3-4f4e-87dc-3c0cd365b3ee",
      "name": "When Executed by Another Workflow",
      "type": "n8n-nodes-base.executeWorkflowTrigger",
      "position": [
        2656,
        -656
      ],
      "parameters": {
        "inputSource": "passthrough"
      },
      "typeVersion": 1.1
    },
    {
      "id": "4b81e6cb-784b-4cef-a65f-5b3664e52db5",
      "name": "Execute Workflow",
      "type": "n8n-nodes-base.executeWorkflow",
      "position": [
        2640,
        -432
      ],
      "parameters": {
        "mode": "each",
        "options": {
          "waitForSubWorkflow": true
        },
        "workflowId": {
          "__rl": true,
          "mode": "id",
          "value": "kAiLbJeChaiOyg6T"
        },
        "workflowInputs": {
          "value": {},
          "schema": [],
          "mappingMode": "defineBelow",
          "matchingColumns": [],
          "attemptToConvertTypes": false,
          "convertFieldsToString": true
        }
      },
      "typeVersion": 1.2
    },
    {
      "id": "d50b2f82-1120-4496-ad33-7196b8996723",
      "name": "Get emails",
      "type": "n8n-nodes-base.gmail",
      "position": [
        416,
        -464
      ],
      "parameters": {
        "simple": false,
        "filters": {
          "receivedAfter": "2025-07-06T00:00:00",
          "receivedBefore": "2025-07-09T23:00:00"
        },
        "options": {
          "downloadAttachments": true
        },
        "operation": "getAll"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "c030f11a-dc3d-4e20-8ad2-46afafb9cbdb",
      "name": "Get email",
      "type": "n8n-nodes-base.gmail",
      "position": [
        1104,
        -448
      ],
      "parameters": {
        "simple": false,
        "options": {
          "downloadAttachments": true
        },
        "messageId": "={{ $json.id }}",
        "operation": "get"
      },
      "credentials": {
        "gmailOAuth2": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 2.1
    },
    {
      "id": "53ca4e5b-5840-473a-8343-1ea07f4ea9b0",
      "name": "Contain attachments?",
      "type": "n8n-nodes-base.if",
      "position": [
        1104,
        -160
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "fb9c769f-1a9d-44a5-a655-bee8f53e573e",
              "operator": {
                "type": "object",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $binary }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "11f81936-1b0a-4dc8-b4a2-910c6b7622be",
      "name": "Search folders",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        1360,
        -176
      ],
      "parameters": {
        "limit": 1,
        "filter": {
          "folderId": {
            "__rl": true,
            "mode": "list",
            "value": "1EitwWVd5rKZTlvOreB4R-632zfEOyWzP",
            "cachedResultUrl": "https://drive.google.com/drive/folders/1EitwWVd5rKZTlvOreB4R-632zfEOyWzP",
            "cachedResultName": "Email Attachments"
          },
          "whatToSearch": "folders"
        },
        "options": {},
        "resource": "fileFolder",
        "queryString": "={{ $('Get email').item.json.from.value[0].address }}"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3,
      "alwaysOutputData": true
    },
    {
      "id": "89d037ce-e0ae-4199-9cf7-d3bd4aa4bf8d",
      "name": "Exist?",
      "type": "n8n-nodes-base.if",
      "position": [
        1616,
        -176
      ],
      "parameters": {
        "options": {},
        "conditions": {
          "options": {
            "version": 2,
            "leftValue": "",
            "caseSensitive": true,
            "typeValidation": "strict"
          },
          "combinator": "and",
          "conditions": [
            {
              "id": "f6b34be9-edeb-4ebc-bd18-bccf43c7b7db",
              "operator": {
                "type": "object",
                "operation": "notEmpty",
                "singleValue": true
              },
              "leftValue": "={{ $json }}",
              "rightValue": ""
            }
          ]
        }
      },
      "typeVersion": 2.2
    },
    {
      "id": "b2475306-50e8-434a-9ef0-efdb9afeb08e",
      "name": "Folder ID",
      "type": "n8n-nodes-base.set",
      "position": [
        1984,
        -192
      ],
      "parameters": {
        "options": {},
        "assignments": {
          "assignments": [
            {
              "id": "f148d473-dde0-444e-8060-1b8608b18d13",
              "name": "folder_id",
              "type": "string",
              "value": "={{ $json.id }}"
            }
          ]
        }
      },
      "typeVersion": 3.4
    },
    {
      "id": "97c82abe-87ac-4b72-bb00-1616b8ca4731",
      "name": "Get attachments",
      "type": "n8n-nodes-base.code",
      "position": [
        1824,
        -448
      ],
      "parameters": {
        "jsCode": "let results = [];\n\nfor (item of items) {\n    for (key of Object.keys(item.binary)) {\n        results.push({\n            json: {\n                fileName: item.binary[key].fileName\n            },\n            binary: {\n                data: item.binary[key],\n            }\n        });\n    }\n}\n\nreturn results;"
      },
      "typeVersion": 2
    },
    {
      "id": "3fa07a7b-505b-474b-9dba-2c8cd0bc02cc",
      "name": "Upload attachment",
      "type": "n8n-nodes-base.googleDrive",
      "position": [
        2928,
        -656
      ],
      "parameters": {
        "name": "={{ $data.fileName }}",
        "driveId": {
          "__rl": true,
          "mode": "list",
          "value": "My Drive"
        },
        "options": {},
        "folderId": {
          "__rl": true,
          "mode": "id",
          "value": "={{ $json.folder_id }}"
        },
        "inputDataFieldName": "=data"
      },
      "credentials": {
        "googleDriveOAuth2Api": {
          "name": "<your credential>"
        }
      },
      "typeVersion": 3
    },
    {
      "id": "772dd36f-ef1a-48bf-89e5-feba92b963c2",
      "name": "Sticky Note",
      "type": "n8n-nodes-base.stickyNote",
      "position": [
        112,
        -912
      ],
      "parameters": {
        "width": 704,
        "height": 208,
        "content": "## Organize Gmail Attachments in Google Drive Folders based on sender\u2019s email\n\nThis workflow automatically processes emails received in Gmail, extracts their attachments, and organizes them into specific folders in Google Drive based on the sender\u2019s email address.\n\n\n**Note**: The workflow avoids duplicates by checking folder existence before creation."
      },
      "typeVersion": 1
    }
  ],
  "active": false,
  "settings": {
    "executionOrder": "v1"
  },
  "versionId": "9e6eac7d-2e84-476a-a93b-63471017778c",
  "connections": {
    "Merge": {
      "main": [
        [
          {
            "node": "Execute Workflow",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Exist?": {
      "main": [
        [
          {
            "node": "Folder ID",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Create folder",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Folder ID": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 1
          }
        ]
      ]
    },
    "Get email": {
      "main": [
        [
          {
            "node": "Contain attachments?",
            "type": "main",
            "index": 0
          },
          {
            "node": "Split Out",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Split Out": {
      "main": [
        [
          {
            "node": "Get attachments",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get emails": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Create folder": {
      "main": [
        [
          {
            "node": "Folder ID",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Search folders": {
      "main": [
        [
          {
            "node": "Exist?",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Get attachments": {
      "main": [
        [
          {
            "node": "Merge",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Loop Over Items": {
      "main": [
        [],
        [
          {
            "node": "Get email",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Execute Workflow": {
      "main": [
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "Upload attachment": {
      "main": [
        []
      ]
    },
    "Contain attachments?": {
      "main": [
        [
          {
            "node": "Search folders",
            "type": "main",
            "index": 0
          }
        ],
        [
          {
            "node": "Loop Over Items",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When Executed by Another Workflow": {
      "main": [
        [
          {
            "node": "Upload attachment",
            "type": "main",
            "index": 0
          }
        ]
      ]
    },
    "When clicking \u2018Execute workflow\u2019": {
      "main": [
        [
          {
            "node": "Get emails",
            "type": "main",
            "index": 0
          }
        ]
      ]
    }
  }
}