Imap email attachment to drive cloud

I just set n8n on our unraid server with the idea of getting some flow automation.

First one I want to do, get email attachment and save it to drive (nextcloud as of now, might eventually change).
I got a template exactly for that but wasn’t working as it’s using the previous node version instead of “code”.
I got it working for getting the new emails from imap, I am just not sure how to handle the attachment to send it to save node.
I want to pass only the attachment, not everything else, bit I also want to save attachment in a client folder with subject (email address and subject fields).

The other thing, I want to do that for new emails but would like to do that for all history emails on the server. Anyway to do that without requiring to turn all emails as unread or something like that ?

The last thing I want/need is pdf merge. Is that something possible? Merge a po to a received invoice and save it with data read from the first pdf. (I know how to handle pdf read, not sure best way to handle the merge).

Hi @Jean-Nicolas_Turcott ,

  1. Since the new 1.0 update, some of the code nodes on templates are broken. I recommend using ChatGPT to fix and include the error message from the code node to fix.
  2. If you get the attachment as a binary blob, you’ll probably need the Move Binary Data node to convert it to JSON to then send to nextcloud, unless nextcloud supports uploading of binary data, in which case you will need to tell nextcloud the key of the binary data to send it over.
  3. You will want to extract from the email into a Set node the data to transform, so in this case the subject, email address, and binary data.
  4. To avoid infinite looping on emails, I recommend using the Folder/Label/Tagging feature of your email server. To simplify, I will refer to them as labels. I follow this process when automating emails:
  • Create a label for the email to initially fall into that is not the inbox. I use mail routing rules to automatically route emails like this to a parent label (for example, if email has this subject and this sender, put in this label). In this case something like a label called Emails to Process.
  • Create a second label for emails that have been processed by n8n, in this case maybe Processed Emails. If you can put both of these labels in a parent label called n8n, then it’s easier to keep an eye on simply by looking at your email and you can create more of these targeted flows inside the n8n label. They should look like this:
    • n8n/Emails to Process
    • n8n/Processed Emails
  1. At the beginning of your n8n flow, only call on emails that are in the n8n/Emails to Process label to trigger the flow.
  2. After you have pulled the attachment and uploaded it to nextcloud with the data you want, add the n8n/Processed Emails label to the email and remove the n8n/Emails to Process label. This will ensure that the next time it fires, it will check the n8n/Emails to Process label and nothing will be found there as n8n has moved it to a different label.
  3. To do all the emails, you may need to change the flow. There are several ways to build this, either targeting one email at a time as it comes in, or building it as Cron/Timer job that checks all emails in the folder and processes them all at once. This may complicate things as you will need to use the Split in Batches node to loop on the nextcloud upload if it does not support multiple file uploads at once. It all depends on how fast you want these processed. If it’s important to do them as soon as they arrive, you will want to build both flows (the bulk one is the same as the single one, but adds a couple extra nodes at the beginning to handle all the incoming emails). If time is not important, you can just build the bulk one and have it fire at the end of each day. To process all your emails, select all and add the process label.
  4. The merge question is doable, but it’s complicated. While n8n can read pdfs, it cannot yet easily create a new pdf from another one without passing the pdf to a different api to process. You will instead want to screenshot your PDF, and use the Edit Image Node to overlay an image of the PO number on top of the PDF image. From there, if you still want a PDF output instead of an image output, you will need to pass it to a 3rd party service to convert it like https://apitemplate.io which has a node.

This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.