Forum Discussion
Sending a PDF file as an email attachment with Power Automate corrupts attachment
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-participants-in-8-easy-steps/ba-p/3697139), 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.
- powertowerCopper Contributor
Bayan000 I can confirm switching back to old designer fixes this issue (as of 12/17/2023).
- Bayan000Copper ContributorThe issue was resolved on its own by using the old flow editor instead of the new designer.
- LydiaElderCopper ContributorThis was exactly what I needed to fix my problem! Thanks for sharing this.
- amytucker714Copper Contributor
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.
- WCarterMVUSDCopper Contributor
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).
- punkymattaCopper Contributor
Bayan000 I am having this exact issue. Did you find a solution?
- GKatesCopper Contributor
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.
- Shin_KojimaCopper Contributor
This should work like a charm
base64ToBinary(outputs('GET_FILE_CONTENT')?['body']?['$content'])
- daisysx1987Copper ContributorWhere do I add this?
- GKatesCopper Contributor
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
- jk_13Copper ContributorI 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.
- daisysx1987Copper Contributor
Bayan000 I am still having this issue can you share your flow?
- TommyTomekCopper Contributor
hi all, I am new in this forum, but I wanted to share with you how I fixed it:
- base64() used to convert the output of the content body
- when appending it to variable converting the output of the compose action with base64ToBinary()
this has fixed my issue.
BR
TOmek- wei_lingfooCopper Contributor
Hi, TommyTomek , can I check after you append to the variable, how did you attach the array variable to the send email action? believed you can't attach a variable as attachment in this action.
I'm facing the same issue of wanting to send a pdf/excel file over but the file appears to be corrupted after sending. Hope you could help me with it thanks.
- BufMobileCopper Contributor
I had to switch to the old editor.
- Manually trigger the flow
- List Rows Present in a Table
a. Excel file with table that has email address, other details needed for the email, and a list of the pdf file names generated with a cmd prompt "dir /b/o *.pdf > flist.txt" and pasted into the proper rows for the person indicated - For Each (Value)
- Get Metadata Using Path
Use the concat() function to put together the file path and the file name - Get File Content using id from above
- Send an email (V2)
a. Attachment File Name from Metadata
b. File Content from Get File Content
This worked great. Sent 62 emails with pdf file attachments of around 64K in about 90 seconds.