Logic App: How to create email sending notification with multiple attachments?

Nico Wijaya 45 Reputation points
2023-10-26T08:21:44.82+00:00

Hi,

I'm trying to create a logic app workflow with HTTP trigger which will receive the following payload.

{
    "email_body": "Hi Users,<br>Please find the files attached.",
    "email_recipients": "recipients@xyz.com;",
    "email_subject": "Do not reply - processed files",
    "email_attachments": [{
            "container_name": "bronze",
            "blob_name": "rootfolder/subfolders/folium_map.html"
        }, {
            "container_name": "bronze",
            "blob_name": "rootfolder/subfolders/stdout.txt"
        }, {
            "container_name": "bronze",
            "blob_name": "rootfolder/subfolders/stderr.txt"
        }
    ]
}

Upon receiving such request, I need to send an email containing the attachments depending of however many is defined in the "email_attachments" array. I will need to lookup the content of such blob from my data lake. Here's my code:

{
    "definition": {
        "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
        "actions": {
            "For_each": {
                "actions": {
                    "Append_to_array_variable": {
                        "inputs": {
                            "name": "attachments",
                            "value": {
                                "ContentBytes": "@{body('Read_blob_content')?['content']}",
                                "Name": "@{body('Read_blob_content')?['blob_name']}"
                            }
                        },
                        "runAfter": {
                            "Read_blob_content": [
                                "SUCCEEDED"
                            ]
                        },
                        "type": "AppendToArrayVariable"
                    },
                    "Read_blob_content": {
                        "inputs": {
                            "parameters": {
                                "blobName": "@items('For_each')?['items(''For_each'')']?['blob_name']",
                                "containerName": "@items('For_each')?['items(''For_each'')']?['container_name']"
                            },
                            "serviceProviderConfiguration": {
                                "connectionName": "AzureBlob-4",
                                "operationId": "readBlob",
                                "serviceProviderId": "/serviceProviders/AzureBlob"
                            }
                        },
                        "type": "ServiceProvider"
                    }
                },
                "foreach": "@triggerBody()?['email_attachments']",
                "runAfter": {
                    "Initialize_variable_-_attachments": [
                        "SUCCEEDED"
                    ]
                },
                "runtimeConfiguration": {
                    "concurrency": {
                        "repetitions": 1
                    }
                },
                "type": "Foreach"
            },
            "Initialize_variable_-_attachments": {
                "inputs": {
                    "variables": [
                        {
                            "name": "attachments",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {},
                "type": "InitializeVariable"
            },
            "Response": {
                "inputs": {
                    "body": "@concat('Email sent to ',triggerBody()?['email_recipients'], ' with the subject of \"',triggerBody()?['email_subject'],'\"')",
                    "headers": {
                        "Content-Type": "application/json"
                    },
                    "statusCode": 200
                },
                "kind": "http",
                "runAfter": {
                    "Send_an_email_from_a_shared_mailbox_(V2)": [
                        "Succeeded"
                    ]
                },
                "type": "Response"
            },
            "Send_an_email_from_a_shared_mailbox_(V2)": {
                "inputs": {
                    "body": {
                        "Attachments": "@variables('attachments')",
                        "Body": "<p>@{triggerBody()?['email_body']}</p>",
                        "Importance": "Normal",
                        "MailboxAddress": "sender@xyz.com",
                        "Subject": "@triggerBody()?['email_subject']",
                        "To": "@triggerBody()?['email_recipients']"
                    },
                    "host": {
                        "connection": {
                            "referenceName": "office365"
                        }
                    },
                    "method": "post",
                    "path": "/v2/SharedMailbox/Mail"
                },
                "runAfter": {
                    "For_each": [
                        "SUCCEEDED"
                    ]
                },
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "triggers": {
            "manual": {
                "inputs": {
                    "schema": {
                        "properties": {
                            "email_attachments": {
                                "items": {
                                    "properties": {
                                        "blob_name": {
                                            "type": "string"
                                        },
                                        "container_name": {
                                            "type": "string"
                                        }
                                    },
                                    "required": [
                                        "container_name",
                                        "blob_name"
                                    ],
                                    "type": "object"
                                },
                                "type": "array"
                            },
                            "email_body": {
                                "type": "string"
                            },
                            "email_recipients": {
                                "type": "string"
                            },
                            "email_subject": {
                                "type": "string"
                            }
                        },
                        "type": "object"
                    }
                },
                "kind": "Http",
                "type": "Request"
            }
        }
    },
    "kind": "Stateful"
}

When I called the workflow, it ended with the following error:

InvalidTemplate

Unable to process template language expressions for action 'For_each' at line '0' and column '0': 'The template language expression 'triggerBody()?['email_attachments']' cannot be evaluated because property 'email_attachments' cannot be selected. Property selection is not supported on values of type 'String'. Please see https://aka.ms/logicexpressions for usage details.'.

User's image

I tried using array(triggerBody()?['email_attachments']) in the "Select An Output From Previous Steps" field on the "For each" action, but that doesn't help either.

Can anyone help shedding a light of how I can achieve that please?

Thanks in advance.

Azure Logic Apps
Azure Logic Apps
An Azure service that automates the access and use of data across clouds without writing code.
3,094 questions
{count} votes

1 answer

Sort by: Most helpful
  1. Mike Urnun 9,801 Reputation points Microsoft Employee
    2023-10-31T00:21:12.32+00:00

    Hello @Nico Wijaya - Thanks for reaching out, and posting on the MS Q&A.

    The expression @triggerBody()?['email_attachments'] used for the Foreach loop is valid and works for me, however, the following used in the Read Blob action looks invalid:

    "parameters": {
      "blobName": "@items('For_each')?['items(''For_each'')']?['blob_name']",
      "containerName": "@items('For_each')?['items(''For_each'')']?['container_name']"
    }
    

    Specifically, the repeating use of Foreach in @items('For_each')?['items(''For_each'')'] and the double quotes surrounding the second Foreach ['items(''For_each'')'] looks invalid to me.


    Please "Accept Answer" if the answer is helpful so that others in the community may benefit from your experience.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.