Converting Email Files in a SharePoint Library to PDF (Including Attachments)
In one of our recent projects, we faced a challenging requirement: converting around 17,000 email files stored in a SharePoint document library into PDFs.
The complexity was not only the volume, but also the structure. The files were distributed across over 9,000 nested folders.
The business requirement was very clear:
-
Convert each email file (.msg) to PDF
-
Include all attachments in the PDF
Ensure images embedded in the email body and attached files remain visible in the converted PDF
-
Save the converted PDF in the same folder path
-
Keep the same file name
A few important notes upfront:
-
The Encodian connector is Premium and paid
-
Email conversion consumes 1 token per email
-
Each attachment also consumes 1 token
-
For this project, I purchased 25,000 tokens, which was sufficient for the volume and attachments involved
The entire solution was implemented in about 3 hours, and the flow successfully converted all files without a single failure. It was a very solid and predictable outcome.
Solution Overview
The solution is implemented using a Power Automate cloud flow that:
-
Reads email files from SharePoint (including nested folders)
-
Converts them to PDF using Encodian
-
Writes the PDF back to the same folder path
Step-by-Step Implementation
1. Create the Flow
Start by creating a new cloud flow.
-
Use a Manual trigger.
2. Get Email Files from SharePoint
Add Get files (properties only).
Configure it as follows:
-
Site Address: Your SharePoint site
-
Library Name: The document library containing the emails
-
Include Nested Items: Yes
If your emails are stored in nested folders, use a Filter Query to:
-
Target specific folder paths
-
Retrieve only .msg files
Example filter query:
(startswith(FileDirRef, '/sites/My_SharePoint_Site/Shared Documents/Emails/Subfolder1/2020/Sub')
or
startswith(FileDirRef, '/sites/My_SharePoint_Site/Shared Documents/Emails/Subfolder1/2020/Sub1'))
and substringof('.msg', FileLeafRef)
This ensures the flow only processes email files from the intended folders Sub & Sub1.
3. Loop Through the Files
Add an Apply to each action.
-
Use the value output from Get files (properties only)
This loop will handle each email file individually.
4. Get File Content
Inside the loop, add Get file content.
-
Site Address: Same SharePoint site
-
File Identifier: Identifier from Get files (properties only)
This action retrieves the binary content of the email file.
5. Get File Metadata
Add Get file metadata (still inside the loop).
-
Site Address: Same SharePoint site
-
File Identifier: Identifier from Get files (properties only)
This is required to retrieve the original folder path, which allows us to save the PDF in the same location.
Creating the Encodian Connection (First Time Only)
If this is your first time using Encodian:
Provide a connection name
Paste your API Key from the Encodian portal
Select the nearest region for best performance
Configure the Convert Email Action
-
File Name: File name with extension (from Get files)
-
File Content: Output of Get file content
-
Merge Attachments: Yes
This ensures the email body and all attachments are merged into a single PDF.
7. Create the PDF File in SharePoint
Finally, add Create file.
- Site Address: Same SharePoint site
-
Folder Path: Folder path from Get file metadata
-
File Name: Output file name from Encodian (PDF)
-
File Content: Output file content from Encodian
This saves the PDF back into the same folder as the original email.
Subfolder after the flow run completed
Recommendations and Best Practices
-
Name your actions clearly
Use meaningful action names and add notes where possible. This makes maintenance much easier for others. -
Process in batches
If you have multiple top-level folders, consider running the flow separately for each folder to reduce risk and simplify troubleshooting. -
Monitor token consumption
Regularly check usage in the Encodian portal to ensure you have enough tokens, especially when attachments are involved.
Comments
Post a Comment