Skip to content

Email with Attachments

This recipe shows how to send emails with file attachments using Azure Communication Services (ACS) and Python.

This recipe walks these steps in order:

flowchart TD
    START["Email with Attachments"]
    N1["Prerequisites"]
    N2["1. SDK Installation"]
    N3["2. Prepare Attachments"]
    N4["3. Send Email with Multiple Attachments"]
    N5["4. Attachment Size Limits"]
    N6["5. Multiple Attachment Types"]
    N7["6. Best Practices"]
    START --> N1
    N1 --> N2
    N2 --> N3
    N3 --> N4
    N4 --> N5
    N5 --> N6
    N6 --> N7

Prerequisites

1. SDK Installation

pip install azure-communication-email

2. Prepare Attachments

Attachments must be base64-encoded and include a name and content type.

import base64

def prepare_attachment(file_path, content_type):
    with open(file_path, "rb") as file:
        file_contents = file.read()
        content_bytes = base64.b64encode(file_contents).decode("utf-8")

    return {
        "name": os.path.basename(file_path),
        "contentType": content_type,
        "contentInBase64": content_bytes
    }

3. Send Email with Multiple Attachments

Include the prepared attachments in the attachments list of the message.

import os
from azure.communication.email import EmailClient
from azure.identity import DefaultAzureCredential

# Initialize client
endpoint = os.getenv("COMMUNICATION_SERVICES_ENDPOINT")
email_client = EmailClient(endpoint, DefaultAzureCredential())

# Prepare attachments
attachment1 = prepare_attachment("document.pdf", "application/pdf")
attachment2 = prepare_attachment("image.png", "image/png")

# Create email message
message = {
    "content": {
        "subject": "ACS Email with Multiple Attachments",
        "plainText": "See the attached files for your reference."
    },
    "recipients": {
        "to": [{"address": "<recipient-email-address>"}]
    },
    "senderAddress": "<verified-sender-email-address>",
    "attachments": [attachment1, attachment2]
}

# Send email
poller = email_client.begin_send(message)
result = poller.result()
print(f"Message ID: {result['messageId']}")

4. Attachment Size Limits

ACS has limits on the total size of an email and individual attachments.

  • Total Email Request Size: 10 MB (including attachments), per the current ACS service limits page. Higher limits (up to 30 MB) are available via Azure Support request.
  • Max Attachments: No strict limit, but bounded by total email size.

Important

Base64 encoding inflates message size by approximately 33%, so a nominal 10 MB request accommodates roughly 7.5 MB of pre-encoded content. For files above the request-size ceiling, store the file in Azure Blob Storage and include a SAS link in the email body instead. See reference/platform-limits.md for the authoritative repository reference.

5. Multiple Attachment Types

Common content types include: - application/pdf - image/png - image/jpeg - text/plain - application/vnd.openxmlformats-officedocument.wordprocessingml.document (Word doc)

6. Best Practices

  • Always validate file size before encoding to avoid exceeding limits.
  • Use meaningful attachment names.
  • Consider compression for large documents.

See Also

Sources