Solved

How I can get base64 files from XML file and send them with email as attachments

  • 18 September 2023
  • 2 replies
  • 160 views

Userlevel 2
Badge

Hi,

 

I have a case where receiving  XML files with base64 (e.g. PDF)files inside the file which ones do I want to send as attachments in an email with Email Send connector (Template mode)

maybe someone knows what script could be used?

XML example

<?xml version="1.0" encoding="utf-8"?>
<ForwardingInstructions xmlns="urn:oasis:names:specification:ubl:schema:xsd:ForwardingInstructions-2" xmlns:dtd="http://dtd.riege.com/scope/shippingorder" xmlns:cbc="urn:oasis:names:specification:ubl:schema:xsd:CommonBasicComponents-2" xmlns:cac="urn:oasis:names:specification:ubl:schema:xsd:CommonAggregateComponents-2">
  
<cac:DocumentReference>
    <cbc:ID>523651</cbc:ID>
    <cbc:DocumentType>comment</cbc:DocumentType>
    <cbc:DocumentDescription> Transporter: Agile Logistics AS - BIL - Bil (Vei transport)</cbc:DocumentDescription>
  </cac:DocumentReference>
  <cac:DocumentReference>
    <cbc:ID>126242</cbc:ID>
    <cbc:Status>Document</cbc:Status>
    <cbc:DocumentType>Packing list</cbc:DocumentType>
    <cbc:Note>T-2337-00M-1</cbc:Note>
    <cac:Attachment>
      <cbc:EmbeddedDocumentBinaryObject>{base64 document}</cbc:EmbeddedDocumentBinaryObject>
      <cac:ExternalReference>
        <cbc:FileName>de03de30fc3e478bbb86e0856741.pdf</cbc:FileName>
      </cac:ExternalReference>
    </cac:Attachment>
  </cac:DocumentReference>
  <cac:DocumentReference>
    <cbc:ID>126242</cbc:ID>
    <cbc:Status>Document</cbc:Status>
    <cbc:DocumentType>Waybill</cbc:DocumentType>
    <cbc:Note>T-2337-00M-1</cbc:Note>
    <cac:Attachment>
      <cbc:EmbeddedDocumentBinaryObject>{base64 document} </cbc:EmbeddedDocumentBinaryObject>
      <cac:ExternalReference>
        <cbc:FileName>de03de30fc3e478bbb86e0856741ec9c.pdf</cbc:FileName>
      </cac:ExternalReference>
    </cac:Attachment>
  </cac:DocumentReference>
</ForwardingInstructions>

 

icon

Best answer by James B 18 September 2023, 18:06

View original

2 replies

Userlevel 6
Badge

Unfortunately, you will not be able to use the Template mode of the EmailSend connector to dynamically generate a message with attachments - so it will not be possible to send a single message that contains your PDFs as attachments.

 

There is code that you can use in a previous step in the flow to scan this XML file, and decode and output the pdf’s as invidual messages. To do this, you’d need to use the xmlDOMSearch and encDecode operations to search the incoming document and output each file individually - you can also add headers to the message if there are other elements you need for the email body:

 

<!-- look for XPaths in the document -->
<arc:set attr="xml.uri" value="[filepath]" />
<arc:set attr="xml.xpath" value="/ForwardingInstructions/cac:DocumentReference" />

<arc:call op="xmlDOMSearch" in="xml">
<arc:if exp="[xpath('cbc:Status') | def | equals('Document')]">
<!-- here, you can add any other headers if you need more metadata in the email step -->
<arc:set attr="output.header:ID" value="[xpath(cbc:ID)]"/>

<!-- this code decodes the base64 data to a temp path on disk
you'll need write permissions here -->
<arc:set attr="output.filename" value="[xpath(cac:Attachment/cac:ExternalReference/cbc:Filename)]" />
<arc:set attr="enc.data" value="[xpath(cac:Attachment/cbc:EmbeddedDocumentBinaryObject)]" />
<arc:set attr="enc.outfile" value="C:\\Temp\\[output.filename]" />
<arc:set attr="enc.format" value="BASE64" />
<arc:call op="encDecode" item="enc" />

<!-- this outputs the pdf as a new message -->
<arc:set attr="output.filepath" value="[enc.outfile]" />
<arc:push item="output" />

<!-- clean up the temp file -->
<arc:set attr="del.file" value="[enc.outfile]" />
<arc:call op="fileDelete" item="del"/>
</arc:if>
</arc:call>

 

But note that this would output one file per PDF and each would send as a separate message in EmailSend in Attachment mode. 

Userlevel 2
Badge

Unfortunately, you will not be able to use the Template mode of the EmailSend connector to dynamically generate a message with attachments - so it will not be possible to send a single message that contains your PDFs as attachments.

 

There is code that you can use in a previous step in the flow to scan this XML file, and decode and output the pdf’s as invidual messages. To do this, you’d need to use the xmlDOMSearch and encDecode operations to search the incoming document and output each file individually - you can also add headers to the message if there are other elements you need for the email body:

 

<!-- look for XPaths in the document -->
<arc:set attr="xml.uri" value="[filepath]" />
<arc:set attr="xml.xpath" value="/ForwardingInstructions/cac:DocumentReference" />

<arc:call op="xmlDOMSearch" in="xml">
<arc:if exp="[xpath('cbc:Status') | def | equals('Document')]">
<!-- here, you can add any other headers if you need more metadata in the email step -->
<arc:set attr="output.header:ID" value="[xpath(cbc:ID)]"/>

<!-- this code decodes the base64 data to a temp path on disk
you'll need write permissions here -->
<arc:set attr="output.filename" value="[xpath(cac:Attachment/cac:ExternalReference/cbc:Filename)]" />
<arc:set attr="enc.data" value="[xpath(cac:Attachment/cbc:EmbeddedDocumentBinaryObject)]" />
<arc:set attr="enc.outfile" value="C:\\Temp\\[output.filename]" />
<arc:set attr="enc.format" value="BASE64" />
<arc:call op="encDecode" item="enc" />

<!-- this outputs the pdf as a new message -->
<arc:set attr="output.filepath" value="[enc.outfile]" />
<arc:push item="output" />

<!-- clean up the temp file -->
<arc:set attr="del.file" value="[enc.outfile]" />
<arc:call op="fileDelete" item="del"/>
</arc:if>
</arc:call>

 

But note that this would output one file per PDF and each would send as a separate message in EmailSend in Attachment mode. 

Hello @James B 

Is it possible to send PDFs in one email with the same message header or if the filename begins the same?  For example 

 

Reply