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:
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:
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:
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",
"help": "http://sendgrid.com/docs/API_Reference/Web_API_v3/Mail/errors.html#message.attachments.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:
Below is the Full Logic App structure:
By this, we can send all received attachment (from the O365) to the Send Grid action; in one execution.
Published Sep 07, 2021
Version 1.0mshboul
Microsoft
Joined October 08, 2020
Azure Integration Services Blog
Follow this blog board to get notified when there's new activity