Tuesday, September 7, 2021

Send Grid Connector - Send Email (Multiple Attachments)

In the scenario when we have a logic app with O365 Trigger - when a new email arrives, and the email comes with multiple attachments; there is a current limitation in Send Grid connector -Send Email V4; where we cannot use the attachments array to send the received attachments all at once in one email.

 

The below logic app shows the limitation scenario:

mshboul_4-1630962837875.png

 

If we used the above structure, the send grid action will fail with the below error:

{
  "error": {
    "code"400,
    "message""Parameter \"Content\" should be provided",
    "source""logic-apis-northcentralus.azure-apim.net",
    "path""choose[3]\\when[1]",
    "policyId""",
    "clientRequestId""ef65bcb8-5ca2-4d8d-b373-05ede281a144"
  }

}

The reason the action fails; is due to the direct mapping from the O365 attachment array to the send grid attachments array; which has a difference in parameter names; such as Content.

 

For Send Grid Connector - Send email (V4); we can manually add the attachments using the individual attachment form; as below:

mshboul_5-1630962909636.png

 

But this will cause each attachment to be sent in a separate email (due to the for each); which is not the desired target of the logic app.

 

By looking into code view, of the attachment array structure; when adding the attachment manually; we find it to be in the below structure:

 "attachments": [
                                    {
                                        "content""@{items('For_each')?['contentBytes']}",
                                        "contenttype""@items('For_each')?['contentType']",
                                        "filename""@items('For_each')?['name']"
                                    }
                                ]
 
So, the work around for this limitation, is to build the above attachments array within the logic app, and pass it as an attachment array to the Send Grid action; as below:
mshboul_2-1630962440623.png

Note: The attachment content from the dynamic content is "@{base64ToString(items('For_each')?['contentBytes'])}"; using it like this causes the send grid action to fail with error:

  {
                "message""The attachment content must be base64 encoded.",
                "field""attachments.0.content",
            }
 
So, we need to switch to Code view, and update the Append Array action to have the below body (we removed the base64ToString expression) :
 {
                                "content""@{items('For_each')?['contentBytes']}",
                                "contenttype""@{items('For_each')?['contentType']}",
                                "filename""@{items('For_each')?['name']}"
                            }

 

Also, the For Each parallelism need to be set to 1; in order to avoid incorrect variable assignments.

 

After having the array ready, we can pass it directly to the send grid action as below:

mshboul_3-1630962760572.png

 

Below is the Full Logic App structure:

mshboul_6-1630963169093.png

 

By this, we can send all received attachment (from the O365) to the Send Grid action; in one execution.

 

 

 

Posted at https://sl.advdat.com/38Pfi4B