Get content of a file attached in an email’s .eml attachment (nested attachments)

Quite recently we were working with the Accounts Payable team to automatically grab invoices received from suppliers and save them in a SharePoint site, we were using the regular “For each attachment”, “Attachment content” and then “Create file” actions in Power Automate for most vendors, until we found one vendor who did not attached their PDF files straight in the email, but inside another email – i.e., nested attachments

The email looked as below: 

The automation must access the PDF file that’s inside the .eml file that is at the same time attached to the automated email the supplier sends. In this blog, we will show you how to achieve this using Power Automate!

Table of Contents

Step 1. Set up your trigger

Look for the “When a new email arrives (V3)” trigger for the “Office 365 Outlook” connector. 

Set up Trigger for Office 365 Outlook Connector

Make sure to select “Yes” in the “Include attachments” field

Step 2. Search for “Send an HTTP request”

Add a new step and search for “HTTP” in the search bar until you find the “Send an HTTP request” action for the Office 365 Outlook connector

Find the “Send an HTTP Request” Action

Step 3. Set up HTTP request URI

Once you select this action, you will be prompted with 4 fields.  

In the URI field, write the following:

				
					https://graph.microsoft.com/v1.0/me/mailfolders/inbox/messages/{message-id-from-trigger}/attachments/{attachment-id-from-trigger}/$value 
				
			
Set up HTTP request URI

{message-id-from-trigger} -> select Message Id from dynamic content. 

{attachment-id-from-trigger} -> select Attachments Attachment Id from dynamic content. Once you select this, your HTTP request action will be wrapped around an “Apply to each action”. 

This is how this should look:

Look of Send an HTTP Request

Step 4. Rename steps

It’s best practice to rename your Power Automate actions, so we will rename the “Send and HTTP request” action to “Get EML content” and the “Apply to each” to “For each attachment”,

Rename Your Power Automate Actions

Step 5. Add a Compose action

  1. Right after the “Get EML content” action, add a Componse action and write the next formula: 

				
					@{string(outputs('Get_EML_content')?['body'])}
				
			

This is how your workflow should look so far:

Add a Compose Action

Step 6. Run your flow to see the outputs of the Compose action

Send an email with the .eml attachment to test the workflow so far, this will help us understand the next steps. 

Once the automation runs, inspect the results of the Compose action. You will notice this text contains all the content of the .eml attachment: body content and PDF attachment. 

Outputs of the Compose Action

You’ll see the PDF content right after the text “MIME-Version” (you can perform a search to find this specific text)

Search for the PDF content

Right below the “MIME-version” text, we will find the content of the PDF file we’re interested in, right between the word “base64” and the characters “–_”. 

Find the Content of the PDF File
Content of the PDF file

This PDF content is in base 64 format and contains some characters we need to get rid of. So, we need to apply some formulas to extract the text we’re interested in.  

Step 7. Write a PowerFx formula

1. Extract the text after the MIME-Version keyword
				
					last(split(string(body('Get_EML_content')),'MIME-Version:')) 
				
			
2. Remove \r\n
				
					replace(…,decodeUriComponent('%0D%0A'),'') 
				
			
3. Get the text after “base64”
				
					last(split(…,'base64')) 
				
			
4. Get the text before the last “–_”
				
					first(split(…,'base64')),'--_')) 
				
			
5. Convert the resulting text in binary format so we can save this in SharePoint as a PDF file
				
					first(split(…,'base64')),'--_')) 
				
			
Final formula should look as follows:
				
					@{base64ToBinary(first(split(last(split(replace(string(last(split(string(body('Get_EML_content')),'MIME-Version:'))),decodeUriComponent('%0D%0A'),''),'base64')),'--_')))} 
				
			

Insert this formula in a Compose action. Your workflow should look as follows:

Final look of Formula in a Compose Action

Step 8. Add the “Create file” action

Right after the Compose action, add a SharePoint “Create file” action.  

  • Select SharePoint site 
  • Select/Indicate folder 
  • File name: Assign a filename (in our case it will be the same as email subject) 
  • File content, select the result of the Compose action 
Add a SharePoint “Create file” Action

Step 9. Test your workflow

We’re ready to test our workflow and see magic happen! Just send an email with an .eml attachment and wait for the automation to run.

Send an Email with an .eml Attachment

 Invoice successfully saved into SharePoint:

Invoice successfully saved into SharePoint

Step 10. Optional: add a conditional to process .eml files

In case you might receive other emails that are not nested .eml files, it might be a good idea to add a conditional and check if the attachment has the .eml extension we’re interested in. 

Add a conditional in .eml Files

And you’re all set! You can use this solution with Power Automate to extract documents attached in .eml files, which are at the same time attached to another email!

Got questions? Contact us here or at contact@powergi.net

Watch a step-by-step walktrhough video