Skip to content

Instantly share code, notes, and snippets.

@foosel
Created January 29, 2022 14:13
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save foosel/d9954fe3961124a1c2d44fa12aa8dbfc to your computer and use it in GitHub Desktop.
Save foosel/d9954fe3961124a1c2d44fa12aa8dbfc to your computer and use it in GitHub Desktop.
Email to Paperless Flow for NodeRed

Flow

[
    {
        "id": "c4be0c03394f3820",
        "type": "e-mail in",
        "z": "8357254d.c82348",
        "name": "Labeled \"paperless\"",
        "protocol": "IMAP",
        "server": "imap.gmail.com",
        "useSSL": true,
        "port": "993",
        "box": "paperless",
        "disposition": "Delete",
        "criteria": "ALL",
        "repeat": "300",
        "fetch": "auto",
        "inputs": 0,
        "credentials": {},
        "x": 150,
        "y": 1620,
        "wires": [
            [
                "e35b453e84d00cd0"
            ]
        ]
    },
    {
        "id": "c2bd3c483e183a98",
        "type": "http request",
        "z": "8357254d.c82348",
        "name": "",
        "method": "use",
        "ret": "txt",
        "paytoqs": "ignore",
        "url": "",
        "tls": "",
        "persist": false,
        "proxy": "",
        "authType": "",
        "senderr": false,
        "credentials": {
            "user": "",
            "password": ""
        },
        "x": 850,
        "y": 1620,
        "wires": [
            []
        ]
    },
    {
        "id": "e35b453e84d00cd0",
        "type": "function",
        "z": "8357254d.c82348",
        "name": "",
        "func": "const url = \"http://your.paperless.host/api/documents/post_document/\";\nconst token = \"xxxxxx\";\n\nconst attachments = msg.attachments;\nif (!attachments) return;\n\nconst createMsg = (attachment => {\n    return {\n        \"method\": \"POST\",\n        \"url\": url,\n        \"headers\": {\n            \"Authorization\": `Token ${token}`,\n            \"Content-Type\": \"multipart/form-data\"\n        },\n        \"payload\": {\n            \"document\": {\n                \"value\": attachment.content,\n                \"options\": {\n                    \"filename\": attachment.filename\n                }\n            }\n        }\n    }    \n})\n\nreturn [attachments.map(createMsg)];\n",
        "outputs": 1,
        "noerr": 0,
        "initialize": "",
        "finalize": "",
        "libs": [],
        "x": 540,
        "y": 1620,
        "wires": [
            [
                "c2bd3c483e183a98"
            ]
        ]
    }
]

Usage

  1. Add your email credentials to the email node. If needed, adjust the label/folder.
  2. Set your paperless-ng instance URL and auth token in the function node. To get an auth token, go into your paperless-ng admin panel, section "Auth Token" and create one for your user.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment