Automate Email Invoice Saving to Nextcloud with n8n

This n8n workflow solves the tedious task of manually downloading email invoice attachments and organizing them in Nextcloud. It automatically extracts attachments from IMAP emails, sanitizes filenames, and uploads them to specific dated folders, saving hours of manual effort and reducing errors.
emailReadImap
function
nextCloud
Workflow Identifier: 2377
NODES in Use: EmailReadImap, Function, NextCloud
Automate email invoices with n8n and Nextcloud

Press CTRL+F5 if the workflow didn't load.

Learn how to Build this Workflow with AI:

What This Workflow Does

This workflow reads emails from an IMAP “Invoices” folder and takes all attachments out of those emails.

It fixes attachment filenames so they are clean and consistent.

It then puts the attachments into folders in Nextcloud, sorted by email sender and date automatically.

This stops manual downloading, renaming, and sorting, saving many work hours and stopping errors.


Tools and Services Used

  • IMAP Email: Reads emails from a specific mailbox folder and gets attachments.
  • Function Node: Cleans filenames and connects email info to files.
  • Nextcloud: Uploads and organizes files inside cloud folders by date and sender.

Inputs, Processing Steps, and Output

Inputs

  • Email account with access to an “Invoices” folder.
  • Emails containing invoice attachments.
  • Nextcloud with proper permissions to upload files.

Processing Steps

  • IMAP Email node fetches all emails from the “Invoices” folder with attachments.
  • Function node extracts each attachment, cleans filenames by removing invalid characters and shortening names, and adds sender and date metadata.
  • Nextcloud node uploads each cleaned file into folders named by the sender and email date.

Output

  • Invoices saved in Nextcloud under well-structured folders like Documents/Invoices/DATE_SENDER_FILENAME.
  • Less manual work and fewer file mistakes.

Beginner Step-by-Step: How to Use This Workflow in n8n

1. Import Workflow

  1. Download the workflow using the Download button on this page.
  2. Open n8n editor and select “Import from File”.
  3. Choose the downloaded workflow file to load it into n8n.

2. Configure Credentials

  1. Add required credentials for your IMAP email account in n8n credentials manager.
  2. Add Nextcloud API credentials in the same way.

3. Adjust Settings

  1. Ensure the IMAP node is set to the exact “Invoices” folder your emails use.
  2. Check the Nextcloud node folder path expression to match your naming preferences or company structure.

4. Test the Workflow

  1. Manually run the workflow inside n8n to check it downloads and uploads one or two files correctly.
  2. Review the output files in Nextcloud to confirm folder structure and filename cleanup.

5. Activate for Production

  1. Toggle the workflow ON in n8n to let it run automatically as scheduled or triggered.
  2. Monitor logs occasionally to catch errors early.

For self hosting n8n, consider self-host n8n to run this workflow continuously.


Customizations and Options

  • Change the Nextcloud path expression to group files by client name or project code.
  • Add a filter node after IMAP to only process PDFs or certain file types.
  • Add notification triggers, like email or Slack, to alert when new invoices upload.

Troubleshooting and Common Issues

No attachments found in Emails

This usually means no files attached or the IMAP node is not fetching attachments properly.

Check email content and IMAP access rights.

Nextcloud upload fails

Check Nextcloud API keys and the folder path expression.

Invalid path or permissions cause upload errors.


Summary

✓ Saves over 10 work hours each week from manual downloading and organizing.

✓ Removes errors from wrong filenames and misplaced files.

→ Automatically fetches invoice emails and extracts attachments.

→ Uploads cleaned files to accurate folders in Nextcloud sorted by date and sender.

→ Provides clear folder structure and consistent naming for quick invoice access.


const _ = require('lodash')

const sanitize = str => _.chain(str)
  .replace(/[^A-Za-z0-9&.-]/g, '-') // sanitise via whitelist of characters
  .replace(/-(?=-)/g, '') // remove repeated dashes
  .trim('-') // trim any leading/trailing dashes
  .truncate({
    length: 60,
    omission: '-'
  })
  .value()

const result = _.flatMap(items.map(item => {
  return _.values(item.binary).map(file => {
    const filename_parts = file.fileName.split('.')
    const ext = _.slice(filename_parts, filename_parts.length-1)
    const filename_main = _.join(_.dropRight(filename_parts), '.')
    file.fileName = sanitize(filename_main) + '.' + ext
    return {
      json: {
        from: sanitize(item.json.from.value[0].name),
        date: sanitize(new Date(item.json.date).toISOString().split("T")[0])
      }, 
      binary: { file }
    }
  })
}))

return result
Automate email invoices with n8n and Nextcloud

Visit through Desktop to Interact with the Workflow.

Promoted by BULDRR AI

Related Workflows

Automate Twist Channel Creation and Messaging with n8n

This workflow automates creating and updating a channel in Twist and sending a personalized message to specific users. It eliminates manual setup errors and saves time managing Twist communications.

Automate Ideogram Image Generation with Google Sheets & Gmail

This workflow automates graphic design image generation via Ideogram AI, storing image data in Google Sheets and Google Drive, with email alerts via Gmail. It saves designers hours by automating image creation, remixing, review, and record-keeping.

Automate IT Support with Slack and OpenAI in n8n

Streamline IT support by automating Slack message handling using n8n and OpenAI. This workflow handles Slack DMs, filters bots, queries a Confluence knowledge base, and delivers AI-generated responses, improving support efficiency and response time.

Automate Crypto Analysis with CoinMarketCap & n8n AI Agent

Discover how this unique n8n workflow leverages CoinMarketCap’s multi-agent AI to deliver precise, real-time cryptocurrency insights directly via Telegram. Manage crypto data analysis efficiently with automated multi-source API integration.

Automate Gumroad to Beehiiv Subscriber Sync with n8n

Learn how to automatically add new Gumroad sales customers as Beehiiv newsletter subscribers using n8n automation. This workflow saves time by syncing sales data to Google Sheets CRM and notifying your Telegram channel instantly.

Generate On-Brand Blog Articles Using n8n and OpenAI

This workflow automates the creation of on-brand blog articles by analyzing existing company content using n8n and OpenAI. It extracts article structures and brand voice to produce consistent draft articles, saving significant content creation time.
1:1 Free Strategy Session
Your competitors are already automating. Are you still paying for it manually?

Do you want to adopt AI Automation?

Every hour your team does repetitive work, you're burning real money.
While you wait, faster businesses are cutting costs and moving quicker.
AI and automations aren't the future anymore — they're the present.

Book a live 1-on-1 session where we show you exactly which of your daily tasks can be automated — and what it’s costing you not to.