Upload Multiple Email Attachments to FTP or S3

I currently have a workflow that is taking inbound email via IMAP Email and eventually sending elements of the data to a HTTP Request POST. What I’m hoping to do next is grab any attachments of the email, upload them to either Wasabi S3 (preferably) or FTP, and then grab the URL of the files and append them to the rest of the data from the IMAP Email so I can add it my JSON that is posted. My thinking is that I would go from IMAP Email to a function that splits the binary data.

for (let item in items) {
  if(Object.keys(items[item]).includes('binary')){
    return Object.keys(items[item].binary).map(key => {
    return {
      json: {},
      binary: {
        data: items[item].binary[key],
      }
    }
});
  }
}
return items;

From there, I would use S3 or FTP node to upload the attachments. But, it’s not clear if I can upload multiple attachments via either of those or if I have to iterate through the attachments individually. Then, I would use an Append node so that this:

[
   {
      "textHtml":"<b>Boo</b>",
      "textPlain":"Boo",
      "metadata":{
         "delivered-to":"[email protected]",
         "reply-to":"[email protected]"
      },
      "from":"Scary Stories <[email protected]>",
      "subject":"Spooky Attachments [Casper]",
      "date":"Thu, 16 Dec 2021 18:21:18 +0000 (UTC)",
      "to":"Email Account <[email protected]>"
   }
]

Becomes something like this:

[
   {
      "textHtml":"<b>Boo</b>",
      "textPlain":"Boo",
      "metadata":{
         "delivered-to":"[email protected]",
         "reply-to":"[email protected]"
      },
      "from":"Scary Stories <[email protected]>",
      "subject":"Spooky Attachments [Casper]",
      "date":"Thu, 16 Dec 2021 18:21:18 +0000 (UTC)",
      "to":"Email Account <[email protected]>"
   },
   {
      "attachments":[
         {
            "id":1,
            "url":"https://wasaby.sys/bucket/filename1.jpg"
         },
         {
            "id":2,
            "url":"https://wasaby.sys/bucket/filename1.jpg"
         }
      ]
   }
]

Due to how the endpoint expects to get the JSON, I have to construct it a bit differently and send it RAW. I use this function to create that JSON and would also appreciate help getting the attachments into this function.

items[0].json.sample = {
    "summary": $node["IMAP Email"].json["subject"],
    "details": $node["IMAP Email"].json["textHtml"],
    "user_id": $node["SetGenUser"].json["user_id"]
    }
    
return items;

Thank you.

Can you please share your workflow?

If I can avoid it, no. There would be a lot of cleaning up I’d have to do. I’ll see if I can recreate the workflow somehow with just the necessities and fake data.

Yes, you can create one with mockup data. It is complicated to come up with a proper solution without the workflow. At least for me.

Understood. That will take some time as I have to figure out how to mockup an email with attachments. Here’s the workflow with all the sensitive bits removed, which I suspect makes it mostly useless for your purposes. Key points here are that SplitBinaryData would then go to an FTP or S3 node. And the IF currently only has FALSE pointing somewhere, but essentially it would have almost identical flows after that point.

And any suggestions on mocking up an email with attachments would be greatly appreciated.

Check the example workflow below:

1 Like

Thanks @RicardoE105. This does work. I’ve implemented it in my workflow and subbed in the generic S3 nodes to work with Wasabi. The one issue I need to address now relates to the placement of these files. Wasabi doesn’t handle duplicates well, and if I have all the files uploaded to the root of the bucket, listing them all will result in attachments from other emails being listed as well. My plan here was to generate a unique folder and set that via Folder Key. The problem I’m running into is that if I use an expression to grab something unique from the IMAP Email node, like the message ID or the timestamp, the S3 node only uploads the first attachment and nothing else. If I set the Folder Key to just “test” or anything that isn’t an expression (even if it’s just an expression to run a date() and not grab from another node), it uploads all attachments. Any idea why that’s happening and how best to work around it?

Actually, disregard. I was able to use the $executionId to generate something that worked. The last thing I need to do but I just can’t put it together in my head; I’m using a function to create the JSON that I need to send to the endpoint. It looks like this:

items[0].json.sample = {
    "summary": $node["IMAP Email"].json["subject"],
    "details": $node["IMAP Email"].json["textHtml"],
    "user_id": $node["SetGenUser"].json["user_id"]
    }
    
return items;

I need to add the attachment URLs to the content of the “details” variable. So let’s say the current content of $node[“IMAP Email”].json[“textHtml”] is:

<b>Boo</b>

I would want it to become:

<b>Boo</b>
<br /><a href="https://wasabi.sys/211/Screenshot_3z1c3fwh.jpeg">Attachment</a>
<br /><a href="https://wasabi.sys/211/Screenshot_jslcoj3l.jpeg">Attachment</a>
<br /><a href="https://wasabi.sys/211/Screenshot_qjmu5pma.jpeg">Attachment</a>

Where what was created by the Split binary items workflow is:

"attachments":[
   {
      "url":"211/Screenshot_3z1c3fwh.jpeg"
   },
   {
      "url":"211/Screenshot_jslcoj3l.jpeg"
   },
   {
      "url":"211/Screenshot_qjmu5pma.jpeg"
   }
]
1 Like