Azure Communication Email client library for Python¶
This package contains a Python SDK for Azure Communication Services for Email.
Key concepts¶
The Azure Communication Email package is used to do following:
Send emails to multiple types of recipients
Query the status of a sent email message
Getting started¶
Prerequisites¶
You need an Azure subscription, a Communication Service Resource, and an Email Communication Resource with an active Domain.
To create these resource, you can use the Azure Portal, the Azure PowerShell, or the .NET management client library.
Examples¶
EmailClient
provides the functionality to send email messages .
Authentication¶
Email clients can be authenticated using the connection string acquired from an Azure Communication Resource in the Azure Portal.
from azure.communication.email import EmailClient
connection_string = "endpoint=https://<resource-name>.communication.azure.com/;accessKey=<Base64-Encoded-Key>"
client = EmailClient.from_connection_string(connection_string);
Email clients can also be authenticated using an AzureKeyCredential.
from azure.communication.email import EmailClient
from azure.core.credentials import AzureKeyCredential
credential = AzureKeyCredential("<api_key>")
endpoint = "https://<resource-name>.communication.azure.com/"
client = EmailClient(endpoint, credential);
Send an Email Message¶
To send an email message, call the send
function from the EmailClient
.
content = EmailContent(
subject="This is the subject",
plain_text="This is the body",
html= "<html><h1>This is the body</h1></html>",
)
address = EmailAddress(email="customer@domain.com", display_name="Customer Name")
message = EmailMessage(
sender="sender@contoso.com",
content=content,
recipients=EmailRecipients(to=[address])
)
response = client.send(message)
Send an Email Message to Multiple Recipients¶
To send an email message to multiple recipients, add a object for each recipient type and an object for each recipient.
content = EmailContent(
subject="This is the subject",
plain_text="This is the body",
html= "<html><h1>This is the body</h1></html>",
)
recipients = EmailRecipients(
to=[
EmailAddress(email="customer@domain.com", display_name="Customer Name"),
EmailAddress(email="customer2@domain.com", display_name="Customer Name 2"),
],
cc=[
EmailAddress(email="ccCustomer@domain.com", display_name="CC Customer Name"),
EmailAddress(email="ccCustomer2@domain.com", display_name="CC Customer Name 2"),
],
bcc=[
EmailAddress(email="bccCustomer@domain.com", display_name="BCC Customer Name"),
EmailAddress(email="bccCustomer2@domain.com", display_name="BCC Customer Name 2"),
]
)
message = EmailMessage(sender="sender@contoso.com", content=content, recipients=recipients)
response = client.send(message)
Send Email with Attachments¶
Azure Communication Services support sending email with attachments.
import base64
content = EmailContent(
subject="This is the subject",
plain_text="This is the body",
html= "<html><h1>This is the body</h1></html>",
)
address = EmailAddress(email="customer@domain.com", display_name="Customer Name")
with open("C://readme.txt", "r") as file:
file_contents = file.read()
file_bytes_b64 = base64.b64encode(bytes(file_contents, 'utf-8'))
attachment = EmailAttachment(
name="attachment.txt",
attachment_type="txt",
content_bytes_base64=file_bytes_b64.decode()
)
message = EmailMessage(
sender="sender@contoso.com",
content=content,
recipients=EmailRecipients(to=[address]),
attachments=[attachment]
)
response = client.send(message)
Get Email Message Status¶
The result from the send
call contains a message_id
which can be used to query the status of the email.
response = client.send(message)
status = client.get_sent_status(response.message_id)
Troubleshooting¶
Email operations will throw an exception if the request to the server fails. The Email client will raise exceptions defined in Azure Core.
from azure.core.exceptions import HttpResponseError
try:
response = email_client.send(message)
except HttpResponseError as ex:
print('Exception:')
print(ex)
Contributing¶
This project welcomes contributions and suggestions. Most contributions require you to agree to a Contributor License Agreement (CLA) declaring that you have the right to, and actually do, grant us the rights to use your contribution. For details, visit cla.microsoft.com.
This project has adopted the Microsoft Open Source Code of Conduct. For more information see the Code of Conduct FAQ or contact opencode@microsoft.com with any additional questions or comments.