Read/Write/Save attachments from EmailReadImap

Hey Jan!

Is it possible to store/read the attachments of new emails that trigger the EmailReadImap trigger?

I would like to process the attachments (XML conversion for example).

Regards Sebastian

Ah yes, that is no problem. Simply activate the “Download Attachment” option on the node. Afterwards, attach the XML-Node and then set the “Property Name” accordingly. If you use the default value it would be “attachment_0” for the first attachment.

What if there are multiple attachments?

Then they are additional ones called “attachment_1”, “attachment_2”, and so on.

I can not get this to work:

I want to get XML Data from inside the attachment.

Saving the file via Write Binary File puts it ito /data in the docker container. I can read from there. But how do I get the contents?

So lets say my E-Mail has sometimes 3 and sometimes 5 attachments. Is there a good way to batch save and process them?

Regards Sebastian

Looks like you do not have an email with an attachment. You have to start the workflow, then send an email with the attachment and then wait till the workflow gets the data. Very similar like what gets done with the Webhook here:

If you need a different amount of attachments you probably have to use a Function-Node to split them into different items which all have the same name like “data” or “attachment”. Then the XML-Node will process all of them.

My email has an attachment. I can see it as Input data -> Binary -> attachment_0 in the XML node. But the Object only offers mimeType, fileName and fileExtension as fields

It sounds like you want to create an expression. That should not be needed. Simply write “attachment_0” in the field.

Hi Jan,
I have the same problem. Following what you said I have the attachment_0 ready in the binary tab and wrote it to disc successfully.

But when I try to convert to XML or use it in any (non-binary) node I get
ERROR: No json property “attachment_0” does not exists on item!

Same happens when I load the written file.

1 Like

This is what I was experiencing too.

Yes the problem is here that there is currently no node yet to move data between the “binary” and “json” data. I Try to create one soon.

Until then you can use the workaround I did post here:

I already came across that workflow. Unfortunately this is the other way around as it describes the way to create a file from binary (which does work).

What we need is to read binary code into a variable to work with it. The only node that does it right now is the Spreadsheet node, so you have to give it some sort of spreadsheet-ish input - which is messy.

Update:
I thought way too complicated. Easy workaround is: Write Binary, Execute Command “cat filename.ext”, keep using the stdout part. d’ ugh :slight_smile:

A custom Function-Node in the other direction (from binary -> JSON) would work like this:

{
  "nodes": [
    {
      "parameters": {
        "functionCode": "items[0].json = JSON.parse(new Buffer(items[0].binary.data.data, 'base64').toString('ascii'));\n\nreturn items;"
      },
      "name": "Create JSON",
      "type": "n8n-nodes-base.function",
      "typeVersion": 1,
      "position": [
        100,
        300
      ]
    }
  ],
  "connections": {}
}

In the latest version of [email protected] there is now a special node called “Move Binary Data”:
https://n8n.io/nodes/n8n-nodes-base.moveBinaryData
It allows moving data between binary and JSON.

Please put up a 0.31.0-ubuntu docker image too :slight_smile:

Is done. Released 0.31.0-ubuntu

1 Like

Hey Jan!

2 questions:
1.) Why do you use ascii as the encoding?
2.) my Attachment for example contains XML data. This fails the JSON.parse as this Node basically can convert base64 encoded JSON to JSON and vice versa (https://github.com/n8n-io/n8n/blob/master/packages/nodes-base/nodes/MoveBinaryData.node.ts#L238). How would I set the text content of the XML attachment to a new key in the Data to be able to use the XML node to convert the value of the key to XML?

Here is a sample XML file

<?xml version="1.0" encoding="utf-8"?>
<ORDERS05>
  <IDOC BEGIN="1">
    <EDI_DC40 SEGMENT="1">
      <TABNAM>EDI_DC40</TABNAM>
      <MANDT>100</MANDT>
      <DOCNUM>0000000000075943</DOCNUM>
      <DOCREL>750</DOCREL>
      <STATUS>03</STATUS>
      <DIRECT>1</DIRECT>
      <OUTMOD>2</OUTMOD>
      <IDOCTYP>ORDERS05</IDOCTYP>
      <MESTYP>ORDERS</MESTYP>
      <SNDPOR>SAPTEP</SNDPOR>
      <SNDPRT>LS</SNDPRT>
      <SNDPRN>TEPCLNT100</SNDPRN>
      <RCVPOR>XML_ORDERS</RCVPOR>
      <RCVPRT>LI</RCVPRT>
      <RCVPFC>LF</RCVPFC>
      <RCVPRN>0000123456</RCVPRN>
      <CREDAT>20180724</CREDAT>
      <CRETIM>150629</CRETIM>
      <SERIAL>20180724150629</SERIAL>
    </EDI_DC40>
    <E1EDK01 SEGMENT="1">
      <CURCY>EUR</CURCY>
      <HWAER>EUR</HWAER>
      <WKURS>1.00000</WKURS>
      <ZTERM>ZB15</ZTERM>
      <BSART>NB</BSART>
      <BELNR>7510045471</BELNR>
      <RECIPNT_NO>0000123456</RECIPNT_NO>
    </E1EDK01>
    <E1EDK14 SEGMENT="1">
      <QUALF>014</QUALF>
      <ORGID>1000</ORGID>
    </E1EDK14>
    <E1EDK14 SEGMENT="1">
      <QUALF>009</QUALF>
      <ORGID>S81</ORGID>
    </E1EDK14>
    <E1EDK14 SEGMENT="1">
      <QUALF>013</QUALF>
      <ORGID>NB</ORGID>
    </E1EDK14>
    <E1EDK14 SEGMENT="1">
      <QUALF>011</QUALF>
      <ORGID>1000</ORGID>
    </E1EDK14>
    <E1EDK03 SEGMENT="1">
      <IDDAT>012</IDDAT>
      <DATUM>20180724</DATUM>
    </E1EDK03>
    <E1EDK03 SEGMENT="1">
      <IDDAT>011</IDDAT>
      <DATUM>20180724</DATUM>
    </E1EDK03>
    <E1EDKA1 SEGMENT="1">
      <PARVW>AG</PARVW>
      <PARTN>1000</PARTN>
      <TELF1>3</TELF1>
      <TELFX></TELFX>
      <BNAME</BNAME>
      <PAORG>1000</PAORG>
      <ORGTX></ORGTX>
      <PAGRU>S81</PAGRU>
    </E1EDKA1>
    <E1EDKA1 SEGMENT="1">
      <PARVW>LF</PARVW>
      <PARTN>0000123456</PARTN>
      <SPRAS>D</SPRAS>
      <SPRAS_ISO>DE</SPRAS_ISO>
    </E1EDKA1>
    <E1EDKA1 SEGMENT="1">
      <PARVW>WE</PARVW>
      <LIFNR>1000</LIFNR>
      <NAME1></NAME1>
      <NAME2></NAME2>
      <NAME3></NAME3>
      <STRAS></STRAS>
      <ORT01></ORT01>
      <PSTLZ></PSTLZ>
      <LAND1>DE</LAND1>
      <SPRAS>D</SPRAS>
      <REGIO>08</REGIO>
      <SPRAS_ISO>DE</SPRAS_ISO>
    </E1EDKA1>
    <E1EDK02 SEGMENT="1">
      <QUALF>001</QUALF>
      <BELNR>7510045471</BELNR>
      <DATUM>20180724</DATUM>
      <UZEIT>150629</UZEIT>
    </E1EDK02>
    <E1EDK17 SEGMENT="1">
      <QUALF>001</QUALF>
      <LKOND>AWE</LKOND>
    </E1EDK17>
    <E1EDK18 SEGMENT="1">
      <QUALF>001</QUALF>
      <TAGE>14</TAGE>
      <PRZNT>3.000</PRZNT>
    </E1EDK18>
    <E1EDK18 SEGMENT="1">
      <QUALF>002</QUALF>
      <TAGE>30</TAGE>
    </E1EDK18>
    <E1EDKT1 SEGMENT="1">
      <TDID>ST</TDID>
      <TSSPRAS>D</TSSPRAS>
      <TSSPRAS_ISO>DE</TSSPRAS_ISO>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Wir bestellen hiermit unter ausschließlicher Geltung der</TDLINE>
        <TDFORMAT>L</TDFORMAT>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Einkaufsbedingungen der </TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Gruppe. Die vorstehend genannten Einkaufsbedingungen können auf der</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Internetseite </TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>abgerufen, heruntergeladen und ausgedruckt werden oder werden Ihnen</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>auf Anforderung von uns zur Verfügung gestellt. Unsere</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Einkaufsbedingungen gelten ausschließlich. Entgegenstehende oder</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>abweichende Bedingungen des Lieferanten erkennen wir nicht an, es sei</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>denn, wir hätten ausdrücklich schriftlich deren Geltung zugestimmt.</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Unsere Einkaufsbedingungen gelten auch dann, wenn wir in Kenntnis</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>entgegenstehender oder von unseren Einkaufsbedingungen abweichender</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Bedingungen des Lieferanten die Lieferung oder Leistung des</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Lieferanten vorbehaltlos annehmen.</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Die zu liefernden Komponenten haben den für sie geltenden gesetzlichen</TDLINE>
        <TDFORMAT>L</TDFORMAT>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Bestimmungen (z.B. CE-Kennzeichen, VDE, GS, TÜV, ...) zu entsprechen.</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>&lt;H&gt;Bitte geben Sie auf allen Papieren unsere Bestell-/Pos.-Nr. sowie</TDLINE>
        <TDFORMAT>L</TDFORMAT>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>alle notwendigen Angaben für die Intrastatmeldung (Zolltarifnummer,</TDLINE>
      </E1EDKT2>
      <E1EDKT2 SEGMENT="1">
        <TDLINE>Ursprungsland, Gewicht, besondere Maßeinheiten) an!&lt;/&gt;</TDLINE>
      </E1EDKT2>
    </E1EDKT1>
    <E1EDP01 SEGMENT="1">
      <POSEX>00010</POSEX>
      <ACTION>001</ACTION>
      <PSTYP>0</PSTYP>
      <MENGE>10.000</MENGE>
      <MENEE>PK</MENEE>
      <BMNG2>10.000</BMNG2>
      <PMENE>PK</PMENE>
      <VPREI>55.81</VPREI>
      <PEINH>1</PEINH>
      <NETWR>558.1</NETWR>
      <MATKL>P99</MATKL>
      <BPUMN>1</BPUMN>
      <BPUMZ>1</BPUMZ>
      <WERKS>1000</WERKS>
      <E1EDP20 SEGMENT="1">
        <WMENG>10.000</WMENG>
        <AMENG>0.000</AMENG>
        <EDATU>20180824</EDATU>
      </E1EDP20>
      <E1EDP19 SEGMENT="1">
        <QUALF>002</QUALF>
        <IDTNR>7063752</IDTNR>
      </E1EDP19>
      <E1EDP19 SEGMENT="1">
        <QUALF>001</QUALF>
        <KTEXT></KTEXT>
      </E1EDP19>
    </E1EDP01>
    <E1EDS01 SEGMENT="1">
      <SUMID>002</SUMID>
      <SUMME>558.1</SUMME>
      <SUNIT>EUR</SUNIT>
    </E1EDS01>
  </IDOC>
</ORDERS05>

I am able to console log the base64 and the decoded XML data when putting some console.log in the MoveBinaryData.node.ts file in the Docker image.

I now want to put the XML file content to new key xml. Wouldn’t I need JSON.stringyfy(new Buffer...) therefore?

Regards Sebastian

@vuchl Yes, that is definitely not right. Should have been utf8 from the start.

I merged your pull request and made additional changes. Can you please check if it now works for you. Then I release 0.31.1 as I consider it a bug-fix.

@vuchl ah btw that should also solve your second problem as it makes JSON.parse optional.

You owerwrote the options.encoding with a hard coded utf8

Edit: Nevermind