azure logic app send email and csv or excel attachment

Madhu Rao 160 Reputation points
2025-05-26T04:34:08.8366667+00:00

Hi All,

I am sending an email to the logic app using the HTTP request JSON method.

The email process works, but the attachment is not readable.

User's image

I am using a script available on the internet to test it.

$tempFile = "C:\Users\testadmin\Downloads\WinVnsTemp.csv"
  
$attachmentsArray = @(
    @{
        "Name" = "WinVnsTemp.csv"
        "ContentBytes" = [Convert]::ToBase64String([System.IO.File]::ReadAllBytes($tempFile))
    }
)

$emailConnection = "https://***********.azurewebsites.net:443/api/trigger/triggers/When_a_HTTP_request_is_received/invoke?api-version=2022-05-01&sp=%2Ftriggers%2FWhen_a_HTTP_request_is_received%2Frun&sv=1.0&sig=WCEQ1dISFtxCY9vthPWkFULXh_zL0WmafLtwddX_rGY"
$emailOutput = [PSCustomObject]@{
    "To" = "admin.test@hot***.com"
    "Subject" = "Test email attachment"
    "Body" = "Hi there! testing email attachments"
    "Attachments" = $attachmentsArray
}
$JSONBody = $emailOutput | ConvertTo-Json -Depth 5
Invoke-RestMethod -Uri $emailConnection -Body $JSONBody -ContentType 'application/json' -Method POST | Out-Null

If I remove ToBase64String conversion it look like below.

User's image

Logic app flow

User's image

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

Answer accepted by question author
  1. RithwikBojja 3,210 Reputation points Microsoft External Staff Moderator
    2025-05-27T05:18:16.46+00:00

    Hi @Madhu Rao ,

    Below design works for me:

    Initially called logic app using below PowerShell Script:

    
    $rithfile = "C:\Users\rbojja\Downloads\test.csv"
    
    $attachmentsArray = @(
    
        @{
    
            "Name" = "RithAttachment.csv"
    
            "ContentBytes" = [Convert]::ToBase64String([System.IO.File]::ReadAllBytes($rithfile))
    
        }
    
    )
    
    $rithcon = "https://rithte.azurewebsites.net:443/api/testemailwkflw/triggers/When_a_HTTP_request_is_received/invoke?api-version=2022-05-01&sp=%2Ftriggers%2FWhen_a_HTTP_request_is_received%2Frun&sv=1.0&sig=Q81WrithwikkWZCI"
    
    $rithout = [PSCustomObject]@{
    
        "To" = "******@mit.com"
    
        "Subject" = "Test email attachment"
    
        "Body" = "Hi there! testing email attachments"
    
        "Attachments" = $attachmentsArray
    
    }
    
    $rijson = $rithout | ConvertTo-Json -Depth 5
    
    Invoke-RestMethod -Uri $rithcon -Body $rijson -ContentType 'application/json' -Method POST | Out-Null
    
    

    Design:

    image

    Then:

    image

    base64(base64ToBinary(items('For_each')['ContentBytes']))

    Code View:

    
    {
    
        "definition": {
    
            "$schema": "https://schema.management.azure.com/providers/Microsoft.Logic/schemas/2016-06-01/workflowdefinition.json#",
    
            "actions": {
    
                "For_each": {
    
                    "type": "Foreach",
    
                    "foreach": "@triggerBody()?['Attachments']",
    
                    "actions": {
    
                        "Send_an_email_(V2)": {
    
                            "type": "ApiConnection",
    
                            "inputs": {
    
                                "host": {
    
                                    "connection": {
    
                                        "referenceName": "office365"
    
                                    }
    
                                },
    
                                "method": "post",
    
                                "body": {
    
                                    "To": "@triggerBody()?['To']",
    
                                    "Subject": "@triggerBody()?['Subject']",
    
                                    "Body": "<p class=\"editor-paragraph\">@{triggerBody()?['Body']}</p>",
    
                                    "Attachments": [
    
                                        {
    
                                            "Name": "@items('For_each')['Name']",
    
                                            "ContentBytes": "@base64(base64ToBinary(items('For_each')['ContentBytes']))"
    
                                        }
    
                                    ],
    
                                    "Importance": "Normal"
    
                                },
    
                                "path": "/v2/Mail"
    
                            }
    
                        }
    
                    },
    
                    "runAfter": {}
    
                }
    
            },
    
            "contentVersion": "1.0.0.0",
    
            "outputs": {},
    
            "triggers": {
    
                "When_a_HTTP_request_is_received": {
    
                    "type": "Request",
    
                    "kind": "Http"
    
                }
    
            }
    
        },
    
        "kind": "Stateful"
    
    }
    
    

    Output:

    enter image description here

    enter image description here

    enter image description here


    If this answer was helpful, please click "Accept the answer" and mark Yes, as this can help other community members.

    enter image description here

    If you have any other questions or are still experiencing issues, feel free to ask in the "comments" section, and I'd be happy to help.

    1 person found this answer helpful.

1 additional answer

Sort by: Most helpful
  1. Alex Burlachenko 18,575 Reputation points Volunteer Moderator
    2025-05-26T07:09:05.63+00:00

    Hey Madhu,

    Thanks for posting your question about sending emails with attachments in azure logic apps ))

    First off, the base64 conversion you're doing is actually the correct approach according to microsoft's documentation on logic apps email attachments (you can check the official docs here: [Microsoft Logic Apps Documentation]). The attachment needs to be base64 encoded when sending through http requests to logic apps.

    The problem might be in how the logic app is processing the incoming data. In your workflow, after the "when a http request is received" trigger, you should have a "send email v2" action. Make sure the attachment part is configured properly there. The contentbytes field should directly receive your base64 string without additional processing.

    One thing i noticed - in your powershell script you're doing the conversion correctly, but maybe the logic app needs some tweaking. Could you check if the "contentbytes" field in the send email action matches exactly what your script sends? Sometimes the json structure gets messed up during transfer.

    Also, just a heads up - for excel files you'd follow the same base64 approach but make sure the file is properly closed in excel before attaching. I've seen cases where the file gets corrupted if its still open when reading bytes ))

    If you want to share more details about how your logic app is processing the attachment, i can give more specific advice. The screenshots help but i cant see the full configuration. Maybe check if there are any additional steps modifying the attachment data before it gets sent?

    Hope this helps get you unstuck! Let me know how it goes

    Best regards,
    Alex
    P.S. If my answer help to you, please Accept my answer
    PPS That is my Answer and not a Comment
    https://ctrlaltdel.blog/
    
    0 comments No comments

Your answer

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