Search code examples
azure-logic-apps

Logic app, send mail with attachment using graph API and managed identity


I am trying to send an e-mail with attachment, inside a logic app, using Graph API but I can't figure out a way. I can perfectly send a mail without attachment but I don't know how to send with attachment.

in my http post request I'm using the body

enter image description here

       {
  "message": {
    "body": {
      "content": "Sending test file.",
      "contentType": "HTML"
    },
    "ccRecipients": [
      {
        "emailAddress": {
          "address": "[email protected]"
        }
      }
    ],
    "subject": "New files",
    "toRecipients": [
      {
        "attachments": {
          "contentBytes": "@{body('Get_file_content')}",
          "contentType": "text/plain",
          "name": "@{items('For_each')?['Name']}",
          "odata.type": "#microsoft.graph.fileAttachment"
        },
        "emailAddress": {
          "address": "[email protected]"
        }
      }
    ]
  },
  "saveToSentItems": "false"
}

The flow fails with the error

{
  "error": {
    "code": "RequestBodyRead",
    "message": "The property 'attachments' does not exist on type 'microsoft.graph.recipient'. Make sure to only use property names that are defined by the type or mark the type as open type."
  }
}

if I save the body with

"@odata.type" = "#microsoft.graph.fileAttachment"

I get this error

enter image description here


Solution

  • @ is a reserved symbol in LogicApps/PowerAutomate when it comes to being the first character in a string.

    To overcome that, escape the @ sign by prefixing it with another, like so ...

    {
      "@@odata.type": "#microsoft.graph.fileAttachment"
    }