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

**URL:** <https://community.cdata.com/t/how-i-can-get-base64-files-from-xml-file-and-send-them-with-email-as-attachments/396>\
**Category:** CData Arc\
**Tags:** question\
**Created:** [September 18, 2023, 8:09am UTC](https://community.cdata.com/t/how-i-can-get-base64-files-from-xml-file-and-send-them-with-email-as-attachments/396 "2023-09-18T08:09:07Z")\
**Posts on this page:** 3\
**Page:** 1

<div class="post-metadata">

**Author:** ![EdiSupportGir](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@EdiSupportGir](https://community.cdata.com/u/EdiSupportGir)\
**Post date:** [September 18, 2023, 8:09am UTC](https://community.cdata.com/t/how-i-can-get-base64-files-from-xml-file-and-send-them-with-email-as-attachments/396/1 "2023-09-18T08:09:07Z")

</div>

Hi,

I have a case where receiving&nbsp; 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)

```auto
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](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”\>  
&nbsp;&nbsp;  
cac:DocumentReference  
&nbsp; &nbsp; cbc:ID523651\</cbc:ID\>  
&nbsp; &nbsp; cbc:DocumentTypecomment\</cbc:DocumentType\>  
&nbsp; &nbsp; cbc:DocumentDescription Transporter: Agile Logistics AS - BIL - Bil (Vei transport)\</cbc:DocumentDescription\>  
&nbsp; \</cac:DocumentReference\>  
&nbsp; cac:DocumentReference  
&nbsp; &nbsp; cbc:ID126242\</cbc:ID\>  
&nbsp; &nbsp; cbc:StatusDocument\</cbc:Status\>  
&nbsp; &nbsp; cbc:DocumentTypePacking list\</cbc:DocumentType\>  
&nbsp; &nbsp; cbc:NoteT-2337-00M-1\</cbc:Note\>  
&nbsp; &nbsp; cac:Attachment  
&nbsp; &nbsp; &nbsp; cbc:EmbeddedDocumentBinaryObject **{base64 document}** \</cbc:EmbeddedDocumentBinaryObject\>  
&nbsp; &nbsp; &nbsp; cac:ExternalReference  
&nbsp; &nbsp; &nbsp; &nbsp; cbc:FileNamede03de30fc3e478bbb86e0856741.pdf\</cbc:FileName\>  
&nbsp; &nbsp; &nbsp; \</cac:ExternalReference\>  
&nbsp; &nbsp; \</cac:Attachment\>  
&nbsp; \</cac:DocumentReference\>  
&nbsp; cac:DocumentReference  
&nbsp; &nbsp; cbc:ID126242\</cbc:ID\>  
&nbsp; &nbsp; cbc:StatusDocument\</cbc:Status\>  
&nbsp; &nbsp; cbc:DocumentTypeWaybill\</cbc:DocumentType\>  
&nbsp; &nbsp; cbc:NoteT-2337-00M-1\</cbc:Note\>  
&nbsp; &nbsp; cac:Attachment  
&nbsp; &nbsp; &nbsp; cbc:EmbeddedDocumentBinaryObject **{base64 document}** \</cbc:EmbeddedDocumentBinaryObject\>  
&nbsp; &nbsp; &nbsp; cac:ExternalReference  
&nbsp; &nbsp; &nbsp; &nbsp; cbc:FileNamede03de30fc3e478bbb86e0856741ec9c.pdf\</cbc:FileName\>  
&nbsp; &nbsp; &nbsp; \</cac:ExternalReference\>  
&nbsp; &nbsp; \</cac:Attachment\>  
&nbsp; \</cac:DocumentReference\>  
\</ForwardingInstructions\>

---

<div class="post-metadata">

**Author:** ![James\_B](https://sea3.discourse-cdn.com/cdata/user_avatar/community.cdata.com/james_b/32/1688_2.png) [@James\_B](https://community.cdata.com/u/James_B)\
**Post date:** [September 18, 2023, 4:06pm UTC](https://community.cdata.com/t/how-i-can-get-base64-files-from-xml-file-and-send-them-with-email-as-attachments/396/2 "2023-09-18T16:06:07Z")

</div>

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&nbsp;[xmlDOMSearch](https://cdn.cdata.com/help/AZJ/mft/op_XMLDOMSearch.html)&nbsp;and&nbsp;[encDecode](https://cdn.cdata.com/help/AZJ/mft/op_encDecode.html)&nbsp;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:

```xml
<!-- 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&nbsp; **Attachment** &nbsp;mode.

---

<div class="post-metadata">

**Author:** ![EdiSupportGir](https://avatars.discourse-cdn.com/v4/letter/e/898d66/32.png) [@EdiSupportGir](https://community.cdata.com/u/EdiSupportGir)\
**Post date:** [September 19, 2023, 12:26pm UTC](https://community.cdata.com/t/how-i-can-get-base64-files-from-xml-file-and-send-them-with-email-as-attachments/396/3 "2023-09-19T12:26:18Z")

</div>

> [@James\_B](#):
>
> 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&nbsp;[xmlDOMSearch](https://cdn.cdata.com/help/AZJ/mft/op_XMLDOMSearch.html)&nbsp;and&nbsp;[encDecode](https://cdn.cdata.com/help/AZJ/mft/op_encDecode.html)&nbsp;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:
> 
> ```xml
> <!-- 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&nbsp; **Attachment** &nbsp;mode.

Hello @James_B

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

![e2b09b1c-2837-4122-a4f7-18bbe50842c5.png](https://us1.discourse-cdn.com/cdata/original/2X/2/263d76ae331094789a975e00d60b0b38fa278988.png)

 ![32cd68be-3387-4763-ad3f-c53028081b5c.png](https://us1.discourse-cdn.com/cdata/original/1X/90ffd6e4be0446c80c47a9ab703e81101795acd6.png)
