SAP PI Mail attachment to File
Overview:
- Business scenario, where it is required to extract mail attachments and store it to some SAP directory.
- Here, one common mail-id is been used by a group of users to send mail-attachments from their respective ends.
- There may be two cases:
- One attachment per unread Mail
- Multiple attachment per unread Mail
SAP PI Scenario details:
- One SAP-PI’s “Mail to File Inbound Asynchronous” interface will be created to achieve above requirement.
- Using Mail Sender Adapter, interface will read all mail with their attachments.
- Using File Receiver Adapter, interface will download attachments to the directory.
- Here only Integration Directory’s configurational objects are required.
- As no message transformation is in scope, so Integration Repository objects are not required.
Pre-requisites:
- SAP-PI should have SMTP mail server accessibility
- SAP-PI should have access to SAP ECC application server’s directory
Steps to develop Interface in SAP-PI:
In SAP-PI’s Integration Directory, we need to follow below steps to develop “Configuration Scenario” of “Mail to File Inbound Asynchronous” interface:
- Note: Object naming conventions is for example purpose, it can be as per client’s conventions
[I]. Business Components: Create two business components, one for sender and other for receiver
- Sender Business Components
- This will work on behalf on sender-side while reading Mails
- Name it as SENDER_BS_COMP
- Receiver Business Components
- This will work on behalf on receiver-side while storing attachments to directory Mails
- Name it as RECEIVER_BS_COMP
[II]. Communication Channels:
Case-1: One attachment per unread Mail : (Sender/Receiver Comm. Channel Cofig.)
Here, with help of below config, one attachment can be easily extracted from unread mail to sap’ app directory.
Sender Mail Adapter Communication Channel:
Select ‘Mail Adapter’ to read mails from mail server and configure below details in channel “SENDER_MAIL_CHNL”:
- We use ‘Transport Protocol’ IMAP4 (Internet Message Access Protocol) to retrieve e-mails from a folder of an e-mail server.
- It reads mail from INBOX folder and post read, mark its status to ‘read’.
- Using ‘Message Protocol’ XIPAYLOAD, The content of the e-mail is put in the payload of the XI/PI message.
- Enable ‘Keep Attachments’ functionality while reading mails.
- Enable ‘Set Adapter-Specific Message Attributes’ functionality while reading mails, this is required to get header details of message. From header details we extract attachment names.
- As we need to extract all attachments of one mail, this can be achieved with help of below Module Beans:
- Module Bean ‘PayloadSwapBean’
- With help of this bean, we swap main payload with 1st attachment
- And with help of File-Receiver channel we will download it to sap app directory
- Module Bean ‘MultipartHeaderBean’
- With help of this bean, we add the following values in dynamic configuration:
- “Attributes of other payloads that are appended to the XI message as an additional attachment”
Receiver File Adapter Communication Channel:
Here, we use ‘File Adapter’ to download attachment to folder and configure below details in channel “RECEIVER_FILE_CHNL”:
- 1st attachment is already ready to get downloaded, which is swapped with main-payload
- Using ‘DynamicConfigurationBean’, get 1st attachment’s original name
- Please note: Here, with this config we can not store multiple attachments from one mail to folder
- Here ‘File Name Scheme’ is referring to variable which is defined in ‘Variable Substitution’
- Define a variable ‘fname’ using Variable Substitution, which stores message interface name.
- Use Module Bean ‘DynamicConfigurationBean’, to get 1st attachment’s original name using logic “Mail Part[1].Content-Description” and set it to message interface name
- Test Screens:
- Unread mail in Mail-Inbox with one attachment
- Mail-Content
- Post Mail read by PI-Channel (Mail-Sender-Adapter), below is the channel log
- Sxmb_Moni log:
- And post attachment extraction by interface, file present in sap app directory
Case-2: Multiple attachment per unread Mail : (Sender/Receiver Channel Cofig.)
Here, we use a custom java adapter module at File-Receiver-Channel side.
- For this case, we create a custom java adapter module, which will have below functionalities:
- Read all attachments with their original names
- and store them to sap directory
- This module will require one user input as ‘SapDirPath’ which is sap directory path location, where attachments will be saved
- This module will be used as Module parameters in ‘Receiver File Adapter communication Channel’
- To create this custom java adapter module, please follow link as :
- Sender/Receiver channel configurations are as below:
Sender Mail Adapter Communication Channel:
Receiver File Adapter Communication Channel:
- Here, using File Receiver, one Temp file (Test.dat) will be generated in directory, which will keep on over-writing every time. This file is of no business use, it is used only to complete channel config.
- Now one custom adapter module will be used, which will have below functionalities:
- Read all incoming attachments from inbound message
- Store it to folder path with original attachment names
- This module requires one input which is sap directory path location ‘SapDirPath’
- Below Module configurations required in File-Receiver-Channel for custom adapter module access, that require input as FolderPath
- And below is the test screen which depicts how multiple attachments from one unread mail gets extracted and store to sap directory
- For Testing example, below case considered:
- Incoming Mail to (mail-id), which need to be read by interface:
- Below is PI’s SXMB_MONI log, in which Mail adapter has forwarded all attachments to receiver channel side
- Below is log of File-Receiver-Channel, in which Custom adapter module’s log can be seen
- and at last, screen of sap directory where files are been stored by custom module
[III]. Receiver Determination
- A receiver determinationis required to specify the receiver or receivers of the message, as well as conditions for the forwarding of the message to the receiver or receivers
- Here we create it with below parameters:
Sender | ||
Communication Component | SENDER_BS_COMP | |
Interface | INT_MailToFile | |
Namespace | NM_MailToFile | |
Receiver | ||
Communication Component | * | |
Configured Receiver | RECEIVER_BS_COMP |
[IV]. Interface Determination
- An interface determination is required to specify the inbound interface of a specified receiver that a message is to be sent to. We can also define mappings here, if required.
- Here we create it with below parameters:
Sender | ||
Communication Component | SENDER_BS_COMP | |
Interface | INT_MailToFile | |
Namespace | NM_MailToFile | |
Receiver | ||
Communication Component | RECEIVER_BS_COMP | |
Receiver Interfaces | ||
Name | INT_MailToFile | |
Namespace | NM_MailToFile |
[V]. Sender Agreement
- A Sender Agreement is required to configure the inbound processingof a message.
- Here we create it with below parameters details:
Sender | ||
Communication Component | SENDER_BS_COMP | |
Interface | INT_MailToFile | |
Namespace | NM_MailToFile | |
Receiver | ||
Communication Component | ||
Sender Communication Channel | SENDER_MAIL_CHNL |
[VI]. Receiver Agreement
- A Receiver Agreement is required to configure the outbound processingof a message.
- Here we create it with below parameters details:
Sender | ||
Communication Component | SENDER_BS_COMP | |
Interface | INT_MailToFile | |
Namespace | NM_MailToFile | |
Receiver | ||
Communication Component | ||
Receiver Communication Channel | RECEIVER_FILE_CHNL |
Hi Dilip!
Wouldn't you please provide more detailed explanation of how multiple attachements within one message are processed. I guess it's not clear enough. For now I could see the logic to send the first attachment only. Or did I miss anything?
Regards, Evgeniy.
Dear Evgeniy Kolmakov
With help of above Interface,
About, how multiple attachments get downloaded to folders, below are the details:
At Sender side:
At Receiver Side:
I hope, above clarifies things.
Thanks & Regards,
Dilip.
Hi Dilip!
I feel a little bit confused. Does the file adapter at receiver's side handle attachments? How are attachments saved?
And if I understand the logic right, mail body is going to be saved too?
Regards, Evgeniy.
Dear Evgeniy Kolmakov
Receiver File adapter will downloads attachments to folder directory.
Mail body is not going to be saved, because we are replacing mail body with 1st attachment,
and rest attachments will be handled using "DynamicConfig' of channel.
I have updated the blog for more clarifications, hope that will be enough for understanding the scenario working.
Thanks & Regards,
Dilip.
Dear Dilip,
This is more helpful for reading the folders from mail using IMAP4 and POP3.
But my requirement is using the Web Service read the attachments from mail in folder and keep it in directory and that mail to move to Archive folder in mail box.. Could you please help me on this.
Thanks & Regards,
Nagesh
Dear Nagesh,
Sorry for very late interaction, till now, you may have already addressed the requirement. if so, Please let me know, how you have handled that.
In my opinion, if you have webservice, you must be aware of all related respective message protocols. It should be treated/configured as webservice consumption scenario for data exchange.
Thanks & Regards,
Dilip
Dear Dilip,
Sorry for very late interaction. Still development is not completed. I am working on that. I unable to get how to start the development using web services.
Regards,
NAgesh.
Hi Nagesh,
Sorry for very late interaction. You may have already addressed the requirement of webService to Mail scenario.
However, In SAP-PI/PO, you can use SOAP Sender adapter to host webservice and on receiver use MAIL adapter. Configure the scenario as per requirement, like if you want to read the mails based on any request coming from webservice and make use of JavaAdapterModule in case of multiple attachments download from mail into directory.
Thanks & Regards,
Dilip
Thanks Dilip. Let me try for this solution.
Hi Dilip,
I followed above steps, I Kept attachments in directory using mail sender adaptor and receiver adapter.
There is business requirement I want to delete mail from mail box folder and move that mail to Archive folder in mail box. How to handle this scenario.
Thanks & Regards,
Nagesh.
Hi Nagesh,
Yet, I havn't gone through any scenario with Deletion and archive of mails.
Will surly look and update you soon.
Thanks & Regards,
Dilip
Hi Dilip,
Thank you very much your response.
Hi Kolmakkov,
Thanks for you reply. Please find screen shots I am using sender mail adaptor and file receiver adaptor.
Sender Mail Adapter
I Used IMAP4 adapter
I checked option Delete Message Once Read
Option * Duplicate Detection Use Augmented MessageID
It will work for down load all attachments in all mails
cuserschepnagdesktopnew-foldersender-adaptor1.png cuserschepnagdesktopnew-foldersender-adaptor3.png
Once I used below PayloadSwapBean , Mail attachment going to directly with name successfully.
Receiver file adaptor
cuserschepnagdesktopnew-folderreceiver-adptor1.png cuserschepnagdesktopnew-folderreciever-adopter2.png cuserschepnagdesktopnew-folderreciever-adopter3.png cuserschepnagdesktopnew-folderreciever-adopter4.png
Now My requirement is I want delete mail and move it to Archive folder.
Below screen shot in folder having 2 mails with attachment down loaded attchamnets to directory. But the mail is not deleting after downloaded attachment. And also I want to move this mails to Archive folder.
cuserschepnagdesktopnew-foldermail-folder.png
Thanks Dilip.
Dear Nagesh,
Are you able to delete and move mails to 'Archive folder' ? If yes, plz let us know.
Else you can try like below and let me know if it works for you:
Thanks & Regards,
Dilip
Dear Dilip,
Thanks for reply.
Yet I did not get any luck for my requirement. I tried above option I able to down load files to archive folder directory from mail . But I unable to move mail to archive folder in mailbox.
Regards,
Nagesh,
Dear Dilip,
Mean while we are using VB script for Archive mail.
Regards,
Nagesh.
Dear Dilip,
Now the interface is working for the check box delete messages once read.
Thanks & Regards,
Nagesh
Dear Dilip,
Interface is working. Reading the mail and attachments downloaded to the folder, Mail moved to Archive folder in mail and Mail deleted from Inbox.
We are adding PI message ID to have unique file name and to avoid overwriting.
File extension is now showing Test.xlsx98b25cd8-8416-11ec-c60c-000000e6deee .
Should the extension of “5508407c-840f-11ec-c0bc-000000e6deee” not be before “.xlsx”?
Thanks & Regards,
NAgesh
Hi Dilip!
So let me ask: have you ever tried to implement the scenario you describe in your blog?
Regards, Evgeniy.
Dear Evgeniy Kolmakov
This is a working scenario, can download multiple attachments of a mail to directory.
As per above log screen, when Mail adapter reads a mail, it forwards all its attribute to receiver side.
Now using ‘DynamicConfig‘ and ‘variable substitution’ parameters, interface can download attachments to folders.
Thanks & Regards,
Dilip.
Hi Dilip!
Thanks for your answer. And at receiver side you get all of your attachments or just that one which has been moved to main payload?
Regards, Evgeniy.
Dear Evgeniy Kolmakov
At receiver side, in mapped folder, we get all attachments with their original naming conventions and file extensions (pdf, excel, txt...etc.)
And thank you for your time and detailed observations, that made me understand, how one blog should be more informative for clear picture of the Idea.
Thanks & Regards,
Dilip.
Agree with Eve , reading multiple attachment from sender mail was never an issue . But the main concern was to drop all the attachments via one single message. Additional in your File channel you are reading Attachment name from Part[1]Content-Description which will always have the first attachment name only and channel will always drop only the first attachment as you have swapped with first attachment only .Also i believe you have variable substitution also as you needs to assign the message.interface for filename attribute of file channel.
I am really not a blogger but my suggestion is to always put a screenshot of your config as picture talks more than 1000 words.
Br,
Manoj
Hi Manoj!
It was my first question, since it looks like typical scenario for storing just the first and the only attachment.
Regards, Evgeniy.
Dear Manoj/Eve,
Thanks for the suggestion.
I will re-edit the blog with config-screen and Test-screen with all its pros/cons.
Thanks & Regards,
Dilip.
Dear Eve/Manoj,
You both are right, about below things:
About above blog:
I will re-update blog with new details shortly.
Thanks & Regards,
Dilip.
Yes Dilip you are right with SFTP you can handle it as there is option of "Store Attachment" along with additional Parameter retain.attachment name . But in this case you need to drop only all the attachments so you need to find a way to suppress the main payload.
I never tried, but if we want to store the multiple attachments with main payload, we can add all main payload and attachments in zip file. send that zip file as main payload and create that zip file as output file in the folder.
later unzip using either module or command line.
Hi,
I’ve also tried to implement this with your description, but get always the error:
“write property message.interface from key (http://sap.com/xi/XI/System/Mail)Part[1].Content-Description with value null; “
Do you know the reason why the message doesn’t create the tag “Content-description?”
Hi Ahmet,
Please re-check Sender Mail channel's config (like ASMA, keep attachments, ModuleBean)
and below receiver File channel's Module config:
Thanks & Regards,
Dilip
Hi ,
my configuratoin are like this:
in NWDS
Hi Ahmet,
Your config too seems to be fine, but I am not clear what causing issue at your side.
For your better insight, I have updated the blog with test screen too, please go through once again, I hope you may get something.
Thanks & Regards,
Dilip
Hi,
here my logs:
And here are message informations listed:
Here is my mail Test:
As you can see here, the part "description isn't listed. But the clu is. When I download the file and send this file from my mail account to the folder, than it will be correctly picked up and all informations comes through. I've checked also the differences. The origin message is send "text/plain" content type and charset in utf-8. My mail was send with "text/html charset in ISO-8859-1
Hi Ahmet!
Content-Description headers are optional and are often used to add descriptive text to non-textual body parts.
I guess that you should check both Content-Disposition and Content-Description headers to get file name.
Regards, Evgeniy.
Hi Evgeniy,
but both are empty in case of origin mail procession.
Or the name isn't correctly set in disposition. There you find only "attachment", not the name of the file.
Check my screens.
Regards,
Ahmet
Hi Ahmet,
As in your message log too,
So case may be, either mail is not having any attachment or either Mail-Adapter is not picking up mail attachment or either swapping is not taking place properly.
Please re-verify your Sender Mail config, and you can cross check with below my side logs:
Thanks & Regards
Dilip
Hi Dilip,
There is only one difference between my communication variant and yours.
I have a FTP Adapter on receiver side and you're using nfs?
Is this the problem? can the current FTP adapter not proceed all send message information's?
Regards,
Ahmet
Hi,
I’ll check and let you know soon if there is any impact due to FTP.
Thanks & Regards
Dilip
I’ve also tried your second example regarding Custom/MailtoFile
and there I get the error message below:
In this case I#ve also used the FTP-Adapter
Hi Ahmet,
If you check log, JAM is getting a ".csv" file, but while dropping it to the given directory path it raises exception.
Check if PI have access to directory path or if such directory exists.
And please note, that JAM example is properly working with File-NFS, but not tested with FTP
Regards,
Dilip
Hi,
yes you're right that's the reason for JAM in my case. I use FTP-Adapter on every communication lines as I've many scenarios.
What about option 1 and FTP? Could you check this?
Regards,
Ahmet
Hi Ahmet,
option 1 and FTP, not yet managed to test, but will try surly and let you know soon...
thanks & regards,
Dilip
Hey Dilip,
option 2 worked fine and I could import the file in NFS mode correct name in local folder!
But, I've seen it generates minimum two file. One of these is the dummy file, which is named in "Process" menu. In you example you had named it "test.dat".
Thanks alot for your help!
Regards,
Ahmet
Hey Ahmet,
Nice to hear that your issue gets resolved,
but in option 2, there is no need of var substitution, becasue all multiple attachments gets strored in sap's directory using JavaAdapterModule (JAM) functionality itself.
JAM requires dirPath and for..loop runs for each file to store them in this path
Thanks & Regards,
Dilip
Yes, your're right that was my temporarily fault. Therefore I've changed the message. 🙂
But the second file will be generated as I wrote.
Dear Ahmet,
Just for more clarity, please find following details w.r.t. “JavaAdapterModule (JAM) example for multiple Mail attachment download” :
Thanks & Regards,
Dilip,
Great blog!.
Hoping you can reply.
By any chance is there any "how to" to setup some java mapping in a Mail Sender to IDOC that can read per the attachment included?
For instance company@test.com shares email as a way for a partner to send invoice emails as PDF attached, if vendor is sending a PDF+XML this goes to a Provider government Party.
But if the vendor only sends a PDF, then this should be handled by an old integration.
Is there a way to set some sort of module + java mapping code that can do this?.
Hi Alvaro,
Same can be done via CustomAdapterModule or JavaMap having with below functionality:
Thanks & Regards
Dilip
Hi Dilip
Great blog!.
Hoping you can reply.
I am trying to connect with gmail using imap4 but not getting any response. I have used the below URL.
imaps://gmail.com/Inbox
I have already open port 993 for imaps protocol. Should i need to anything for SSL?
Please suggest.
Regards,
Sudipto
Hi Sudipta,
You may require to do below things:
Thanks & Regards
Dilip
Hi Dilip,
Thank you so much for the wonderful blog.
I hope you will able to help me on my requirement and its almost similar to this blog.
We have a requirement where we will get one attachment with multiple sheets coming through mail attachment and needs to detach the attachment and post to file location. Would you be able to help me on how to handle multiple sheets in one .xls file please?
Thanks,
Lova
Dear Lovaraju Gudivada,
With respect to your queries, please find below comments
1st Query which is:
We have a requirement where we will get one attachment with multiple sheets coming through mail attachment and needs to detach the attachment and post to file location.
Comments:
one attachment with multiple sheets means one excel file having multiple sheets.
Extract it and place into some folder location of SAP-ECC-App directory.
If above understaning is correct w.r.t. your query, then This blog has answer for same, please refer. section ” Case-1: One attachment per unread Mail : ”
2nd query which is:
Would you be able to help me on how to handle multiple sheets in one .xls file please?
Comments:
Is it like, you want to split one excel file’s multiple sheets into separate excel file and store it into directory ?
If so, then you have to use Java Map for spiting.
Thanks & regards,
Dilip
Hi Dilip,
Thank you so much Dilip. I have actually followed the same above steps before came to you but due to a small mistake in the config it didn't worked for me. Now yes i have resolved it and it is perfectly working for me.
Much appreciated.
Thanks,
Lova
Hi Dilip,
I have implemented this scenario for our requirement but unfortunately i am getting the same issue what Ahmet faced for the option 1.
This is strange such a way that it was happening for 1 or 2 first files every day when we are posting around 30 files. But as Ahmet mentioned if i download the content from monitoring for the failed one and save it on our desktop and send it to mail then this time successfully processing. I couldn’t be able to find any difference whats wrong with the file because it was success when we post second time.
Kindly let me know if you have solution in your notice.
Ta,Lova
Hi Lovaraju,
I haven’t faced any such surprising case, I doubt if I can help u. Will surly let you know, if any related content comes in my notice. Just a suggestion, please re-check channel configurations once again.
And about "how single attachment per mail scenarios works ? " its like below:
Thanks n regards,
Dilip
Fine Dilip I will have a look but i have exactly configured the same as you did.
I am wondering if subject and attachment name and body has a same name is causing an issue hence have asked sender to do some change to subject and body will see how it goes from the next run.
will keep you update.
Ta,Lova
In this scenario vendors are not accessing SAP directly, they are only sending details to the desired email address as an attachment, SAP PI is accessing the data from the email and processing further. Can anyone tell me in this scenario SAP license will impact directly or not as vendors will be involved in the process however they are not using SAP directly or indirectly. Please advise at earliest.
Hi Sanjoy,
You check with your Basis/Admin guys once.
In my view, there is no SAP license imapct w.r.t. Vendors who sends mails to the desired common email address.
It's SAPPI, who reads mails and drops attachments to SAPECC server directory, and this happens through background SAPPI user-ids.
Regards,
Dilip