File to Email with multiple attachments – SAP PO
I am sure many of you by now would have encountered scenarios to send multiple attachments within a Mail message to recipients. But our Mail Adapter as such treats them as “Untitled.xml” which is not acceptable.
Also in some cases we would be requiring to send data to different Mail servers like (Domino – lotus Notes) and all which will interpret the attachments in different way than standard Microsoft Outlook which is widely used.
There are many blogs in the SDN on this front using Java Mapping and Java UDF. One by my good friend Praveen Gujjeti as well ;).
Now lets see on how we can achieve this without using any CODING at all and using only standard function modules – ya correct i know at this point everyone reading this would be bit excited :):).
Scenario: let’s say there is a scenario where there are 5 files. Out of this 1 file will act as main payload which contains the from and To Email addresses, Subject and Body of a mail. And rest of them is txt files which need to be transmitted to the recipients as mail attachments.The files are as below.
File Names: Email.xml ,Email_TextFile1.txt,Email_TextFile2.txt,Email_TextFile3.txt,Email_TextFile4.txt
In here “Email.xml” will act as the main payload file which will contain the Email address, subject and body of the mail and using Mail packaging we will extract the information to the receiver Mail adapter.
And Email_TextFile1.txt, Email_TextFile2.txt,Email_ TextFile3.txt & Email_TextFile4.txt are the files which need to be attached to the email.
For Information on Mail Packaging ref: Receiver Mail Adapter
Here below i will show you 4 steps:
- How to configure sender adapter to pick up Additional Files as attachments.
- Message Mapping for Mail packaging.
- How to get the attachment Names from Payload – Integration Engine.
4. Receiver Mail adapter configuration for getting multiple attachments in proper format.
Sender Adapter Configuration for picking multiple files:
Things to rememebr:
- Addition Files only works in NFS mode. So make sure you find a way to temporarily place the files in NFS of PI.
- Here the property “*.optional” makes sure that the main payload “Email.xml” is not executed until all the attachment is available for pickup from the location. If set as YES then even if additional files do not exist. The mail payload will be processed by the channel.
Message Mapping Configuration for Mail packaging:
Things to remember:
- Receiver mail structure is standard. So you have to define under namespace: “http://sap.com/xi/XI/Mail/30” and Message Type should be “Mail” we cannot use any other namespace nor Message type for sending parameter for mail adapter using Mail packaging.
- Make sure from and To address are specified with valid email id’s.
- Specify Content_Type as “text/plain” without quotes.
4. Make sure each Email address is separated with a semicolon like: firstname.lastname@example.org;email@example.com
5. Also Subject and Content fields will contain the “to-be” subject and content of the mail respectively.
How to Get Attachment Names from SXI_MONITOR
This is important. Here we will find the Attachment names of the files which were picked up from the source. (these will be same as what we specified in File List in sender file adapter).
You can confirm the same from SXMB_MONI–> Open the Message –> Inbound Message –>Soap Body –> Manifest
Here in the XML you will be able to see the attachment Names.
If you study carefully the ApplicationAttachment (additional Files) names have come without any change (as what we specified in the File List – sender communication channel).
The main XML (Application) file Email.xml has been renamed to “MainDocument” as this is the main payload of the message.
Hence in this scenario, we will use MainDocument (to select “Email.xml” file), TextFile1 (to select “TextFile1.txt” file),TextFile2 (to select “TextFile2.txt” file ),TextFIle3 (to Select “TextFile3.txt” file) and TextFile4(to select “TextFile4.txt” file) from the payload to bring them to required format.
Receiver Mail Adapter Configuration:
Things to remember:
- Here make sure Message Protocol is “XIPAYLOAD” , use Mail Package and Keep Attachments are checked as we are using Mail packagingand we do have attachment with the payload.
Now comes the task of preserving the format and content of the text file:
What we are actually doing:
- Selecting each attachment payload one by one using payloadswapbean.
- Convert the selected attachment payload from step 1 as MainPayload (inline) using the module MessageTransformationBean and explicitly name the file with its appropriate name using the property “filename”.
- Perform step 1 to 2 for all the rest of the attachments in the payload as shown in the screenshot above.
- swap5 and trans5 are used to make the original payload “MainDocument” as the Main payload at the end, as this should be passed to “Mail” adapter module and it contains the Email address ,subject and content of the mail which is needed by the adapter (as we are using Mail packaging). – this step is important.
Well what if the attachment file names are dynamic and will have a time stamp or something to it.
1. In sender adapter user PayloadZip bean and zip all the files into one zip file.
2. use an UDF to read the zip file in Mapping if using Mail packaging.
3. perform the same operation in receiver mail adapter mentioned above to rename the zip file attachment.
In this way the zip file will contain multiple files within it unchanged.
comments and suggestions appriciated,