Are you working on a Power Automate flow to download/upload attachment from an email, but the attachment is corrupt?
Corrupt attachment from an email is one of the most common problems in Power Automate. It doesn’t matter if you need to download the files, upload them somewhere else, attach them to a task or another email. There’s a chance you saw error message as below. So how to avoid it?
Add base64ToBinary() expression
The expression base64ToBinary() should help. If you get an error there’s a chance that the file content is not properly encoded. Use the base64ToBinary() expression to convert the ‘Attachments Content’.
format:
base64ToBinary([Attachments Content])
note: [Attachments Content] is a dynamic content
when using in a flow, it could look like this:
base64ToBinary(items('Apply_to_each')?['contentBytes'])
Instead of using the ‘Attachments content’ directly.
Use the base64ToBinary expression.
I’ve never had this issue with files located in SharePoint or OneDrive, the only problematic source seems to be the email attachments. And even there I didn’t understand why it sometimes works and sometimes not. But using the expression above when processing email attachments seems to solve the corruption problem.
Add ‘Get attachment’ action
Another option to try if the expression doesn’t work is to add ‘Get attachment’ action before processing the file. Instead of using the attachments data from the trigger, include an extra step in your flow to load the attachment data.
This was a workaround I originally used when testing a flow. The problem was that the file was created properly when I actually sent an email, but when I tried to test-run the flow, the files were corrupt. But since then I found the expression above that seems to work much better than this.
thank you for this
When trying the base64ToBinary expression, the flow failed saying a field was null. However, I was successful using the ‘Get Attachment’ function.
same for me
Same for me.
same here
Thanks!!!!!
I’ve been struggling a lot with this
Thank you very much Tom!!!!! I was struggling a lot with this issue and the base64ToBinary() function solves everything.
Thank you SO MUCH!! I have been trying to solve this problem for hours with no success until I read this!!
Awesome work thanks so much for this I’ve looked at lots of other posts but this is simple and actually works
Hey I am trying to use the expression but my excel file still has its data all messed up and corrupted.
I had a problem with corrupted attachments when I switched from the Outlook to the SMTP Connector. I used the base64ToBinary() function, but I found that when I saved and reopened the flow, it changed automatically!
base64ToBinary(body(‘Apply_to_each’)) turns into body(‘Apply_to_each’)?[‘$content’] which works just fine. Maybe it helps someone.
You have saved my life with this trick!! thank you I am so grateful 🙂
Thanks for this suggestion. I spent almost 3-4 hrs on this and then after I got this blob. It worked for me.
Thank you!!!!!
Thanks! I have similar problem when sending OneDrive Excel file via Outlook.
The base64ToBinary does not work for me. But I find an Action similar to “Get Attachment” – “Get File Content by Path”. I used that to obtain the File Content.
Then in my Action “Send an email (V2)”, under “advanced options”, “Attachments-Content”, I entered “File Content” retrieved in Dynamic Content. Then vola! The attachment sent can be opened without problem 🙂
Hello Peter,
thank you for sharing the solution!
Thanks.
looked for this exact solution !
“Add ‘Get attachment’ action” works perfect for me.
Now progress to unzip the file inside 🙂
This worked for me! Took hours for me to find the issue but this is gold now! Thank you!
Excellent! base64ToBinary() did the trick. Thanks for posting this.
I am having issues based on my specific flow, which looks different from yours and I could really use some help. Can I email screenshots to get some assistance?
Hello Tina,
I’d recommend posting to the Power Automate forum if it’s an issue unrelated to the article: https://powerusers.microsoft.com/t5/Microsoft-Power-Automate/ct-p/MPACommunity
I spent way too many days on this issue (even leaving the automation completely for two months). Thank you for this amazing post!
I was facing the same issue. I resolved it by selecting “Yes” for includes Attachments in “When a New Email Arrives” trigger.
Thank you so much Syed!! That worked perfectly!! I’ve been beating myself up for 2 days over this.
Syed the select “Yes” made it work for me as well…thanks for posting.
Bulls Eye Man
I would like to comment on this.
For me, it took these two steps to make it function.
First make sure that “Yes” for includes Attachments in “When a New Email Arrives” trigger.
Second use Add ‘Get attachment’ action and then “create file”
This allowed it to work for me.
This worked for me, thanks a lot!
Thank you so much. Binary solution worked for me 🙂
Hm, I followed the steps but it is still corrupted. Flow works with no errors. Got the content from a file in sharepoint and on the email attachment added the 64tobinary function and specifying the column to $content as it otherwise is an object.
Hello Sara,
the solution is from files going from Outlook attachment to SharePoint, no the other way around, files from SharePoint should be fine as they are, no conversion needed.
That is what my flow does (save an attachment from outlook, if a file with the same exists in the sharepoint folder, it renames the file, then creates the file). I can create the file without a problem if I am not renaming it. Otherwise the result is a corrupted file. The 64tobinary is not working for me.
Hello Daiane,
if it works when creating but not when renaming then there must be problem somewhere in/after the renaming part, e.g. missing file extension, using wrong dynamic content in one of the actions, etc.
HI Daiane,
I know this is quite an old post but, I was also trying to rename a file while creating it in SharePoint. For some reason when used both methods together; 1) Creating a file using 64tobinary then overwriting the same file by 2) Creating a file using Content Bytes this seemed to solve my issue!
I spent some days troubleshooting this and every other solution I tried wasn’t feasible so I hope this helps!
Same here Sara. Still struggling to get this fixed.
I am doing the same thing, but instead of SharePoint I am uploading the file to a OneDrive folder. I found that while testing the automation, the file will still give that corrupted error message — but if you get someone to send you a file normally, it seems to work. For OneDrive the code is:
base64ToBinary(items(‘Create_New_File’)?[‘contentBytes’])
My process flow is:
1. When email arrives
2. Create new file (select attachments as output from previous steps)
3. Create file OneDrive (this is inside of the create new file step)
Specify the folder path in OneDrive, the file name (with extension as .xlsx) and put the above code inside the file content section.
I hope this helps anybody with this same issue.
Hello Ethan,
tnank you for sharing your solution.
Using the Base64ToBinary option worked like a charm for me. Thank you, I no longer need to pull out my hair…
I looked all over the internet today and this is the only thing that helped me. Thank you a million times over!!
I am trying to set up a flow to save a pdf attachment when it is sent to a shared mailbox. I have tried adding both the additional step mentioned above and the base64 code and I’m still having problems.
The file saves in the correct Sharepoint folder and the flow says it has run correctly, but I can’t open the attachment.
Any suggestions please?
Hello Kate,
I’d double check the dynamic contents you use after you get the file / file content and then it’d be mostly trial and error to try the various options one by one.
It’s absolutely horrible that this isn’t covered anywhere. The only article on powerusers that does not solve anything. I struggled with this for 3 days.
Infinite thanks.
This is great! Thank you for this.
I tried both methods across several issues and they each work in different situations.
Thank you so much!
Wow! I’ve been pulling my hair out trying to make this work. 100000000 thank yous!
Thank you, this proved really helpful 🙂
When I tried to use
base64ToBinary(items(‘Apply_to_each’)?[‘contentBytes’])
in the New Designer I received an error (invalid expression).
When I switched to the Old Designer I could enter ‘File Content’ as Dynamic Content and everything works fine. Looks like Microsoft has some work to do.
You just saved me hours of work, thank you!
Brilliant. The workaround with Get Attachments and using Content Bytes worked perfect! I had a lot of trouble with this. Thanks for posting.
This post was god send. This just saved me so much time as I was gonna re-design a complex flow to get around this problem.
Click : On new email -> show advanced options -> include attachments YES
https://blog.ciaops.com/2023/06/05/power-automate-email-arrive-action-file-size-only-4-bytes/