In this post let us learn how we can export email attachment(s) to Onedrive using Power Automate. In my previous post Power Automate – Export email to Onedrive | Knowledge Share (spknowledge.com) we learned how to export email to Onedrive but the downside is the attachments are not exported and the .eml file in Onedrive shows the attachment name and not the actual file. Now we are gonna export the attachments into a separate folder.
Let’s start building Power Automate Flow
When a new email arrives (V3) – Auto Trigger
Let us start with the auto trigger which triggers whenever a new email arrives at the configured user’s inbox. Here we have configured the trigger actions with some more options explained below. We can also use other options like export email only with attachments, emails having particular subject etc.
- Folder: Inbox
- To: User email addresses
- Only with Attachments: Yes
Note: Since we have selected the option Only with Attachments as Yes, the flow will be triggered only if the email received has an attachment(s).
Apply to each – Looping the attachment(s)
Next action is use the Apply to each action to loop through all the attachments. The input to this action should be the Attachments output from the trigger.
The other actions mentioned below should be inside the Apply to each action which means for each individual attachment, we have to do the following action.
Get Attachment (V2) – Get the current attachment in the loop
The first action inside the Apply to each action should be the Get Attachment to get the current attachment in the loop. The input params to the action is as follows.
- Message Id: Message Id from the trigger action ( triggerOutputs()?[‘body/id’] )
- Attachment Id: The current attachment id in the loop ( items(‘Apply_to_each’)?[‘Id’] )
The above action will return the attachment as binary.
Create file – Create the attachment in Onedrive
Next action is to create the file with the content. The input to the flow is as follows
- Folder: Specify the folder path to the store the attachments
- File Name: You can use the same attachment name ( outputs(‘Get_Attachment_(V2)’)?[‘body/name’] )
- File Content: Pass the binary contents ( outputs(‘Get_Attachment_(V2)’)?[‘body/contentBytes’] )
Enhancements or Readers Tasks
Below are some of the additional actions that can be added to the above flow to make it more functional.
- Create a new folder for each email with the email subject name as the folder name
- Make sure that you check whether the folder exists or not before creating the folder.
- Both the email and the attachments has to be stored inside the folder created above.
- If you receive different kinds of attachments in a single email, then separate the attachments in different folder based on the type or category of the attachment.
Conculssion & What’s next?
We have come to the end of the post. The flow that we learned above is very simple and the functionality we implemented is also very simple by just getting the email attachments to the root folder in Onedrive. Please try to complete the above tasks to get exprienced in Power Automate.
Please leave your comments and also let me know if you have any issues or any scenario that you want me to implement using Power Automate.
I hope you enjoy reading the post, see you soon in another post.
Cheers. Happy Flowing…