Using eMail Attachment Content in Power Automate Without Saving the File
Sometimes, the go-to solutions won't work for a particular story. When it comes to dealing with eMail attachments, most solutions involve saving the file someplace and then working with it. But what if you don't have that option?
Recently, Sara Lagerquist asked to get the contents of a text file attachment to use later in a flow, without having to save it to someplace like SharePoint or OneDrive. The text file would be some XML that could be extracted and perhaps used in another step.
For my trigger, I used "When a new email arrives (V3), but your story might be different. The important part is that you have some mechanism in place to get the email (with attachment) into PowerAutomate's awareness.
Note that I added a From: filter on this for testing so that I'm not looking at every eMail...only the ones I send to myself.
I could have also changed the flag at the bottom "Only with Attachments" so that I was only looking at emails that had files attached. No time for that now :)
Next, we need to get the attachment from the email. Since there can be more than one file attached to each eMail, Power Automate kicks off an Apply to Each loop automatically. Don't freak out.
I say that because I would get super-frustrated and confused when a loop kicked off. It seemed to make things more complicated. But if I think of an array like a grocery list, and then everything inside the loop is just doing something so I can check that item off of that list, that helps calm my nerves. Probably because I like food.
The next few steps can probably be combined into a single expression depending on your comfort, but I broke it into several steps to show there are multiple ways to get to the same result. The first part is to isolate the body of the email attachment. I did this by dropping the body dynamic value from the previous step into a Compose action.
This will return some JSON that has a contentBytes field that we'll want. Since I know I'm going to use the Parse JSON action next, I'll want the output of this Compose to use as sample data to build the schema for that parsing action. Go ahead and run your flow now, and then copy the output of this compose to your clipboard. Then go back to the edit mode.
If you haven't used the Parse JSON action, it's a great way to get at data but comes with some caveats. Take some time to read John Liu's A Thesis on the Parse JSON action in Microsoft Flow. Super helpful even after more than 3 years.
For the content, I used the output of the above Compose and then for Schema, I clicked on "Generate from sample" and pasted the output from the previous run. Power Automate did the rest. As mentioned above, one of the fields coming back is contentBytes...but to use it, we need to convert it to a string from the Base64 format it's in right now. I used another Compose to hold the expression to do that.
This will produce the XML in a string format, as seen by the run output.
From here, we can drop that XML someplace, or if it was text, you could use that for a Word template or something. It's totally up to you!
Hopefully, this helps you to 'do the thing'.
Get in, Make a mess, learn some things along the way. Try. Fail. Learn. Repeat.