skip to Main Content

I’m trying to send email using azure email communication Python SDK

poller = self.email_client.begin_send(
                message=payload,
            )

time_elapsed = 0

while not poller.done():
    logger.info("Email send poller status: " + poller.status())

    poller.wait(POLLER_WAIT_TIME)
    time_elapsed += POLLER_WAIT_TIME

    if time_elapsed > 18 * POLLER_WAIT_TIME:
        raise RuntimeError("Polling timed out.")

result = poller.result()

Payload:

{
    "content": {
        "subject": "Sample Subject",
        "html": "<!DOCTYPE html>n<html>n<head>nnn</head>n<body>nn<div style="padding-left: 5%; padding-right: 5%">n<p>Hello Team,</p></div>n</body>n</html>"
    },
    "recipients": {
        "to": [
            {
                "address": "my_email.com",
                "displayName": "My Name"
            }
        ],
        "cc": [],
        "bcc": []
    },
    "senderAddress": "[email protected]",
    "attachments": []
}

Result:

{
   "id":"570e68e8-0418-4cde-bd5e-49d9a9bf3f49",
   "status":"Succeeded",
   "error":null
}

I need message_id for various purposes, but I’m not getting message_id here from azure api, am i missing anything ?

How to get the message_id of the email sent?

2

Answers


  1. This code may be of interest to you

    from azure.communication.email import EmailClient, EmailContent, 
    EmailRecipients, EmailSender
    from azure.identity import DefaultAzureCredential
    
    # Initialize the EmailClient with your connection string
    email_client = 
    EmailClient.from_connection_string("your_connection_string_here")
    
    # Construct the email message payload
    email_content = EmailContent(subject="Sample Subject")
    email_content.html = "<div><p>Hello Team,</p></div>"
    recipients = EmailRecipients(to=[{"email": "[email protected]", 
    "displayName": "Recipient Name"}])
    sender = EmailSender(email="[email protected]", display_name="Sender 
    Name")
    
    # Send the email
    send_operation = email_client.send(email_content, recipients, sender)
    
    # Wait for the send operation to complete and retrieve the result
    send_result = send_operation.result()
    
    # Extract the Message ID from the send result
    message_id = send_result.message_id
    print(f"Message ID: {message_id}")
    

    Retrieving Message ID from Azure Email Service

    Login or Signup to reply.
  2. From your result example above the message id is 570e68e8-0418-4cde-bd5e-49d9a9bf3f49

    {
       "id":"570e68e8-0418-4cde-bd5e-49d9a9bf3f49",
       "status":"Succeeded",
       "error":null
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search