Forum Discussion

Bayan000's avatar
Bayan000
Copper Contributor
Nov 03, 2023

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. 

  • Bayan000's avatar
    Bayan000
    Copper Contributor
    The issue was resolved on its own by using the old flow editor instead of the new designer.
    • FvVega's avatar
      FvVega
      Copper Contributor

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

    • LydiaElder's avatar
      LydiaElder
      Copper Contributor
      This was exactly what I needed to fix my problem! Thanks for sharing this.
    • amytucker714's avatar
      amytucker714
      Copper 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.

      • WCarterMVUSD's avatar
        WCarterMVUSD
        Copper Contributor

        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).
    • GKates's avatar
      GKates
      Copper 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_Kojima's avatar
    Shin_Kojima
    Copper Contributor

    This should work like a charm

     

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



    Bayan000 

      • GKates's avatar
        GKates
        Copper 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_13's avatar
    jk_13
    Copper Contributor
    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.
  • TommyTomek's avatar
    TommyTomek
    Copper Contributor

    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.

     

     

    BR
    TOmek

    • wei_lingfoo's avatar
      wei_lingfoo
      Copper 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.

  • BufMobile's avatar
    BufMobile
    Copper Contributor

    I had to switch to the old editor.

    1. Manually trigger the flow
    2. 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
    3. For Each (Value)
    4. Get Metadata Using Path
      Use the concat() function to put together the file path and the file name
    5. Get File Content using id from above
    6. 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.

     

Resources