How-To-Unzip a zip file with XML and PDF Files and send Email with XML as mail body and PDF as attachments
The Objective of this blog post is to provide detailed information on:
- How-To Unzip a .zip file containing an XML file & Multiple PDF Files and send an Email with XML file contents in Mail body and PDF files as attachment
- Configurations & development required in SAP PI to achieve this scenario
Invoice Files / Service Report Files, zipped into a single .zip file is sent to middleware system (PI) by third party system.
The zip contains an .xml file and multiple .pdf files in sequence.
PI is required to perform the following steps –
- Unzip the .zip file
- Use the .xml file to read the mail related parameters – From recipient, To recipient, Subject, Mail body etc. and send Email using Mail Adapter using these mail attributes
- Use the .pdf files to send as attachments in the same Email with above contents
Approach to find Solution
A Java Mapping could have been implemented where Java code would easily unzip the .zip file and send the individual files over Email thereby achieving the entire requirement.
However, the requirement is achieved keeping in mind the maximum use of standard features available in SAP PI.
Interface Details –
- The .zip file is sent from Third Party to PI File Directory via OpenFT (Open File Transfer)
- PI Sender File Adapter polls this directory and picks up the .zip file
- SAP Standard Module PayloadZipBean unzips the .zip file into XML File and PDF Files
- Operation Mapping reads the XML File to prepare the Email Body using Mail package as receiver structure
- PDF files are sent as attachments to the Email recipients via Mail adapter
- Prior knowledge on SAP PI development and configuration
- Standard Modules/Beans provided by SAP
ESR Design Steps
Source Structure: The source structure is based on the XML file received from Third Party system. Below is the structure created with Mail information related fields as highlighted
Target Structure: Mail package is used for receiver structure, the external definition of which is available freely.
Create the corresponding Outbound and Inbound Service Interface for Source XML and Target Mail Package XSD.
Create Message Mapping to map the key fields in the Mail Package structure with data coming in from the XML file as below –
Create Operation Mapping assigning the respective Service Interfaces and Message Mapping.
Key detail to note here is the checkbox – “Read Attachments”.
This will enable the PI Operation Mapping to pass the attachments as received to the destination.
ID Configuration Details
Sender Communication Channel: Below is the configuration of the Sender File Adapter.
Key detail to note is the Module Configuration for PayloadZipBean.
This standard SAP Module – PayloadZipBean is used to zip/unzip files.
While unzipping, the following behavior is followed in case of multiple files.
- First file inside the zip is converted to main Payload
- Rest of the files, following the first file, are converted into attachments.
|Module Name||Type||Module Key|
|AF_Modules/PayloadZipBean||Local Enterprise Bean||1|
|Module Key||Parameter Name||Parameter Value|
Receiver Communication Channel: Below is the configuration of the Receiver Mail Adapter.
Key Detail to note here is for the Mail Attributes, we select the checkbox “Use Mail Package” and “Keep Attachments”
The Mail adapter provides two options –
One using the mail package & other without using mail package.
When you do not use mail package you have to provide the email header details (like To and From addresses, Subject line etc.) in the Mail adapter.
However, since we are using mail package here, we have provided these details in the mapping as shown in ESR section above. Hence we enable the checkbox – “Use Mail Package”
“Keep Attachment” makes sure that the attachments received in PI from Sender system are further sent to destination as attachments intact.
A File to Mail scenario is created. PI would read .zip file from the PI Directory, unzip the files using PayloadZipBean and send them to the destination over Email adapter with XML file data in Main Payload and PDF file as attachments.
This completes the Interface Setup in PI.
Step1 – A .zip file comes over to PI Directory from Sending System containing an XML file and multiple PDF files as below.
Step2 – Sender File Adapter picks up the ZIP File. Message Monitoring Logs shows the End to End audit logs of the message.
Step3 – Message monitoring showing the Message Content as below.
Step 4 – Open the message to observe the Before Message Mapping, After Message Mapping and Attachments tab.
Before Message Mapping –
After Message Mapping –
Step5 – Mail received to the recipients with the mail attributes used from the XML file.
PDF files are received in the same email as attachments.
SAP provides a standard feature to unzip multiple files contained in a .zip file using PayloadZipBean.
The development in SAP PI has made it possible to unzip a zip file containing an XML file and multiple PDF files in sequence and use XML data to form the Email body and PDF files to form the Email Attachments.
Plenty of blog posts are available in SCN for the exactly reverse scenario – Mail to File implementing Java Mapping to achieve similar functionality.
However, not many explain the current scenario – File to Mail using Standard SAP Feature.
Hope this blog post is useful for many SAP PI practitioners who have a similar requirement to unzip a .zip file containing multiple files and implementing a File to Mail Scenario.
Your feedback and improvement ideas are welcome!