Microsoft Azure MyService Management Client Library for Python
Project description
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.
Installing
Install the Azure Communication Email client library for Python with pip:
pip install azure-communication-email
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)
Next steps
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.
Release History
1.0.0b1 (2022-08-09)
The first preview of the Azure Communication Email Client has the following features:
- send emails to multiple recipients with attachments
- get the status of a sent message
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file azure-communication-email-1.0.0b1.zip.
File metadata
- Download URL: azure-communication-email-1.0.0b1.zip
- Upload date:
- Size: 65.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.28.1 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
05eb0bab42893a120efe2e3895b46f0abdbcc8810badae71dde0de6c6391c454
|
|
| MD5 |
f17737a6cca007d6c0e463a8bf4f7ead
|
|
| BLAKE2b-256 |
0390b74fab9ff9fecf350eb62e70af66701eb4796990eeeed6d0e2b40085722d
|
File details
Details for the file azure_communication_email-1.0.0b1-py3-none-any.whl.
File metadata
- Download URL: azure_communication_email-1.0.0b1-py3-none-any.whl
- Upload date:
- Size: 39.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.1.1 pkginfo/1.5.0.1 requests/2.28.1 setuptools/58.1.0 requests-toolbelt/0.9.1 tqdm/4.64.0 CPython/3.9.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
cb28d1bf82785bde2c86c0f5495e9a333a7e840c4cf6ae54b88eaec062cb39d3
|
|
| MD5 |
e4c2fe5bac028619faa4276fb2115e9c
|
|
| BLAKE2b-256 |
c28b6f4e59ffc45770391456543fb92ff1b34e2a3039f651dbec8c22330d3b4c
|