Sending a PDF file as an email attachment with Power Automate corrupts attachment

Copper Contributor

I have followed this great tutorial for creating and sending digital certificates to event participants (https://techcommunity.microsoft.com/t5/educator-developer-blog/send-digital-certificate-to-event-par...), and everything worked except for the last step which is sending the email with the certificate attached. I am able to receive an email with the pdf attached however, the file is corrupted and I'm unable to open it. When I check the pdf file in my share point it is not corrupted so the issue seems to be in the step of sending the email.

I tried using base64ToBinary(base64(body('Convert_file'))), when I added this I was able to open the pdf attachment but the pdf was empty and the content was erased. Please let me know if there is any way to overcome this issue. 

16 Replies
The issue was resolved on its own by using the old flow editor instead of the new designer.

@Bayan000 I switched to the classic editor, but it didn't work for me 😞

@Bayan000 I am having this exact issue. Did you find a solution?

@punkymatta The issue is that in the new designer, when using send email V2, it does not grab file name and file content in the same manner as the old one.  If you delete or modify your attachment section using the old designer, you will see that your options on getting the data from the one drive connector are different and once applied, your code should work.

@Bayan000 I can confirm switching back to old designer fixes this issue (as of 12/17/2023).

This should work like a charm

 

base64ToBinary(outputs('GET_FILE_CONTENT')?['body']?['$content'])
 



@Bayan000 

This was exactly what I needed to fix my problem! Thanks for sharing this.
I found the solution here - using the old designer! This bug was driving me nuts. I knew the input was wrong - but as you cannot change the code i could not fix it.

@Bayan000 I am still having this issue can you share your flow?

@daisysx1987 you will add it to your 'Compose' portion of your automation.  The bigger thing you'll need to make sure (unless they've fixed this bug, haven't had any issues as of late) is to make sure you are not using the new designer mode as for some reason it populates item's slightly different and corrupts the file once created.

Hope this helps

 

Were you referring to adding this code to the 'Compose' portion. I tried to find that section of the automation in the 'peek code' settings in the Send V2 email but didn't see where 'Compose' was located. Can you elaborate please?

base64ToBinary(outputs('GET_FILE_CONTENT')?['body']?['$content'])

@Bayan000 , Hi. I am using my organization's web based version of Power Automate which does not have an option for reverting to the old designer. Is there a fix for those of us stuck in the new designer? Thank you very much.

@amytucker714 

 

I've seen this kind of issue before with writing JPG files to Blob storage, there's something funky with the new editor that it mungs up parsing binary/base64 conversion unless the code is edited. I'm not sure if you can do this in Power Automate, but in logic apps in the code designer you can find the email attachment field and remove the outermost curly braces from the contentData string:

 

                        "attachment": [
                            {
                                "contentData""@{body('Convert_Word_Document_to_PDF')}",
                                "contentType""application/pdf",
                                "fileName""Report.pdf"
                            }
becomes:
                        "attachment": [
                            {
                                "contentData""@body('Convert_Word_Document_to_PDF')",
                                "contentType""application/pdf",
                                "fileName""Report.pdf"
                            }
 
I don't know why that fixes it but the old editor doesn't add the extra curly braces around your function while the new editor does, iirc it's related to how the underlying engine parses data inside curly braces vs without as it implies some kind of typing that totally breaks things. If you can't edit the code directly you might be able to get it to work correctly by using the Expression editor and selecting the PDF output there instead of using the dynamic expression shortcut (lightning bolt).

@Bayan000 

hi all, I am new in this forum, but I wanted to share with you how I fixed it:

 

  1. base64() used to convert the output of the content body
  2. when appending it to variable converting the output of the compose action with base64ToBinary()

this has fixed my issue.

TommyTomek_0-1727791744895.png

 

 

BR
TOmek