This article will explain how to use the receiver mail adapter to send a payload as an attachment in SAP PI.
There could be several scenarios where we need to use mail with attachments in sap pi. Some of the cases are as follows:
- Sending files over email scenario.
- As an alert – If data in the file is missing send the file to the concerned team for correction and re-trigger.
Let’s consider the alerting example.
Business scenario: If an incoming XML file is missing data, PI must route that file as an attachment and send an email to the appropriate teams for correction.
This can be accomplished through Java mapping, which I believe is a cleaner approach, but for the purposes of this blog, I will use UDFs because not all PI consultants are Java savvy.
The focus of this blog will be on the mail adapter and how to send a payload as an attachment. For more on the mail adapter scenario check the blog SOAP To Mail Scenario in SAP CPI
ESR configuration
Import mail package XSD
Map Subject, From, and To as per your requirement.
The Content_type should be set as “multipart/mixed” and the boundary value should also be specified.
UDF will be used for Content field mapping, with the incoming file as input.
UDF Snippet
The code below contains a lot of hardcoding, such as FileName and email body. This is only for explanation, and these should be set as needed rather than hardcoded in UDF.
public String generateContentWithAttachment(String XMLPayload, Container container) throws StreamTransformationException{
String strFileName = "AirlineDetails_Error.xml";// this can be set dynamic if required
String fileContent = XMLPayload; // This is input to UDF complete XML payload
String emailBody = "Hi Team,\n\nThe attached file is missing data.\nPlease check and re-trigger.\n\nThanks,\nPI team"
String output = new String("");
output ="----AaZz"+"\r\nContent-Type: text/plain; charset=UTF-8" + "\r\n" +
"Content-Disposition: inline" + "\r\n" + "\r\n" +
emailBody + "\r\n" +"----AaZz" + "\r\n" +
"Content-Type: application/xml; name=" + strFileName + "\r\n" +
"Content-Disposition: attachment; filename=" + strFileName + "\r\n" +
"\r\n" + fileContent + "\r\n";
return output;
Configure the Source field as shown below to send an incoming payload as input to the UDF.
Configure the mail adapter as below
All set, test the scenario, and an email with the payload attached will be sent.
For more on Receiver Mail adapter refer – Configuring the Receiver Mail Adapter