Download attachments and rename them with the email subject

I’m trying to understand how to download attachments from my email account and save them in a folder, but renamed with the subject

I can’t figured out how save in a file with the original extensions, 'cause i do not know what kind of attachment will be in the next email.
I’m currently doing something like this:

I know this is easy but i’m a noob coder.

I’m using the latest n8n build in docker

Hope someone can help me
thanks

Hey @djun87,

Welcome to the community :raised_hands:

When you have an attachment in the email does the binary data view show an extension?

I can’t see attachment data on the output from the imap node.
I don’t know if imap node gives some kind of output about the attachment either

Hey @djun87,

I have just given this a quick test and for me if I have an attachment downloaded I can see the filename and extension.

This means if I wanted to use the subject name as the filename when saving it and the extension I can use an expression like this /files/{{$json["subject"]}}.{{$binary.attachment_0.fileExtension}} of course using the subject could lead to trying to use characters the OS can’t use in a filename so that would need to be thought about but a simple workflow that would do the job is below.

EDIT: everything works :smiley:

1 Like

Hey @djun87,

Multiple attachments is a bit trickier, If you look at the example workflow linked below there is a part for transformation. This will turn multiple attachments into multiple items, If you add that between the email node and the write binary data node it should do what you are after.

1 Like

I made a workflow that saves email attachments, rename with email subject limited to 20 chars, consequentially numbered and managed file extensions.
Hope this help someone

1 Like

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