使用 Azure 逻辑应用程序发送带有多个附件的电子邮件

Send Email with multiple attachment using Azure Logic App

我需要将上传到我的 Azure 存储容器的 blob 作为附件发送。上传到容器的文件数量会发生变化,因此我需要使用动态方法来添加附件。

我正在使用以下逻辑:

附加到变量值

{

"Name": items('For_each')?['DisplayName']
"ContentBytes":body('Get_blob_content')

} 当我尝试保存逻辑时,出现以下错误:

Save logic app failed
Failed to save logic app testing. The template validation failed: 'The action(s) 'Get_blob_content' referenced by 'inputs' in action 'Append_to_array_variable' are not defined in the template.'.

我该如何解决这个问题?

根据上面分享的错误消息,建议您在每个阶段或 appendtoarray 变量阶段之前保存逻辑应用,而不是一次保存整个工作流 & post 然后追加具有前一阶段输出的附加变量的值。

基于上述要求,我们在本地环境中创建了以下逻辑应用程序并对其进行了测试,它运行良好。

在我们的工作流程中,我们使用 For EachList Blobs 动作循环 blob。在 For Each 中,您可以使用 Get blob content 获取 blob 内容,然后使用 Append to array variable 附加附件。

表达式Name和ContentBytes如下:

 "ContentBytes": "@base64(body('Get_blob_content_(V2)'))",
 "Name": "@items('For_each')?['DisplayName']"

这是我们创建的逻辑应用程序的代码视图:

{
    "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": "@base64(body('Get_blob_content_(V2)'))",
                                "Name": "@items('For_each')?['DisplayName']"
                            }
                        },
                        "runAfter": {
                            "Get_blob_content_(V2)": [
                                "Succeeded"
                            ]
                        },
                        "type": "AppendToArrayVariable"
                    },
                    "Get_blob_content_(V2)": {
                        "inputs": {
                            "host": {
                                "connection": {
                                    "name": "@parameters('$connections')['azureblob']['connectionId']"
                                }
                            },
                            "method": "get",
                            "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/files/@{encodeURIComponent(encodeURIComponent(items('For_each')?['Path']))}/content",
                            "queries": {
                                "inferContentType": true
                            }
                        },
                        "runAfter": {},
                        "type": "ApiConnection"
                    }
                },
                "foreach": "@body('Lists_blobs_(V2)')?['value']",
                "runAfter": {
                    "Initialize_variable": [
                        "Succeeded"
                    ]
                },
                "type": "Foreach"
            },
            "Initialize_variable": {
                "inputs": {
                    "variables": [
                        {
                            "name": "attachments",
                            "type": "array"
                        }
                    ]
                },
                "runAfter": {
                    "Lists_blobs_(V2)": [
                        "Succeeded"
                    ]
                },
                "type": "InitializeVariable"
            },
            "Lists_blobs_(V2)": {
                "inputs": {
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azureblob']['connectionId']"
                        }
                    },
                    "method": "get",
                    "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/foldersV2/@{encodeURIComponent(encodeURIComponent('JTJmcmVwb3J0cw=='))}",
                    "queries": {
                        "nextPageMarker": "",
                        "useFlatListing": false
                    }
                },
                "metadata": {
                    "JTJmcmVwb3J0cw==": "/reports"
                },
                "runAfter": {},
                "type": "ApiConnection"
            },
            "Send_an_email_(V2)": {
                "inputs": {
                    "body": {
                        "Attachments": "@variables('attachments')",
                        "Body": "<p>tested logic app flow successfully</p>",
                        "Subject": "blob test",
                        "To": "<username>@microsoft.com"
                    },
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['office365']['connectionId']"
                        }
                    },
                    "method": "post",
                    "path": "/v2/Mail"
                },
                "runAfter": {
                    "For_each": [
                        "Succeeded"
                    ]
                },
                "type": "ApiConnection"
            }
        },
        "contentVersion": "1.0.0.0",
        "outputs": {},
        "parameters": {
            "$connections": {
                "defaultValue": {},
                "type": "Object"
            }
        },
        "triggers": {
            "When_a_blob_is_added_or_modified_(properties_only)_(V2)": {
                "evaluatedRecurrence": {
                    "frequency": "Minute",
                    "interval": 1
                },
                "inputs": {
                    "host": {
                        "connection": {
                            "name": "@parameters('$connections')['azureblob']['connectionId']"
                        }
                    },
                    "method": "get",
                    "path": "/v2/datasets/@{encodeURIComponent(encodeURIComponent('AccountNameFromSettings'))}/triggers/batch/onupdatedfile",
                    "queries": {
                        "checkBothCreatedAndModifiedDateTime": false,
                        "folderId": "JTJmcmVwb3J0cw==",
                        "maxFileCount": 10
                    }
                },
                "metadata": {
                    "JTJmcmVwb3J0cw==": "/reports"
                },
                "recurrence": {
                    "frequency": "Minute",
                    "interval": 1
                },
                "splitOn": "@triggerBody()",
                "type": "ApiConnection"
            }
        }
    },
    "parameters": {
        "$connections": {
            "value": {
                "azureblob": {
                    "connectionId": "/subscriptions/<sub-ID>/resourceGroups/<resourceGroup>/providers/Microsoft.Web/connections/azureblob",
                    "connectionName": "azureblob",
                    "id": "/subscriptions/<sub-id>/providers/Microsoft.Web/locations/eastus/managedApis/azureblob"
                },
                "office365": {
                    "connectionId": "/subscriptions/<sub-id>/resourceGroups/<resroucegroup>/providers/Microsoft.Web/connections/office365",
                    "connectionName": "office365",
                    "id": "/subscriptions/<sub-id>/providers/Microsoft.Web/locations/eastus/managedApis/office365"
                }
            }
        }
    }
}

这里是示例输出以供参考: